二叉树的递归遍历和非递归遍历(附详细例子)
来源:互联网 发布:mac 办公软件 免费 编辑:程序博客网 时间:2024/06/04 18:42
二叉树的递归遍历和非递归遍历(附详细例子)
二叉树的遍历主要有递归实现和非递归实现,递归实现比较好理解,非递归实现主要是利用了栈的思想,后进先出,本文实现二叉树的非递归遍历主要是用了LinkedList可以当做栈使用的功能。具体例子如下:
package com.sheepmu; import java.util.LinkedList; public class BinaryTree{ private TreeNode root;// 根节点 public BinaryTree() { } public BinaryTree(TreeNode root) { this.root = root; } public TreeNode getRoot() { return root; } public void setRoot(TreeNode root) { this.root = root; } /** * 节点类 */ private static class TreeNode { private String data = null;// 数据部分 private TreeNode left;// 左节点的引用 private TreeNode right;// 右节点的引用 public TreeNode(String data, TreeNode left, TreeNode right)//节点的构造函数,测试函数中创建节点就是用了它 { this.data = data; this.left = left; this.right = right; } public String getData()//节点类的get和set方法 { return data; } public void setData(String data) { this.data = data; } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } } /** * 递归 前续遍历 */ public void preOrder(TreeNode node) { if (node != null) { System.out.print(node.getData()+" "); preOrder(node.getLeft()); preOrder(node.getRight()); } } /** * 递归 中续遍历 */ public void inOrder(TreeNode node) { if (node != null) { inOrder(node.getLeft()); System.out.print(node.getData()+" "); inOrder(node.getRight()); } } /** * 递归 后续遍历 */ public void postOrder(TreeNode node) { if (node != null) { postOrder(node.getLeft()); postOrder(node.getRight()); System.out.print(node.getData()+" "); } } /** * 非递归 前续遍历 */ public void preOrderNoRecursion() { LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); stack.push(root); TreeNode current = null; while (!stack.isEmpty()) { current = stack.pop(); System.out.print(current.data+" "); if (current.getRight() != null) stack.push(current.getRight()); if (current.getLeft() != null) stack.push(current.getLeft()); } System.out.println(); } /** * 非递归 中续遍历 */ public void inorderNoRecursion() { LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode current = root; while (current != null || !stack.isEmpty()) { while(current != null) { stack.push(current); current = current.getLeft(); } if (!stack.isEmpty()) { current = stack.pop(); System.out.print(current.data+" "); current = current.getRight(); } } System.out.println(); } /** * 非递归 后续遍历 */ public void postorderNoRecursion() { TreeNode rNode = null; TreeNode current = root; LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); while(current != null || !stack.isEmpty()) { while(current != null) { stack.push(current); current = current.getLeft(); } current = stack.pop(); while (current != null && (current.getRight() == null ||current.getRight() == rNode)) { System.out.print(current.data+" "); rNode = current; if (stack.isEmpty()) { System.out.println(); return; } current = stack.pop(); } stack.push(current); current = current.getRight(); } } public static void main(String[] args) { TreeNode l2 = new TreeNode("E", null, null);//这五行构造一棵二叉树 TreeNode r2 = new TreeNode("D", null, null); TreeNode l1 = new TreeNode("B",null, r2);// 根节点左子树 TreeNode r1 = new TreeNode("C", l2, null);// 根节点右子树 TreeNode root = new TreeNode("A", l1, r1);// 创建根节点 BinaryTree bt = new BinaryTree(root); System.out.print(" 递归 前序遍历------->"); bt.preOrder(bt.getRoot()); System.out.print(" 非递归 前序遍历------->"); bt.preorderNoRecursion(); System.out.print(" 递归 中序遍历------->"); bt.inOrder(bt.getRoot()); System.out.print(" 非递归 中序遍历------->"); bt.inorderNoRecursion(); System.out.print(" 递归 后序遍历------->"); bt.postOrder(bt.getRoot()); System.out.print(" 非递归 后序遍历------->"); bt.postorderNoRecursion(); }}递归 前序遍历------->A B D C E 非递归 前序遍历------->A B D C E
递归 中序遍历------->B D A E C 非递归 中序遍历------->B D A E C
递归 后序遍历------->D B E C A 非递归 后序遍历------->D B E C A
1 0
- 二叉树的递归遍历和非递归遍历(附详细例子)
- 二叉树的递归遍历和非递归遍历(附详细例子)
- 二叉树的递归遍历和非递归遍历
- 二叉树的递归遍历和非递归遍历
- 二叉搜索树的递归遍历和非递归遍历
- java 二叉树的递归遍历和非递归遍历
- 二叉树的遍历(递归和非递归)
- 二叉树的遍历(递归和非递归)
- 二叉树的建立和遍历(递归、非递归)
- 二叉树的遍历(非递归和递归实现)
- 二叉树的先序中序后序遍历 (递归和非递归)
- 二叉树的遍历算法(递归和非递归)
- 二叉树的递归和非递归遍历(java)
- 二叉树的遍历(递归+非递归+层次遍历)
- 递归 和 非递归 遍历二叉树
- 递归和非递归遍历二叉树
- 递归和非递归遍历二叉树
- 二叉树递归和非递归遍历
- 快速判断一个数是否是2的幂次方c++
- redhat环境下ssh不能自动认证登录的解决方法
- icot1
- 第六章堆排序之“优先级队列实现先进先出队列和栈”(练习6.5-6)
- Ubuntu Linux下安装OpenCV2.4.1所需包
- 二叉树的递归遍历和非递归遍历(附详细例子)
- 性能优化注意事项
- poj 3616
- android listview 图文并茂
- iscsi使用命令行
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- 算法竞赛入门经典 第六章层次遍历
- 阿拉伯数字 转换为 汉字数字 算法实现
- 带你读懂8259A中断控制器