小弟初学java,写了一个泛型的二叉搜索树(源代码见后)。
IBSTree接口定义一个二叉搜索树的对外接口,这个接口描述了二叉搜索树的所有对外可见的行为。具体的方法描述见源代码的注释。
IBSTree接口中有定义了一个二叉搜索树的节点类型的接口IBSTNode(这个接口也可以在IBSTree接口的外部定义,只是这个接口太小,个人觉得没必要单独定义),
这个接口描述了二叉搜索树的一个节点的对外可以的部分(对外只是可以获取节点的值)。
然后再定义了两个类BSTNode和BSTree,分别实现了这两个接口。
这个设计有点复杂,自己都觉得不够清爽^-^,
定义IBSNode纯粹是考虑到实现的效率,返回一个IBSNode的类可以传达更多的节点信息,尽管这些信息对外不可见,但是内部实现需要这些信息。
二叉搜索树的各个操作的实现没有做注释^-^,是由于这些操作在任何一本数据结构的书上都有详细的说明。
小弟主要关注的是泛型和程序的整个结构。
请各位路过的大虾指点一下,小弟万分感激。^-^
源文件共三个:IBSTree.java、BSTNode.java、BSTree.java。
文件1:IBSTree.java
package BinarySearchTree;
//二叉搜索树类型
public interface IBSTree<Value extends Comparable<Value>> {
    
    public interface IBSTNode<Value> {
        
        Value getValue();                            //获取二叉搜索树结点的键
        
        boolean isEmpty();                            //判断二叉搜索树结点是否为空
        
    }
    
    void makeEmptyBST();                            //创建一棵空的二叉搜索树
    
    boolean isEmptyBST();                            //判断一棵二叉搜索树是否为空
    
    IBSTNode<Value> search(Value k);                //在二叉树搜索树中查找指定的键
    
    boolean insert(Value k);                        //向二叉搜索树中插入键
    
    IBSTNode<Value> getMax();                        //获取二叉搜索树中的最大值
    
    IBSTNode<Value> getMin();                        //获取二叉搜索树中的最小值
    
    IBSTNode<Value> succ(IBSTNode<Value> n);        //获取后继
    
    IBSTNode<Value> pred(IBSTNode<Value> n);        //获取前驱
    
    boolean delete(IBSTNode<Value> n);                //删除二叉搜索树中的指定元素 
    
}
文件2:BSTNode.java
package BinarySearchTree;
/**
 *BSTNode类表示二叉搜索树的一个结点。
 *BSTNode的值可以是:
 *  1、EMPTY。表示这是一个空结点。
 *  2、(key,left,right,parent)。其中key表示结点值,left表示左子树,right表示右子树,parent表示父节点。 
 *Value类型是一种实现了Comparable接口的类型。
 */
public final class BSTNode<Value extends Comparable<Value>> 
    implements IBSTree.IBSTNode<Value> {
        
        public Value getValue() { //获取二叉搜索树结点的键
            if (this==EMPTY) return null;
            else return this.key;
        }
        
        public boolean isEmpty() { //判断二叉搜索树结点是否为空
            if (this==EMPTY) return true;
            else return false;
        }
        
        static final BSTNode EMPTY=new BSTNode(null,null,null,null); //表示空的二叉树节点
            
        BSTNode<Value> left;
        
        BSTNode<Value> right;
        
        BSTNode<Value> parent;
        
        Value key;
        
        BSTNode(Value key,BSTNode<Value> left,
            BSTNode<Value> right,BSTNode<Value> parent) {
            this.key=key;
            this.left=left;
            this.right=right;
            this.parent=parent;
        }            
        
    }
文件3:BSTree.java
package BinarySearchTree;
/**
 *BSTree类表示二叉搜索树。
 *Value类型是一种实现了Comparable接口的类型。
 */
public final class BSTree<Value extends Comparable<Value>> 
    implements IBSTree<Value> {
    public void makeEmptyBST() {//创建一棵空的二叉搜索树
        root=BSTNode.EMPTY;
    }
    
    public boolean isEmptyBST()    {//判断二叉搜索树结点是否为空
        if (root==BSTNode.EMPTY) return true;
        else return false;
    }
    public IBSTNode<Value> getMax() {//获取二叉搜索树中的最大值
        if (root==BSTNode.EMPTY) return BSTNode.EMPTY;
        else {
            BSTNode<Value> p=root;
            while ((p!=BSTNode.EMPTY)&&(p.right!=BSTNode.EMPTY))
                p=p.right;
            return p;
        }            
    }
    public IBSTNode<Value> getMin() {//获取二叉搜索树中的最小值
        if (root==BSTNode.EMPTY) return BSTNode.EMPTY;
        else {
            BSTNode<Value> p=root;
            while ((p!=BSTNode.EMPTY)&&(p.left!=BSTNode.EMPTY))
                p=p.left;
            return p;
        }            
    }
    
    public IBSTNode<Value> search(Value k) {//在二叉树搜索树中查找指定的键
        BSTNode<Value> p=root;
        while ((p!=BSTNode.EMPTY)&&(p.key.compareTo(k)!=0))
            if (k.compareTo(p.key)<0) p=p.left;
            else p=p.right;
        return p;
    }
    public boolean insert(Value k) {//向二叉搜索树中插入键
        if (root==BSTNode.EMPTY) {
            root=new BSTNode<Value>(k,BSTNode.EMPTY,
                BSTNode.EMPTY,BSTNode.EMPTY);
            return true;
        } else {
            BSTNode<Value> p=root;
            BSTNode<Value> q=root;
            while ((p!=BSTNode.EMPTY)&&(p.key.compareTo(k)!=0))
                if (k.compareTo(p.key)<0) {
                    q=p;
                    p=p.left;
                } else {
                    q=p;
                    p=p.right;
                };
            if (p==BSTNode.EMPTY) {
                p=new BSTNode<Value>(k,BSTNode.EMPTY,
                    BSTNode.EMPTY,BSTNode.EMPTY);
                if (k.compareTo(q.key)<0) q.left=p;
                else q.right=p;
                return true;
            } else return false;
        }
    }
public IBSTNode<Value> succ(IBSTNode<Value> n) {//获取后继
        if (!(n instanceof BSTNode)) return BSTNode.EMPTY;
        BSTNode<Value> m;
        m=(BSTNode<Value>) n;
        if (m==BSTNode.EMPTY) return BSTNode.EMPTY;
        else {
            if (m.right!=BSTNode.EMPTY) {
                BSTree<Value> p=new BSTree<Value>(m.right);
                return p.getMin();
            } else {
                BSTNode<Value> p=m;
                BSTNode<Value> q=p.parent;
                while ((q!=BSTNode.EMPTY)&&(q.right==p)) {
                    p=q;
                    q=p.parent;
                }
                if (q==BSTNode.EMPTY) return BSTNode.EMPTY;
                else return q;
            }
        }        
    }
    public IBSTNode<Value> pred(IBSTNode<Value> n) {//获取前驱
        if (!(n instanceof BSTNode)) return BSTNode.EMPTY;
        BSTNode<Value> m;
        m=(BSTNode<Value>) n;
        if (m==BSTNode.EMPTY) return BSTNode.EMPTY;
        else {
            if (m.left!=BSTNode.EMPTY) {
                BSTree<Value> p=new BSTree<Value>(m.left);
                return p.getMax();
            } else {
                BSTNode<Value> p=m;
                BSTNode<Value> q=p.parent;
                while ((q!=BSTNode.EMPTY)&&(q.left==p)) {
                    p=q;
                    q=p.parent;
                }
                if (q==BSTNode.EMPTY) return BSTNode.EMPTY;
                else return q;
            }
        }
    }
    public boolean delete(IBSTNode<Value> n) {//删除二叉搜索树中的指定元素 ,没实现
        return true;
    }
    
    private BSTNode<Value> root;
    
    private BSTree(BSTNode<Value> root){
        this.root=root;
    }
}



											
	    

	