二叉查找樹

二叉查找樹是二叉樹的一種,它的特徵是:全部節點的左子樹小於節點,全部節點的右子樹大於節點,這樣便於查找.算法

下面是二叉查找樹的具體實現數據結構

BinarySearchTree類架構

 

 1 /*
 2  * 簡化的二叉查找樹,節點只是int類型
 3  */
 4 public class BinarySearchTree {
 5     private static class BinaryNode {//private聲明的類只能定義爲內部類 
 6     //定義樹的節點類型
 7     }
 8     
 9     private BinaryNode root; //根節點,定義爲私有
10     
11     public BinarySearchTree() {
12         root = null;
13     }
14     
15     public boolean isEmpty() { //判斷樹是否爲空
16         return (root == null);
17     }
18     
19     public void makeEmpty() { //把樹置爲空
20         root = null;
21     }
22     //查找
23     public boolean contains(int x) {
24         return contains(x, root);
25     }
26     //最小值
27     public int findMin() {
28         return findMin(root).element;
29     }
30     //最大值
31     public int findMax() {
32         return findMax(root).element;
33     }
34     //插入
35     public void insert(int x) {
36         root = insert(x, root);
37     }
38     //刪除
39     public void remove(int x) {
40         root = remove(x, root);
41     }
43 private boolean contains(int x, BinaryNode t) { 44 //查找實現 45 } 48 private BinaryNode findMin(BinaryNode t) { 49 //最小值實現(遞歸) 50 } 51 52 53 private BinaryNode findMax(BinaryNode t) { 54 //最大值實現(非遞歸) 55 } 58 private BinaryNode insert(int x, BinaryNode t) { 59 //插入實現 60 } 63 private BinaryNode remove(int x, BinaryNode t) { 64 //刪除實現 65 } 66 }

 

定義樹的節點類型

 1     private static class BinaryNode { //private聲明的類只能定義爲內部類
 2         public BinaryNode(int ele) {
 3             this(ele, null, null); //引用構造函數
 4         }
 5         public BinaryNode(int ele, BinaryNode lt, BinaryNode rt) {
 6             element = ele;
 7             left = lt;
 8             right = rt;
 9         }
10         
11         int element; //先定義爲一個簡單的整型數據
12         BinaryNode left; //左孩子
13         BinaryNode right; //右孩子        
14     }

 實現查找算法

 1     //查找
 2     private boolean contains(int x, BinaryNode t) {
 3         if(t == null) {
 4             return false;
 5         }
 6         
 7         if(x > t.element) {
 8             return contains(x, t.right);
 9         }
10         else if(x < t.element) {
11             return contains(x, t.left);
12         } else {
13             return true;
14         }
15     }

實現最小值算法(遞歸)

 1     //最大值(遞歸)
 2     private BinaryNode findMin(BinaryNode t) { 
 3         if(t == null) {
 4             return null;
 5         }
 6         else if(t.left == null) {
 7             return t;
 8         }
 9         return findMin(t);
10     }

實現最大值算法(非遞歸)

1     //最大值(非遞歸)
2     private BinaryNode findMax(BinaryNode t) { 
3         if(t != null) {
4             while(t.right != null) {
5                 t = t.right;
6             }
7         }
8         return t;    
9     } 

實現插入算法

 1     //插入
 2     private BinaryNode insert(int x, BinaryNode t) { 
 3         if(t == null) {
 4             return new BinaryNode(x, null, null);
 5         }
 6         if(x < t.element) {
 7             t.left = insert(x, t.left);
 8         }
 9         else if(x > t.element) {
10             t.right = insert(x, t.right);
11         } 
12         return t;        
13     } 

實現刪除算法

 1     //刪除
 2     private BinaryNode remove(int x, BinaryNode t) { 
 3         if(t == null) {
 4             return t;
 5         }
 6         if(x < t.element) {
 7             t.left = remove(x, t.left);
 8         }
 9         else if(x > t.element) {
10             t.right = remove(x, t.right);
11         }
12         //x和t.element相等的狀況
13         else if(t.left != null && t.right != null) { //有左右孩子的狀況
14             t.element = findMin(t.right).element;
15             t.right = remove(t.element, t.right);
16         }
17         else { //有左孩子的狀況
18             t = (t.left != null) ? t.left : t.right;
19         }
20         return t;
21     }

 實現的過程的中用到了大量的遞歸,遞歸的關鍵是想到結束遞歸的條件是什麼.架構

參考

數據結構與算法分析_Java語言描述(第二版)函數

相關文章
相關標籤/搜索