Java实现二叉树的示例代码(递归&迭代)

二叉树(Binary tree)是树形结构的一个重要类型。本文将利用Java语言实现二叉树,文中的示例代码讲解详细,需要的同学可以参考一下

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

2.本次展示链式存储

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

//基础二叉树实现 //使用左右孩子表示法 import java.util.*; import java.util.Deque; public class myBinTree { private static class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(char val) { this.val = val; } } public static TreeNode build(){ TreeNode nodeA=new TreeNode('A'); TreeNode nodeB=new TreeNode('B'); TreeNode nodeC=new TreeNode('C'); TreeNode nodeD=new TreeNode('D'); TreeNode nodeE=new TreeNode('E'); TreeNode nodeF=new TreeNode('F'); TreeNode nodeG=new TreeNode('G'); TreeNode nodeH=new TreeNode('H'); nodeA.left=nodeB; nodeA.right=nodeC; nodeB.left=nodeD; nodeB.right=nodeE; nodeE.right=nodeH; nodeC.left=nodeF; nodeC.right=nodeG; return nodeA; } //方法1(递归) //先序遍历: 根左右 public static void preOrder(TreeNode root){ if(root==null){ return; } System.out.print(root.val+" "); preOrder(root.left); preOrder(root.right); } //方法1(递归) //中序遍历 public static void inOrder(TreeNode root){ if(root==null){ return; } inOrder(root.left); System.out.print(root.val+" "); inOrder(root.right); } //方法1(递归) //后序遍历 public static void postOrder(TreeNode root){ if(root==null){ return; } postOrder(root.left); postOrder(root.right); System.out.print(root.val+" "); } //方法2(迭代) //先序遍历 (迭代) public static void preOrderNonRecursion(TreeNode root){ if(root==null){ return ; } Deque stack=new LinkedList<>(); stack.push(root); while (!stack.isEmpty()){ TreeNode cur=stack.pop(); System.out.print(cur.val+" "); if(cur.right!=null){ stack.push(cur.right); } if(cur.left!=null){ stack.push(cur.left); } } } //方法2(迭代) //中序遍历 (迭代) public static void inorderTraversalNonRecursion(TreeNode root) { if(root==null){ return ; } Deque 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 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 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 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 queue =new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ int size=queue.size(); for (int i = 0; i 

如上main引用结果如下:

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

以上就是Java实现二叉树的示例代码(递归&迭代)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java