二叉树实现 java代码,二叉树非递归遍历代码java

  二叉树实现 java代码,二叉树非递归遍历代码java

  

目录

1.二叉树基本概念见上节:详解爪哇中二叉树的基础概念(递归迭代)

 

  2.本次展示链式存储

  以此图为例,完整代码如下:

  //基础二叉树实现//使用左右孩子表示法导入Java。util。*;导入Java。util。德雀;公共类myBinTree {私有静态类TreeNode { char valTreeNode向左;TreeNode右侧;公共TreeNode(char val){ this。val=val} }公共静态TreeNode build(){ TreeNode nodeA=new TreeNode( A );TreeNode nodeB=新的TreeNode( B );TreeNode nodeC=新的TreeNode( C );TreeNode节点=新的TreeNode( D );TreeNode nodeE=新TreeNode( E );TreeNode nodeF=新的TreeNode( F );TreeNode nodeG=新TreeNode( G );TreeNode nodeH=新的TreeNode( H );nodea . left=nodebnodea . right=nodecnodeb . left=nodednodeb . right=nodenodee . right=nodehnodec . left=nodefnodec . right=nodeG返回nodeA} //方法1(递归) //先序遍历: 根左右public static void preOrder(TreeNode root){ if(root==null){ return;}系统。出去。打印(根。val );前序(根。左);前序(根。对);} //方法1(递归) //中序遍历公共静态void in order(TreeNode root){ if(root==null){ return;}按顺序(根。左);系统。出去。打印(根。val );按顺序(根。对);} //方法1(递归) //后序遍历公共静态void post order(TreeNode root){ if(root==null){ return;}邮政订单(根。左);邮政订单(根。对);系统。出去。打印(根。val );} //方法2(迭代) //先序遍历(迭代)public static void preOrderNonRecursion(TreeNode root){ if(root==null){ return;} DequeTreeNode stack=new链表();堆栈。推(根);而(!堆栈。isempty()){ TreeNode cur=stack。pop();系统。出去。打印(当前。val );if(cur.right!=null){ stack。推(当前。对);} if(cur.left!=null){ stack。推(当前。左);} } } //方法2(迭代) //中序遍历(迭代)顺序遍历中的公共静态void

  NonRecursion(TreeNode root) { if(root==null){ return ; } Deque<TreeNode> stack=new LinkedList<>(); // 当前走到的节点 TreeNode cur=root; while (!stack.isEmpty() cur!=null){ // 不管三七二十一,先一路向左走到根儿~ while (cur!=null){ stack.push(cur); cur=cur.left; } // 此时cur为空,说明走到了null,此时栈顶就存放了左树为空的节点 cur=stack.pop(); System.out.print(cur.val+" "); // 继续访问右子树 cur=cur.right; } } //方法2(迭代) //后序遍历 (迭代) public static void postOrderNonRecursion(TreeNode root){ if(root==null){ return; } Deque<TreeNode> stack=new LinkedList<>(); TreeNode cur=root; TreeNode prev=null; while (!stack.isEmpty() cur!=null){ while (cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); if(cur.right==null prev==cur.right){ System.out.print(cur.val+" "); prev=cur; cur=null; }else { stack.push(cur); cur=cur.right; } } } //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树中一共有多少个节点,返回节点数 //此时的访问就不再是输出节点值,而是计数器 + 1操作 public static int getNodes(TreeNode root){ if(root==null){ return 0; } return 1+getNodes(root.left)+getNodes(root.right); } //方法2(迭代) //使用层序遍历来统计当前树中的节点个数 public static int getNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque<TreeNode> queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode cur = queue.poll(); size++; if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } return size; } //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树的叶子结点个数 public static int getLeafNodes(TreeNode root){ if(root==null){ return 0; } if(root.left==null && root.right==null){ return 1; } return getLeafNodes(root.left)+getLeafNodes(root.right); } //方法2(迭代) //使用层序遍历来统计叶子结点的个数 public static int getLeafNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque<TreeNode> queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ TreeNode cur=queue.poll(); if(cur.left==null && cur.right==null){ size++; } if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } return size; } //层序遍历 public static void levelOrder(TreeNode root) { if(root==null){ return ; } // 借助队列来实现遍历过程 Deque<TreeNode> queue =new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ int size=queue.size(); for (int i = 0; i < size; i++) { TreeNode cur=queue.poll(); System.out.print(cur.val+" "); if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } } } //传入一个以root为根节点的二叉树,就能求出该树的高度 public static int height(TreeNode root){ if(root==null){ return 0; } return 1+ Math.max(height(root.left),height(root.right)); } //求出以root为根节点的二叉树第k层的节点个数 public static int getKLevelNodes(TreeNode root,int k){ if(root==null k<=0){ return 0; } if(k==1){ return 1; } return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1); } //判断当前以root为根节点的二叉树中是否包含指定元素val, //若存在返回true,不存在返回false public static boolean contains(TreeNode root,char value){ if(root==null){ return false; } if(root.val==value){ return true; } return contains(root.left,value) contains(root.right,value); } public static void main(String[] args) { TreeNode root=build(); System.out.println("方法1(递归):前序遍历的结果为:"); preOrder(root); System.out.println(); System.out.println("方法2(迭代):前序遍历的结果为:"); preOrderNonRecursion(root); System.out.println(); System.out.println("方法1(递归):中序遍历的结果为:"); inOrder(root); System.out.println(); System.out.println("方法2(迭代):中序遍历的结果为:"); inorderTraversalNonRecursion(root); System.out.println(); System.out.println("方法1(递归):后序遍历的结果为:"); postOrder(root); System.out.println(); System.out.println("方法2(迭代):后序遍历的结果为:"); postOrderNonRecursion(root); System.out.println(); System.out.println(); System.out.println("层序遍历的结果为:"); levelOrder(root); System.out.println(); System.out.println(); System.out.println("方法1(递归):当前二叉树一共有:"+getNodes(root)+"个节点数"); System.out.println("方法2(迭代):当前二叉树一共有:"+getNodesNoRecursion(root)+"个节点数"); System.out.println("方法1(递归):当前二叉树一共有:"+getLeafNodes(root)+"个叶子节点数"); System.out.println("方法2(迭代):当前二叉树一共有:"+getLeafNodesNoRecursion(root)+"个叶子节点数"); System.out.println(contains(root,E)); System.out.println(contains(root,P)); System.out.println("当前二叉树的高度为:"+height(root)); System.out.println("当前二叉树第3层的节点个数为:"+getKLevelNodes(root,3)); }}如上main引用结果如下:

  

 

  到此这篇关于Java实现二叉树的示例代码(递归&迭代)的文章就介绍到这了,更多相关Java二叉树内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: