节点类 Node.java
package com.shundong.btree; /** * 链式二叉树节点 * @author shundong106 * */ public class Node { Object value; Node leftChild; Node rightChild; public Node(int value) { super(); this.value = value; } public Node(int value, Node leftChild, Node rightChild) { super(); this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } }
方法一 递归
package com.shundong.btree; /** * 方法1 递归 * @author shundong106 * */ public class TraversalOfBinaryTree { private Node root;//根节点 public TraversalOfBinaryTree() { super(); // TODO Auto-generated constructor stub } public TraversalOfBinaryTree(Node root) { super(); this.root = root; } //二叉树先序遍历 public void preorderTraversal() { System.out.print("先根遍历"); this.preorderTraversal(root); System.out.println(); } //先序遍历的辅助函数 private void preorderTraversal(Node root) { if(root !=null) { //输出根 System.out.print(root.value+" "); //遍历根的 左子树 this.preorderTraversal(root.leftChild); //遍历根的 右子树 this.preorderTraversal(root.rightChild); } } //二叉树中序遍历 public void inOrderTraversal() { System.out.print("中序遍历"); this.inOrderTraversal(root); System.out.println(); } private void inOrderTraversal(Node root) { if(root !=null) { //先输出左子树 this.inOrderTraversal(root.leftChild); //输出根 System.out.print(root.value+" "); //再输出右子树 this.inOrderTraversal(root.rightChild); } } //二叉树的后根遍历 public void postOrderTraversal() { System.out.print("后根遍历"); this.postOrderTraversal(root); System.out.println(); } private void postOrderTraversal(Node root) { if(root !=null) { //先遍历左子树 this.postOrderTraversal(root.leftChild); //在输出右子树 this.postOrderTraversal(root.rightChild); //再输出根 System.out.print(root.value+" "); } } }
方法二 非递归(栈)
package com.shundong.btree; import java.util.Deque; import java.util.LinkedList; /** * !递归 * @author shundong106 * */ public class TraversalOfBinaryTree2 { private Node root;//根节点 public TraversalOfBinaryTree2() { super(); // TODO Auto-generated constructor stub } public TraversalOfBinaryTree2(Node root) { super(); this.root = root; } //二叉树先序遍历 非递归 public void preOrderTraversal() { System.out.print("先根遍历"); this.preOrderTraversal(root); System.out.println(); } //先序遍历的辅助函数 private void preOrderTraversal(Node root) { //创建一个栈 Deque<Node> stack = new LinkedList<Node>(); while (root != null || !stack.isEmpty()) { while (root != null) { //先直接输出 根节点 System.out.print(root.value+" "); stack.push(root);//将根节点入栈 root = root.leftChild;//将当前根节点的做孩子 当做新的 根节点 } if (!stack.isEmpty()) { root = stack.pop(); root = root.rightChild; } } } //二叉树中序遍历递归 public void inOrderTraversal() { System.out.print("中序遍历"); this.inOrderTraversal(root); System.out.println(); } private void inOrderTraversal(Node root) { //创建一个栈 Deque<Node> stack = new LinkedList<Node>(); //将根节点 赋值 Node current = root ; while(current !=null || !stack.isEmpty()) { while(current !=null) { stack.push(current);//让根进栈 current = current.leftChild;//然后再遍历左子树 只有到达二叉树的左叶子 才会终止循环 } //此刻若栈不为空 那么栈顶必定是 根节点 if(!stack.isEmpty()) { current = stack.pop();//输出当前的栈顶 也就是 左叶子节点 System.out.print(current.value+" "); current = current.rightChild;//然后此刻开始遍历右子树 } } } //二叉树的后根遍历 public void postOrderTraversal() { System.out.print("后根遍历"); this.postOrderTraversal(root); System.out.println(); } private void postOrderTraversal(Node root) { Deque<Node> stack = new LinkedList<Node>(); Deque<Integer> stack2 = new LinkedList<Integer>(); Integer i = new Integer(1); while (root != null || !stack.isEmpty()) { while (root != null) { stack.push(root); stack2.push(new Integer(0)); root = root.leftChild; } while (!stack.isEmpty() && stack2.peek().equals(i)) { stack2.pop(); System.out.print(stack.pop().value +" "); } if (!stack.isEmpty()) { stack2.pop(); stack2.push(new Integer(1)); root = stack.peek(); root = root.rightChild; } } } }
测试类 Test.java
package com.shundong.btree; /** * 二叉树遍历的测试类 * @author shundong * */ public class Test { public static void main(String[] args) { Node node5 = new Node(5,null,null); Node node4 = new Node(4,null,node5); Node node7 = new Node(7,null,null); Node node6 = new Node(6,null,node7); Node node3 = new Node(3,null,null); Node node2 = new Node(2, node3, node6); Node node1 = new Node(1, node4, node2);//根节点 TraversalOfBinaryTree btree = new TraversalOfBinaryTree(node1); //先根遍历 btree.preorderTraversal(); //中根遍历 btree.inOrderTraversal(); //后根遍历 btree.postOrderTraversal(); System.out.println("-------------------------非递归——————————————————————"); TraversalOfBinaryTree2 btree2 = new TraversalOfBinaryTree2(node1); btree2.preOrderTraversal(); btree2.inOrderTraversal(); btree2.postOrderTraversal(); } }