前序遍歷:
1.訪問根節點
2.前序遍歷左子樹
3.前序遍歷右子樹
中序遍歷:
1.中序遍歷左子樹
2.訪問根節點
3.中序遍歷右子樹
后序遍歷:
1.后序遍歷左子樹
2.后序遍歷右子樹
3.訪問根節點
---------------------
package design; import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack; public class BinTree { char data; BinTree leftChild; BinTree rightChild; public BinTree(char c) { data = c; } public static void preSearch(BinTree root){ if(root !=null){ System.out.print(root.data); preSearch(root.leftChild); preSearch(root.rightChild); } } public static void midSearch(BinTree root){ if(root !=null){ midSearch(root.leftChild); System.out.print(root.data); midSearch(root.rightChild); }else{ return; } } public static void postSearch(BinTree root){ if(root !=null){ postSearch(root.leftChild); postSearch(root.rightChild); System.out.print(root.data); } } // 先序遍歷非遞歸 public static void preOrder(BinTree root){ Stack<BinTree> s = new Stack<BinTree>(); while(root !=null || !s.empty()){ while(root!=null){ System.out.print(root.data); s.push(root); root = root.leftChild; } if(!s.empty()){ root = s.pop(); root = root.rightChild; } } } // 中序遍歷非遞歸 public static void midOrder(BinTree root){ Stack<BinTree> s = new Stack<BinTree>(); while(root!=null || !s.empty()){ while(root!=null){ s.push(root); root = root.leftChild; } if(!s.empty()){ root =s.pop(); System.out.print(root.data); root = root.rightChild; } } } // 后序遍歷非遞歸 public static void postOrder(BinTree root){ Stack<BinTree> s = new Stack<BinTree>(); Stack<Integer> s2 = new Stack<Integer>(); Integer i = new Integer(1); while(root!=null || !s.empty()){ while(root!=null){ s.push(root); s2.push(new Integer(0)); root = root.leftChild; } while(!s.empty() && s2.peek().equals(i)){ s2.pop(); System.out.print(s.pop().data); } if(!s.empty()){ s2.pop(); s2.push(new Integer(1)); root =s.peek(); root = root.rightChild; } } } //計算二叉樹的深度 public static int level(BinTree root){ if(root == null){ return 0; } return level(root.leftChild)+1>level(root.rightChild)+1?level(root.leftChild)+1:level(root.rightChild)+1; } //層序遍歷二叉樹 public static void levelTrav(BinTree root) { if (root == null) return; Queue<BinTree> q = new ArrayDeque<BinTree>(); q.add(root); BinTree cur; while (!q.isEmpty()) { cur = q.peek(); System.out.print(cur.data + " "); if (cur.leftChild != null) q.add(cur.leftChild); if (cur.rightChild != null) q.add(cur.rightChild); q.poll(); } } public static void main(String[] args) { BinTree b1 = new BinTree('a'); BinTree b2 = new BinTree('b'); BinTree b3 = new BinTree('c'); BinTree b4 = new BinTree('d'); BinTree b5 = new BinTree('e'); /** * a * / \ * b c * / \ * d e */ b1.leftChild = b2; b1.rightChild = b3; b2.leftChild = b4; b2.rightChild = b5; BinTree.preSearch(b1); System.out.println(); BinTree.preOrder(b1); System.out.println("========================"); BinTree.midSearch(b1); System.out.println(""); BinTree.midOrder(b1); System.out.println("========================"); BinTree.postSearch(b1); System.out.println(); BinTree.postOrder(b1); System.out.println("========================"); System.out.println(BinTree.level(b1)); System.out.println("========================"); BinTree.levelTrav(b1); } }