Java 二叉樹查找算法代碼


  1. /** 二叉樹節點 */   
  2. public class BTNode {   
  3.   private char key;   
  4.   private BTNode left, right;   
  5.   public BTNode(char key) {   
  6.     this(key, null, null);   
  7.   }   
  8.   public BTNode(char key, BTNode left, BTNode right) {   
  9.     this.key = key;   
  10.     this.left = left;   
  11.     this.right = right;   
  12.   }   
  13.   public char getKey() {   
  14.     return key;   
  15.   }   
  16.   public void setKey(char key) {   
  17.     this.key = key;   
  18.   }   
  19.   public BTNode getLeft() {   
  20.     return left;   
  21.   }   
  22.   public void setLeft(BTNode left) {   
  23.     this.left = left;   
  24.   }   
  25.   public BTNode getRight() {   
  26.     return right;   
  27.   }   
  28.   public void setRight(BTNode right) {   
  29.     this.right = right;   
  30.   }   
  31. }   
  32.   
  33.   
  34.   
  35. /** 二叉樹遍歷 */   
  36. public class BinTree {   
  37.   protected BTNode root;   
  38.   public BinTree(BTNode root) {   
  39.     this.root = root;   
  40.   }   
  41.   public BTNode getRoot() {   
  42.     return root;   
  43.   }   
  44.   /** 構造樹 */   
  45.   public static BTNode init() {   
  46.     BTNode a = new BTNode('A');   
  47.     BTNode b = new BTNode('B', null, a);   
  48.     BTNode c = new BTNode('C');   
  49.     BTNode d = new BTNode('D', b, c);   
  50.     BTNode e = new BTNode('E');   
  51.     BTNode f = new BTNode('F', e, null);   
  52.     BTNode g = new BTNode('G', null, f);   
  53.     BTNode h = new BTNode('H', d, g);   
  54.     return h;// root   
  55.   }   
  56.   /** 訪問節點 */   
  57.   public static void visit(BTNode p) {   
  58.     System.out.print(p.getKey() + " ");   
  59.   }   
  60.   /** 遞歸實現前序遍歷 */   
  61.   protected static void preorder(BTNode p) {   
  62.     if (p != null) {   
  63.       visit(p);   
  64.       preorder(p.getLeft());   
  65.       preorder(p.getRight());   
  66.     }   
  67.   }   
  68.   /** 遞歸實現中序遍歷 */   
  69.   protected static void inorder(BTNode p) {   
  70.     if (p != null) {   
  71.       inorder(p.getLeft());   
  72.       visit(p);   
  73.       inorder(p.getRight());   
  74.     }   
  75.   }   
  76.   /** 遞歸實現後序遍歷 */   
  77.   protected static void postorder(BTNode p) {   
  78.     if (p != null) {   
  79.       postorder(p.getLeft());   
  80.       postorder(p.getRight());   
  81.       visit(p);   
  82.     }   
  83.   }   
  84.   /** 非遞歸實現前序遍歷 */   
  85.   protected static void iterativePreorder(BTNode p) {   
  86.     Stack<BTNode> stack = new Stack<BTNode>();   
  87.     if (p != null) {   
  88.       stack.push(p);   
  89.       while (!stack.empty()) {   
  90.         p = stack.pop();   
  91.         visit(p);   
  92.         if (p.getRight() != null)   
  93.           stack.push(p.getRight());   
  94.         if (p.getLeft() != null)   
  95.           stack.push(p.getLeft());   
  96.       }   
  97.     }   
  98.   }   
  99.   /** 非遞歸實現後序遍歷 */   
  100.   protected static void iterativePostorder(BTNode p) {   
  101.     BTNode q = p;   
  102.     Stack<BTNode> stack = new Stack<BTNode>();   
  103.     while (p != null) {   
  104.       // 左子樹入棧   
  105.       for (; p.getLeft() != null; p = p.getLeft())   
  106.         stack.push(p);   
  107.       // 當前節點無右子或右子已經輸出   
  108.       while (p != null && (p.getRight() == null || p.getRight() == q)) {   
  109.         visit(p);   
  110.         q = p;// 記錄上一個已輸出節點   
  111.         if (stack.empty())   
  112.           return;   
  113.         p = stack.pop();   
  114.       }   
  115.       // 處理右子   
  116.       stack.push(p);   
  117.       p = p.getRight();   
  118.     }   
  119.   }   
  120.   /** 非遞歸實現中序遍歷 */   
  121.   protected static void iterativeInorder(BTNode p) {   
  122.     Stack<BTNode> stack = new Stack<BTNode>();   
  123.     while (p != null) {   
  124.       while (p != null) {   
  125.         if (p.getRight() != null)   
  126.           stack.push(p.getRight());// 當前節點右子入棧   
  127.         stack.push(p);// 當前節點入棧   
  128.         p = p.getLeft();   
  129.       }   
  130.       p = stack.pop();   
  131.       while (!stack.empty() && p.getRight() == null) {   
  132.         visit(p);   
  133.         p = stack.pop();   
  134.       }   
  135.       visit(p);   
  136.       if (!stack.empty())   
  137.         p = stack.pop();   
  138.       else   
  139.         p = null;   
  140.     }   
  141.   }   
  142.   public static void main(String[] args) {   
  143.     BinTree tree = new BinTree(init());   
  144.     System.out.print(" Pre-Order:");   
  145.     preorder(tree.getRoot());   
  146.     System.out.println();   
  147.     System.out.print(" In-Order:");   
  148.     inorder(tree.getRoot());   
  149.     System.out.println();   
  150.     System.out.print("Post-Order:");   
  151.     postorder(tree.getRoot());   
  152.     System.out.println();   
  153.     System.out.print(" Pre-Order:");   
  154.     iterativePreorder(tree.getRoot());   
  155.     System.out.println();   
  156.     System.out.print(" In-Order:");   
  157.     iterativeInorder(tree.getRoot());   
  158.     System.out.println();   
  159.     System.out.print("Post-Order:");   
  160.     iterativePostorder(tree.getRoot());   
  161.     System.out.println();   
  162.   }   
  163. }   
發佈了29 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章