package com.shiyeqiang.tree; import java.util.Stack; public class BiTree { public static void main(String[] args) { // 首先构造叶子节点 BiTree leafA1 = new BiTree(4); BiTree leafA2 = new BiTree(5); BiTree leafB1 = new BiTree(6); BiTree leafB2 = new BiTree(7); // 构建二叉树的结构 BiTree treeA = new BiTree(2, leafA1, leafA2); BiTree treeB = new BiTree(3, leafB1, leafB2); BiTree tree = new BiTree(1, treeA, treeB); System.out.println("递归前序遍历二叉树结果: "); preOrder(tree); System.out.println(); System.out.println("非递归前序遍历二叉树结果: "); iterativePreOrder(tree); System.out.println(); System.out.println("非递归中序遍历二叉树结果: "); iterativeInOrder(tree); System.out.println(); System.out.println("递归后续遍历二叉树结果: "); iterativePostOrder(tree); } private BiTree leftTree; private BiTree rightTree; private Object data; public BiTree() { } public BiTree(Object data) { this.data = data; } public BiTree(Object data, BiTree leftTree, BiTree rightTree) { this.data = data; this.leftTree = leftTree; this.rightTree = rightTree; } public static void visit(Object data) { // 访问二叉树的数据 System.out.print(data + " "); } public static void preOrder(BiTree tree) { // 递归实现前序遍历二叉树 if (tree != null) { visit(tree.data); if (tree.leftTree != null) preOrder(tree.leftTree); if (tree.rightTree != null) preOrder(tree.rightTree); } } public static void inOrder(BiTree tree) { // 递归实现中序遍历二叉树(左-根--右边) if (tree != null) { if (tree.leftTree != null) inOrder(tree.leftTree); visit(tree.data); if (tree.rightTree != null) inOrder(tree.rightTree); } } public static void postOrder(BiTree tree) { // 递归实现后序遍历二叉树 if (tree != null) { if (tree.leftTree != null) postOrder(tree.leftTree); if (tree.rightTree != null) postOrder(tree.rightTree); visit(tree.data); } } // 非递归实现前序遍历,根----左子树---右子树 public static void iterativePreOrder(BiTree tree) { Stack<BiTree> stack = new Stack<BiTree>(); if (tree != null) { stack.push(tree); while (!stack.isEmpty()) { tree = stack.pop(); visit(tree.data); if (tree.rightTree != null) stack.push(tree.rightTree); // 首先必须是右子树入栈, if (tree.leftTree != null) stack.push(tree.leftTree); } } } // 非递归实现中序遍历,左---根---右 public static void iterativeInOrder(BiTree tree) { Stack<BiTree> stack = new Stack<BiTree>(); // 主要是根据入栈以及出栈的思想实现的 //以一个1,2,3,4,5,6,7的满二叉树为例:中序遍历如下: //右子树3入栈,根节点1入栈,右子树5入栈,根节点2入栈,根节点4入栈。 //左子树4出栈,根节点2出栈,5出战,按照上述规则重新遍历右子树5 while (tree != null) { while (tree != null) { if (tree.rightTree != null) stack.push(tree.rightTree);// 当前节点右子入栈 stack.push(tree);// 当前节点入栈 tree = tree.leftTree; } tree = stack.pop(); // 下面为访问的是叶子节点 while (!stack.empty() && tree.rightTree == null) { visit(tree.data); tree = stack.pop(); } visit(tree.data); if (!stack.empty()) { tree = stack.pop(); // 下一次遍历,其实在这个时候遍历的是右子树把 } else { tree = null; } } } // 非递归实现后序遍历 public static void iterativePostOrder(BiTree tree) { BiTree tempTree = tree; Stack<BiTree> stack = new Stack<BiTree>(); while (tree != null) { // 左子树入栈 for (; tree.leftTree != null; tree = tree.leftTree) stack.push(tree); // 当前节点无右子或右子已经输出 while (tree != null && (tree.rightTree == null || tree.rightTree == tempTree)) { visit(tree.data); tempTree = tree;// 记录上一个已输出节点 if (stack.empty()) return; tree = stack.pop(); //一般处理根节点的时候均是先出战 ,然后再入栈 } // 处理右子 stack.push(tree); tree = tree.rightTree; } } }
二叉树三种遍历(递归以及非递归实现),布布扣,bubuko.com
原文地址:http://blog.csdn.net/shiyeqiangldh/article/details/28246939