参考资料:
http://blog.csdn.net/wuwenxiang91322/article/details/12231657
环境:
Java: jdk1.8.0_91
import java.util.Stack; public class BinaryTreeTest { /** * 树结构如下: * root * / \ * A B * / \ \ * C D G * / / / \ * E F H I * */ public static void main(String[] args) { Node root = new Node("root"); Node A = new Node("A"), B = new Node("B"), C = new Node("C"), D = new Node("D"), E = new Node("E"), F = new Node("F"), G = new Node("G"), H = new Node("H"), I = new Node("I"); root.setLeft(A); A.setLeft(C); A.setRight(D); C.setLeft(E); D.setLeft(F); root.setRight(B); B.setRight(G); G.setLeft(H); G.setRight(I); BinaryTreeTraversalor tree = BinaryTreeTraversalor.PRE_ORDER; System.out.print(String.format("%s->", tree.getStrategy())); tree.execute(root); tree = BinaryTreeTraversalor.IN_ORDER; System.out.print(String.format("\n%s->", tree.getStrategy())); tree.execute(root); tree = BinaryTreeTraversalor.POST_ORDER; System.out.print(String.format("\n%s->", tree.getStrategy())); tree.execute(root); tree = BinaryTreeTraversalor.PRE_ORDER_NORECU; System.out.print(String.format("\n%s->", tree.getStrategy())); tree.execute(root); tree = BinaryTreeTraversalor.IN_ORDER_NORECU; System.out.print(String.format("\n%s->", tree.getStrategy())); tree.execute(root); tree = BinaryTreeTraversalor.POST_ORDER_NORECU; System.out.print(String.format("\n%s->", tree.getStrategy())); tree.execute(root); } } class Node { private String value; private Node left; private Node right; public Node(String value) { this.value = value; } public String getValue() { return value; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } } enum BinaryTreeTraversalor { IN_ORDER("递归中序遍历") { @Override public void execute(Node node) { if (node != null) { execute(node.getLeft()); visit(node); execute(node.getRight()); } } }, PRE_ORDER("递归先序遍历") { @Override public void execute(Node node) { if (node != null) { visit(node); execute(node.getLeft()); execute(node.getRight()); } } }, POST_ORDER("递归后序遍历") { @Override public void execute(Node node) { if (node != null) { execute(node.getLeft()); execute(node.getRight()); visit(node); } } }, IN_ORDER_NORECU("非递归中序遍历") { @Override public void execute(Node node) { final Stack<Node> stack = new Stack<>(); while (node != null || !stack.isEmpty()) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { node = stack.pop(); visit(node); node = node.getRight(); } } } }, PRE_ORDER_NORECU("非递归先序遍历") { @Override public void execute(Node node) { final Stack<Node> stack = new Stack<>(); while (node != null || !stack.isEmpty()) { while (node != null) { visit(node); stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { node = stack.pop(); node = node.getRight(); } } } }, POST_ORDER_NORECU("非递归后序遍历") {// 需要重点理解 @Override public void execute(Node node) { final Stack<Node> stack = new Stack<>(); Node tmp = null; while (node != null) { while (node.getLeft() != null) { stack.push(node); node = node.getLeft(); } while (node != null && (node.getRight() == null || node.getRight() == tmp)) {// 当前结点无右子树或右子树已经输出 visit(node); // 记录上一个已输出结点 tmp = node; if (stack.empty()) return; node = stack.pop(); } stack.push(node); node = node.getRight(); } } }; private static void visit(Node root) { System.out.print(root.getValue() + "\t"); } private String strategy; BinaryTreeTraversalor(String strategy) { this.strategy = strategy; } public abstract void execute(Node node); public String getStrategy() { return strategy; } }