前序遍历:
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); } }