概要
在前面分別介紹了"二叉查找樹的相關理論知識,然后給出了二叉查找樹的C和C++實現版本"。這一章寫一寫二叉查找樹的Java實現版本。
目錄
1. 二叉樹查找樹
2. 二叉查找樹的Java實現
3. 二叉查找樹的Java測試程序
轉載請注明出處:http://www.cnblogs.com/skywang12345/p/3576452.html
更多內容: 數據結構與算法系列 目錄
(01) 二叉查找樹(一)之 圖文解析 和 C語言的實現
(02) 二叉查找樹(二)之 C++的實現
(03) 二叉查找樹(三)之 Java的實現
二叉查找樹簡介
二叉查找樹(Binary Search Tree),又被稱為二叉搜索樹。
它是特殊的二叉樹:對於二叉樹,假設x為二叉樹中的任意一個結點,x節點包含關鍵字key,節點x的key值記為key[x]。如果y是x的左子樹中的一個結點,則key[y] <= key[x];如果y是x的右子樹的一個結點,則key[y] >= key[x]。那么,這棵樹就是二叉查找樹。如下圖所示:
在二叉查找樹中:
(01) 若任意節點的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值;
(02) 任意節點的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值;
(03) 任意節點的左、右子樹也分別為二叉查找樹。
(04) 沒有鍵值相等的節點(no duplicate nodes)。
二叉查找樹的Java實現
1. 二叉查找樹節點的定義
public class BSTree<T extends Comparable<T>> { private BSTNode<T> mRoot; // 根結點 public class BSTNode<T extends Comparable<T>> { T key; // 關鍵字(鍵值) BSTNode<T> left; // 左孩子 BSTNode<T> right; // 右孩子 BSTNode<T> parent; // 父結點 public BSTNode(T key, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right) { this.key = key; this.parent = parent; this.left = left; this.right = right; } } ...... }
BSTree是二叉樹,它保護了二叉樹的根節點mRoot;mRoot是BSTNode類型,而BSTNode是二叉查找樹的節點,它是BSTree的內部類。BSTNode包含二叉查找樹的幾個基本信息:
(01) key -- 它是關鍵字,是用來對二叉查找樹的節點進行排序的。
(02) left -- 它指向當前節點的左孩子。
(03) right -- 它指向當前節點的右孩子。
(04) parent -- 它指向當前節點的父結點。
2 遍歷
這里講解前序遍歷、中序遍歷、后序遍歷3種方式。
2.1 前序遍歷
若二叉樹非空,則執行以下操作:
(01) 訪問根結點;
(02) 先序遍歷左子樹;
(03) 先序遍歷右子樹。
前序遍歷代碼
private void preOrder(BSTNode<T> tree) { if(tree != null) { System.out.print(tree.key+" "); preOrder(tree.left); preOrder(tree.right); } } public void preOrder() { preOrder(mRoot); }
2.2 中序遍歷
若二叉樹非空,則執行以下操作:
(01) 中序遍歷左子樹;
(02) 訪問根結點;
(03) 中序遍歷右子樹。
中序遍歷代碼
private void inOrder(BSTNode<T> tree) { if(tree != null) { inOrder(tree.left); System.out.print(tree.key+" "); inOrder(tree.right); } } public void inOrder() { inOrder(mRoot); }
2.3 后序遍歷
若二叉樹非空,則執行以下操作:
(01) 后序遍歷左子樹;
(02) 后序遍歷右子樹;
(03) 訪問根結點。
后序遍歷代碼
private void postOrder(BSTNode<T> tree) { if(tree != null) { postOrder(tree.left); postOrder(tree.right); System.out.print(tree.key+" "); } } public void postOrder() { postOrder(mRoot); }
看看下面這顆樹的各種遍歷方式:
對於上面的二叉樹而言,
(01) 前序遍歷結果: 3 1 2 5 4 6
(02) 中序遍歷結果: 1 2 3 4 5 6
(03) 后序遍歷結果: 2 1 4 6 5 3
3. 查找
遞歸版本的代碼
/* * (遞歸實現)查找"二叉樹x"中鍵值為key的節點 */ private BSTNode<T> search(BSTNode<T> x, T key) { if (x==null) return x; int cmp = key.compareTo(x.key); if (cmp < 0) return search(x.left, key); else if (cmp > 0) return search(x.right, key); else return x; } public BSTNode<T> search(T key) { return search(mRoot, key); }
非遞歸版本的代碼
/* * (非遞歸實現)查找"二叉樹x"中鍵值為key的節點 */ private BSTNode<T> iterativeSearch(BSTNode<T> x, T key) { while (x!=null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else return x; } return x; } public BSTNode<T> iterativeSearch(T key) { return iterativeSearch(mRoot, key); }
4. 最大值和最小值
查找最大值的代碼
/* * 查找最大結點:返回tree為根結點的二叉樹的最大結點。 */ private BSTNode<T> maximum(BSTNode<T> tree) { if (tree == null) return null; while(tree.right != null) tree = tree.right; return tree; } public T maximum() { BSTNode<T> p = maximum(mRoot); if (p != null) return p.key; return null; }
查找最小值的代碼
/* * 查找最小結點:返回tree為根結點的二叉樹的最小結點。 */ private BSTNode<T> minimum(BSTNode<T> tree) { if (tree == null) return null; while(tree.left != null) tree = tree.left; return tree; } public T minimum() { BSTNode<T> p = minimum(mRoot); if (p != null) return p.key; return null; }
5. 前驅和后繼
節點的前驅:是該節點的左子樹中的最大節點。
節點的后繼:是該節點的右子樹中的最小節點。
查找前驅節點的代碼
/* * 找結點(x)的前驅結點。即,查找"二叉樹中數據值小於該結點"的"最大結點"。 */ public BSTNode<T> predecessor(BSTNode<T> x) { // 如果x存在左孩子,則"x的前驅結點"為 "以其左孩子為根的子樹的最大結點"。 if (x.left != null) return maximum(x.left); // 如果x沒有左孩子。則x有以下兩種可能: // (01) x是"一個右孩子",則"x的前驅結點"為 "它的父結點"。 // (01) x是"一個左孩子",則查找"x的最低的父結點,並且該父結點要具有右孩子",找到的這個"最低的父結點"就是"x的前驅結點"。 BSTNode<T> y = x.parent; while ((y!=null) && (x==y.left)) { x = y; y = y.parent; } return y; }
查找后繼節點的代碼
/* * 找結點(x)的后繼結點。即,查找"二叉樹中數據值大於該結點"的"最小結點"。 */ public BSTNode<T> successor(BSTNode<T> x) { // 如果x存在右孩子,則"x的后繼結點"為 "以其右孩子為根的子樹的最小結點"。 if (x.right != null) return minimum(x.right); // 如果x沒有右孩子。則x有以下兩種可能: // (01) x是"一個左孩子",則"x的后繼結點"為 "它的父結點"。 // (02) x是"一個右孩子",則查找"x的最低的父結點,並且該父結點要具有左孩子",找到的這個"最低的父結點"就是"x的后繼結點"。 BSTNode<T> y = x.parent; while ((y!=null) && (x==y.right)) { x = y; y = y.parent; } return y; }
6. 插入
插入節點的代碼
/* * 將結點插入到二叉樹中 * * 參數說明: * tree 二叉樹的 * z 插入的結點 */ private void insert(BSTree<T> bst, BSTNode<T> z) { int cmp; BSTNode<T> y = null; BSTNode<T> x = bst.mRoot; // 查找z的插入位置 while (x != null) { y = x; cmp = z.key.compareTo(x.key); if (cmp < 0) x = x.left; else x = x.right; } z.parent = y; if (y==null) bst.mRoot = z; else { cmp = z.key.compareTo(y.key); if (cmp < 0) y.left = z; else y.right = z; } } /* * 新建結點(key),並將其插入到二叉樹中 * * 參數說明: * tree 二叉樹的根結點 * key 插入結點的鍵值 */ public void insert(T key) { BSTNode<T> z=new BSTNode<T>(key,null,null,null); // 如果新建結點失敗,則返回。 if (z != null) insert(this, z); }
注:本文實現的二叉查找樹是允許插入相同鍵值的節點的。若想禁止二叉查找樹中插入相同鍵值的節點,可以參考"二叉查找樹(一)之 圖文解析 和 C語言的實現"中的插入函數進行修改。
7. 刪除
刪除節點的代碼
/* * 刪除結點(z),並返回被刪除的結點 * * 參數說明: * bst 二叉樹 * z 刪除的結點 */ private BSTNode<T> remove(BSTree<T> bst, BSTNode<T> z) { BSTNode<T> x=null; BSTNode<T> y=null; if ((z.left == null) || (z.right == null) ) y = z; else y = successor(z); if (y.left != null) x = y.left; else x = y.right; if (x != null) x.parent = y.parent; if (y.parent == null) bst.mRoot = x; else if (y == y.parent.left) y.parent.left = x; else y.parent.right = x; if (y != z) z.key = y.key; return y; } /* * 刪除結點(z),並返回被刪除的結點 * * 參數說明: * tree 二叉樹的根結點 * z 刪除的結點 */ public void remove(T key) { BSTNode<T> z, node; if ((z = search(mRoot, key)) != null) if ( (node = remove(this, z)) != null) node = null; }
8. 打印
打印二叉查找樹的代碼
/* * 打印"二叉查找樹" * * key -- 節點的鍵值 * direction -- 0,表示該節點是根節點; * -1,表示該節點是它的父結點的左孩子; * 1,表示該節點是它的父結點的右孩子。 */ private void print(BSTNode<T> tree, T key, int direction) { if(tree != null) { if(direction==0) // tree是根節點 System.out.printf("%2d is root\n", tree.key); else // tree是分支節點 System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction==1?"right" : "left"); print(tree.left, tree.key, -1); print(tree.right,tree.key, 1); } } public void print() { if (mRoot != null) print(mRoot, mRoot.key, 0); }
9. 銷毀
銷毀二叉查找樹的代碼
/* * 銷毀二叉樹 */ private void destroy(BSTNode<T> tree) { if (tree==null) return ; if (tree.left != null) destroy(tree.left); if (tree.right != null) destroy(tree.right); tree=null; } public void clear() { destroy(mRoot); mRoot = null; }
完整的實現代碼
二叉查找樹的Java實現文件(BSTree.java)

1 /** 2 * Java 語言: 二叉查找樹 3 * 4 * @author skywang 5 * @date 2013/11/07 6 */ 7 8 public class BSTree<T extends Comparable<T>> { 9 10 private BSTNode<T> mRoot; // 根結點 11 12 public class BSTNode<T extends Comparable<T>> { 13 T key; // 關鍵字(鍵值) 14 BSTNode<T> left; // 左孩子 15 BSTNode<T> right; // 右孩子 16 BSTNode<T> parent; // 父結點 17 18 public BSTNode(T key, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right) { 19 this.key = key; 20 this.parent = parent; 21 this.left = left; 22 this.right = right; 23 } 24 25 public T getKey() { 26 return key; 27 } 28 29 public String toString() { 30 return "key:"+key; 31 } 32 } 33 34 public BSTree() { 35 mRoot=null; 36 } 37 38 /* 39 * 前序遍歷"二叉樹" 40 */ 41 private void preOrder(BSTNode<T> tree) { 42 if(tree != null) { 43 System.out.print(tree.key+" "); 44 preOrder(tree.left); 45 preOrder(tree.right); 46 } 47 } 48 49 public void preOrder() { 50 preOrder(mRoot); 51 } 52 53 /* 54 * 中序遍歷"二叉樹" 55 */ 56 private void inOrder(BSTNode<T> tree) { 57 if(tree != null) { 58 inOrder(tree.left); 59 System.out.print(tree.key+" "); 60 inOrder(tree.right); 61 } 62 } 63 64 public void inOrder() { 65 inOrder(mRoot); 66 } 67 68 69 /* 70 * 后序遍歷"二叉樹" 71 */ 72 private void postOrder(BSTNode<T> tree) { 73 if(tree != null) 74 { 75 postOrder(tree.left); 76 postOrder(tree.right); 77 System.out.print(tree.key+" "); 78 } 79 } 80 81 public void postOrder() { 82 postOrder(mRoot); 83 } 84 85 86 /* 87 * (遞歸實現)查找"二叉樹x"中鍵值為key的節點 88 */ 89 private BSTNode<T> search(BSTNode<T> x, T key) { 90 if (x==null) 91 return x; 92 93 int cmp = key.compareTo(x.key); 94 if (cmp < 0) 95 return search(x.left, key); 96 else if (cmp > 0) 97 return search(x.right, key); 98 else 99 return x; 100 } 101 102 public BSTNode<T> search(T key) { 103 return search(mRoot, key); 104 } 105 106 /* 107 * (非遞歸實現)查找"二叉樹x"中鍵值為key的節點 108 */ 109 private BSTNode<T> iterativeSearch(BSTNode<T> x, T key) { 110 while (x!=null) { 111 int cmp = key.compareTo(x.key); 112 113 if (cmp < 0) 114 x = x.left; 115 else if (cmp > 0) 116 x = x.right; 117 else 118 return x; 119 } 120 121 return x; 122 } 123 124 public BSTNode<T> iterativeSearch(T key) { 125 return iterativeSearch(mRoot, key); 126 } 127 128 /* 129 * 查找最小結點:返回tree為根結點的二叉樹的最小結點。 130 */ 131 private BSTNode<T> minimum(BSTNode<T> tree) { 132 if (tree == null) 133 return null; 134 135 while(tree.left != null) 136 tree = tree.left; 137 return tree; 138 } 139 140 public T minimum() { 141 BSTNode<T> p = minimum(mRoot); 142 if (p != null) 143 return p.key; 144 145 return null; 146 } 147 148 /* 149 * 查找最大結點:返回tree為根結點的二叉樹的最大結點。 150 */ 151 private BSTNode<T> maximum(BSTNode<T> tree) { 152 if (tree == null) 153 return null; 154 155 while(tree.right != null) 156 tree = tree.right; 157 return tree; 158 } 159 160 public T maximum() { 161 BSTNode<T> p = maximum(mRoot); 162 if (p != null) 163 return p.key; 164 165 return null; 166 } 167 168 /* 169 * 找結點(x)的后繼結點。即,查找"二叉樹中數據值大於該結點"的"最小結點"。 170 */ 171 public BSTNode<T> successor(BSTNode<T> x) { 172 // 如果x存在右孩子,則"x的后繼結點"為 "以其右孩子為根的子樹的最小結點"。 173 if (x.right != null) 174 return minimum(x.right); 175 176 // 如果x沒有右孩子。則x有以下兩種可能: 177 // (01) x是"一個左孩子",則"x的后繼結點"為 "它的父結點"。 178 // (02) x是"一個右孩子",則查找"x的最低的父結點,並且該父結點要具有左孩子",找到的這個"最低的父結點"就是"x的后繼結點"。 179 BSTNode<T> y = x.parent; 180 while ((y!=null) && (x==y.right)) { 181 x = y; 182 y = y.parent; 183 } 184 185 return y; 186 } 187 188 /* 189 * 找結點(x)的前驅結點。即,查找"二叉樹中數據值小於該結點"的"最大結點"。 190 */ 191 public BSTNode<T> predecessor(BSTNode<T> x) { 192 // 如果x存在左孩子,則"x的前驅結點"為 "以其左孩子為根的子樹的最大結點"。 193 if (x.left != null) 194 return maximum(x.left); 195 196 // 如果x沒有左孩子。則x有以下兩種可能: 197 // (01) x是"一個右孩子",則"x的前驅結點"為 "它的父結點"。 198 // (01) x是"一個左孩子",則查找"x的最低的父結點,並且該父結點要具有右孩子",找到的這個"最低的父結點"就是"x的前驅結點"。 199 BSTNode<T> y = x.parent; 200 while ((y!=null) && (x==y.left)) { 201 x = y; 202 y = y.parent; 203 } 204 205 return y; 206 } 207 208 /* 209 * 將結點插入到二叉樹中 210 * 211 * 參數說明: 212 * tree 二叉樹的 213 * z 插入的結點 214 */ 215 private void insert(BSTree<T> bst, BSTNode<T> z) { 216 int cmp; 217 BSTNode<T> y = null; 218 BSTNode<T> x = bst.mRoot; 219 220 // 查找z的插入位置 221 while (x != null) { 222 y = x; 223 cmp = z.key.compareTo(x.key); 224 if (cmp < 0) 225 x = x.left; 226 else 227 x = x.right; 228 } 229 230 z.parent = y; 231 if (y==null) 232 bst.mRoot = z; 233 else { 234 cmp = z.key.compareTo(y.key); 235 if (cmp < 0) 236 y.left = z; 237 else 238 y.right = z; 239 } 240 } 241 242 /* 243 * 新建結點(key),並將其插入到二叉樹中 244 * 245 * 參數說明: 246 * tree 二叉樹的根結點 247 * key 插入結點的鍵值 248 */ 249 public void insert(T key) { 250 BSTNode<T> z=new BSTNode<T>(key,null,null,null); 251 252 // 如果新建結點失敗,則返回。 253 if (z != null) 254 insert(this, z); 255 } 256 257 /* 258 * 刪除結點(z),並返回被刪除的結點 259 * 260 * 參數說明: 261 * bst 二叉樹 262 * z 刪除的結點 263 */ 264 private BSTNode<T> remove(BSTree<T> bst, BSTNode<T> z) { 265 BSTNode<T> x=null; 266 BSTNode<T> y=null; 267 268 if ((z.left == null) || (z.right == null) ) 269 y = z; 270 else 271 y = successor(z); 272 273 if (y.left != null) 274 x = y.left; 275 else 276 x = y.right; 277 278 if (x != null) 279 x.parent = y.parent; 280 281 if (y.parent == null) 282 bst.mRoot = x; 283 else if (y == y.parent.left) 284 y.parent.left = x; 285 else 286 y.parent.right = x; 287 288 if (y != z) 289 z.key = y.key; 290 291 return y; 292 } 293 294 /* 295 * 刪除結點(z),並返回被刪除的結點 296 * 297 * 參數說明: 298 * tree 二叉樹的根結點 299 * z 刪除的結點 300 */ 301 public void remove(T key) { 302 BSTNode<T> z, node; 303 304 if ((z = search(mRoot, key)) != null) 305 if ( (node = remove(this, z)) != null) 306 node = null; 307 } 308 309 /* 310 * 銷毀二叉樹 311 */ 312 private void destroy(BSTNode<T> tree) { 313 if (tree==null) 314 return ; 315 316 if (tree.left != null) 317 destroy(tree.left); 318 if (tree.right != null) 319 destroy(tree.right); 320 321 tree=null; 322 } 323 324 public void clear() { 325 destroy(mRoot); 326 mRoot = null; 327 } 328 329 /* 330 * 打印"二叉查找樹" 331 * 332 * key -- 節點的鍵值 333 * direction -- 0,表示該節點是根節點; 334 * -1,表示該節點是它的父結點的左孩子; 335 * 1,表示該節點是它的父結點的右孩子。 336 */ 337 private void print(BSTNode<T> tree, T key, int direction) { 338 339 if(tree != null) { 340 341 if(direction==0) // tree是根節點 342 System.out.printf("%2d is root\n", tree.key); 343 else // tree是分支節點 344 System.out.printf("%2d is %2d's %6s child\n", tree.key, key, direction==1?"right" : "left"); 345 346 print(tree.left, tree.key, -1); 347 print(tree.right,tree.key, 1); 348 } 349 } 350 351 public void print() { 352 if (mRoot != null) 353 print(mRoot, mRoot.key, 0); 354 } 355 }
二叉查找樹的C++測試程序(BSTreeTest.java)

1 /** 2 * Java 語言: 二叉查找樹 3 * 4 * @author skywang 5 * @date 2013/11/07 6 */ 7 public class BSTreeTest { 8 9 private static final int arr[] = {1,5,4,3,2,6}; 10 11 public static void main(String[] args) { 12 int i, ilen; 13 BSTree<Integer> tree=new BSTree<Integer>(); 14 15 System.out.print("== 依次添加: "); 16 ilen = arr.length; 17 for(i=0; i<ilen; i++) { 18 System.out.print(arr[i]+" "); 19 tree.insert(arr[i]); 20 } 21 22 System.out.print("\n== 前序遍歷: "); 23 tree.preOrder(); 24 25 System.out.print("\n== 中序遍歷: "); 26 tree.inOrder(); 27 28 System.out.print("\n== 后序遍歷: "); 29 tree.postOrder(); 30 System.out.println(); 31 32 System.out.println("== 最小值: "+ tree.minimum()); 33 System.out.println("== 最大值: "+ tree.maximum()); 34 System.out.println("== 樹的詳細信息: "); 35 tree.print(); 36 37 System.out.print("\n== 刪除根節點: "+ arr[3]); 38 tree.remove(arr[3]); 39 40 System.out.print("\n== 中序遍歷: "); 41 tree.inOrder(); 42 System.out.println(); 43 44 // 銷毀二叉樹 45 tree.clear(); 46 } 47 }
在二叉查找樹的Java實現中,使用了泛型,也就意味着支持任意類型; 但是該類型必須要實現Comparable接口。
二叉查找樹的Java測試程序
上面的BSTreeTest.java是二叉查找樹樹的測試程序,運行結果如下:
== 依次添加: 1 5 4 3 2 6 == 前序遍歷: 1 5 4 3 2 6 == 中序遍歷: 1 2 3 4 5 6 == 后序遍歷: 2 3 4 6 5 1 == 最小值: 1 == 最大值: 6 == 樹的詳細信息: 1 is root 5 is 1's right child 4 is 5's left child 3 is 4's left child 2 is 3's left child 6 is 5's right child == 刪除根節點: 3 == 中序遍歷: 1 2 4 5 6
下面對測試程序的流程進行分析!
(01) 新建"二叉查找樹"root。
(02) 向二叉查找樹中依次插入1,5,4,3,2,6 。如下圖所示:
(03) 遍歷和查找
插入1,5,4,3,2,6之后,得到的二叉查找樹如下:
前序遍歷結果: 1 5 4 3 2 6
中序遍歷結果: 1 2 3 4 5 6
后序遍歷結果: 2 3 4 6 5 1
最小值是1,而最大值是6。
(04) 刪除節點4。如下圖所示:
(05) 重新遍歷該二叉查找樹。
中序遍歷結果: 1 2 4 5 6