【算法】二叉樹

 

堆類似,二叉樹也是一種很奇特的數據結構。它包含了根節點,節點最多隻有一個左右節點。

父節點和左右子節點之間有一定的關係:
1. 父節點比左節點大(小)。
2. 父節點比右節點小(大)。

通過這種特性,二叉樹的查找定位非常方便,比數組、鏈表的查找效率要高很多。在我的機器上,從100萬個隨機整數中查找一個整數平均需要0.00386毫秒。可見效率確實很高。

不過,二次樹有一個致命的缺點:如果插入的數是經過排序的,則會使二次樹高度非常大,就等同於線性數組了,從而極大影響了查找和插入的效率。

下面是二叉樹的Java實現代碼。

  1. package cn.tenyears.demo;
  2. import java.util.Comparator;
  3. /**
  4.  * Binary Tree
  5.  * 
  6.  * @author taolue
  7.  * 
  8.  */
  9. public class BinTree<T> {
  10.   public class Node {
  11.     private T data;
  12.     private Node left;
  13.     private Node right;
  14.     public void setData(T data) {
  15.       this.data = data;
  16.     }
  17.     public void setLeft(Node left) {
  18.       this.left = left;
  19.     }
  20.     public void setRight(Node right) {
  21.       this.right = right;
  22.     }
  23.     public Node getLeft() {
  24.       return this.left;
  25.     }
  26.     public Node getRight() {
  27.       return this.right;
  28.     }
  29.     public T getData() {
  30.       return this.data;
  31.     }
  32.     public Node(T data, Node l, Node r) {
  33.       this.data = data;
  34.       this.left = l;
  35.       this.right = r;
  36.     }
  37.   }
  38.   class ParentAndChild {
  39.     public Node parent = null;
  40.     public Node child = null;
  41.     public ParentAndChild(Node p, Node c) {
  42.       this.parent = p;
  43.       this.child = c;
  44.     }
  45.   }
  46.   private Comparator<T> comparator = null;;
  47.   public final ParentAndChild NONE_PC = new ParentAndChild(nullnull);
  48.   private Node root = null;
  49.   private int treeSize = 0;
  50.   public BinTree(Comparator<T> comparator) {
  51.     this.comparator = comparator;
  52.   }
  53.   public Node search(T data) {
  54.     return searchPC(data).child;
  55.   }
  56.   public void insert(T data) {
  57.     Node temp = root;
  58.     Node parent = null;
  59.     int equal = 0;
  60.     while (temp != null) {
  61.       parent = temp;
  62.       equal = comparator.compare(data, temp.getData());
  63.       if (equal < 0)
  64.         temp = temp.getLeft();
  65.       else
  66.         temp = temp.getRight();
  67.     }
  68.     Node one = new Node(data, nullnull);
  69.     if (parent == null)
  70.       root = one;
  71.     else if (comparator.compare(data, parent.getData()) < 0)
  72.       parent.setLeft(one);
  73.     else
  74.       parent.setRight(one);
  75.     this.treeSize++;
  76.   }
  77.   public boolean delete(T data) {
  78.     Node deleted = null;
  79.     Node parent = null;
  80.     Node right = null;
  81.     ParentAndChild pc = this.searchPC(data);
  82.     deleted = pc.child;
  83.     parent = pc.parent;
  84.     if (deleted == null)
  85.       return false;
  86.     if (deleted.getRight() == null)
  87.       right = deleted.getLeft();
  88.     else if (deleted.getLeft() == null)
  89.       right = deleted.getRight();
  90.     else {
  91.       Node parent1 = deleted;
  92.       right = deleted.getLeft();
  93.       while (right.getRight() != null) {
  94.         parent1 = right;
  95.         right = right.getRight();
  96.       }
  97.       if (parent1 == deleted)
  98.         right.setRight(deleted.getRight());
  99.       else {
  100.         parent1.setRight(right.getLeft());
  101.         right.setLeft(deleted.getLeft());
  102.         right.setRight(deleted.getRight());
  103.       }
  104.     }
  105.     if (parent == null)
  106.       root = right;
  107.     else if (comparator.compare(deleted.getData(), parent.getData()) < 0)
  108.       parent.setLeft(right);
  109.     else
  110.       parent.setRight(right);
  111.     this.treeSize--;
  112.     return true;
  113.   }
  114.   public int getSize() {
  115.     return this.treeSize;
  116.   }
  117.   private ParentAndChild searchPC(T data) {
  118.     Node temp = root;
  119.     Node parent = null;
  120.     int equal = 0;
  121.     while (temp != null) {
  122.       equal = comparator.compare(data, temp.getData());
  123.       if (equal == 0)
  124.         break;
  125.       else {
  126.         parent = temp;
  127.         if (equal < 0)
  128.           temp = parent.getLeft();
  129.         else
  130.           temp = parent.getRight();
  131.       }
  132.     }
  133.     if (temp != null)
  134.       return new ParentAndChild(parent, temp);
  135.     return this.NONE_PC;
  136.   }
  137.   public static void main(String[] args) {
  138.     Comparator<Integer> com = new Comparator<Integer>() {
  139.       public int compare(Integer o1, Integer o2) {
  140.         return o1.compareTo(o2);
  141.       }
  142.     };
  143.     BinTree<Integer> tree = new BinTree<Integer>(com);
  144.     int size = 1000000;
  145.     Integer[] a = new Integer[size];
  146.     for (int i = 0; i < size; i++) {
  147.       a[i] = Integer.valueOf((int) Math.round(Math.random() * size));
  148.       tree.insert(a[i]);
  149.     }
  150.     long start = System.currentTimeMillis();
  151.     // find
  152.     for (int i = 0; i < size; i++) {
  153.       if (tree.search(a[i]) == null)
  154.         System.out.println("Error: Find None.");
  155.     }
  156.     long end = System.currentTimeMillis();
  157.     System.out.println("Last(AVG): " + (end - start) * 1.0f / size + " ms");
  158.   }
  159. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章