二叉查找树

二叉查找树是二叉树的一种,它的特征是:所有节点的左子树小于节点,所有节点的右子树大于节点,这样便于查找.

下面是二叉查找树的具体实现

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语言描述(第二版)

posted @ 2014-12-30 13:28  黄金JFF  阅读(182)  评论(0编辑  收藏  举报