數據結構 二叉查找樹(java實現)

定義:

二叉查找樹(Binary Search Tree),(又:二叉搜索樹,二叉排序樹)它或者是一棵空樹,或者是具有下列性質的二叉樹: 若它的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值; 若它的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值; 它的左、右子樹也分別爲二叉排序樹

實現:

    首先看看如何實現一個二叉查找樹的節點,節點類分別包含節點的數據,節點的左節點,節點的右節點,實現起來不難

public class BinaryNode<AnyType> {
    AnyType element;
    BinaryNode<AnyType> left;
    BinaryNode<AnyType> right;

    BinaryNode(AnyType theElement){
        this(theElement, null, null);
    }
    BinaryNode(AnyType theElement,BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){
        element = theElement;
        left = lt;
        right = rt;
    }
}

    下面實現一個二叉查找樹的類

public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
    private BinaryNode<AnyType> root;
//    private Comparator<? super AnyType> cmp;
//    public BinarySearchTree(Comparator<?super AnyType> c){
//        root = null;
//        cmp = c;
//    }
    private static class BinaryNode<AnyType>{
        AnyType element;
        BinaryNode<AnyType> left;
        BinaryNode<AnyType> right;

        BinaryNode(AnyType theElement){
            this(theElement, null, null);
        }
        BinaryNode(AnyType theElement,BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){
            element = theElement;
            left = lt;
            right = rt;
        }
    }
    public BinarySearchTree(){
        root = null;
    }
    public void makeEmpty(){
        root = null;
    }
    public boolean isEmpty(){
        return root == null;
    }
    public boolean contains(AnyType x){
        return contains(x,root);
    }
    public AnyType findMin() throws Exception {
        if(isEmpty()) throw new Exception();
        return findMin(root).element;
    }
    public AnyType findMax() throws Exception {
        if(isEmpty()) throw new Exception();
        return findMax(root).element;
    }
    public void insert(AnyType x){
        root = insert(x,root);
    }
    public void remove(AnyType x){
        root = remove(x,root);
    }
    public void printTree(){

    }
//    private int myCompare(AnyType lhs, AnyType rhs){
//        if(cmp!=null)
//            return cmp.compare(lhs,rhs);
//        else return ((Comparable)lhs).compareTo(rhs);
//    }
    private boolean contains(AnyType x , BinaryNode<AnyType> t){
        if(t == null){
            return  false;
        }
        int compareResult = x.compareTo(t.element);
        if(compareResult<0)
            return contains(x, t.left);
        else if(compareResult>0)
            return contains(x, t.right);
        else return true;
    }
    private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
        if(t == null)
            return null;
        else if(t.left == null)
            return t;
        return findMin(t.left);

    }
    private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
        if(t!=null)
            while (t.right!=null)
                t = t.right;
        return t;

    }
    private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t){
        if(t == null)
            return new BinaryNode<>(x , null ,null);
        int compareResult = x.compareTo(t.element);
        if(compareResult<0)
            insert(x, t.left);
        else if(compareResult>0)
            insert(x,t.right);
        else ;
        return t;
    }
    private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t){
        if(t == null)
            return t;
        int compareResult = x.compareTo(t.element);
        if(compareResult<0)
            t.left = remove(x, t.left);
        else if(compareResult>0)
            t.right = remove(x,t.right);
        else if(t.left!=null&&t.right!=null){
            t.element = findMin(t.right).element;
            t.right = remove(t.element,t.right);
        }
        else t = (t.left!=null)? t.left : t.right;
        return t;
    }
    private void print(AnyType x, BinaryNode<AnyType> t){

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章