您的位置:首页 > 其它

二叉查找树

2012-07-12 15:32 127 查看
package net.liuyx.algorithm;

import java.util.Comparator;

public class BinarySearchTree<T> {
    
    private static class BinaryNode<E>{
        public BinaryNode(E theElement) {
            this(theElement,null,null);
        }
        
        BinaryNode(E theElement, BinaryNode<E> lt, BinaryNode<E> rt){
            element = theElement;
            left = lt;
            right = rt;
        }
        
        E element;
        BinaryNode<E> left;
        BinaryNode< E> right;
    }
    
    private BinaryNode<T> root;
    private Comparator<? super T> cmp;
    public BinarySearchTree() {
        this(null);
    }
    
    public BinarySearchTree(Comparator<? super T>c) {
        root = null;
        cmp = c;
    }
    
    @SuppressWarnings("unchecked")
    private int myCompare(T lhs, T rhs) {
        if(cmp != null)
            return cmp.compare(lhs, rhs);
        else
            return ((Comparable<T>)lhs).compareTo(rhs);
    }
    
    public void makeEmpty() {
        root = null;
    }
    
    public boolean isEmpty() {
        return root == null;
    }
    
    public boolean contains(T x) {
        return contains(x,root);
    }
    
    public T findMin() {
        if(isEmpty())
            throw new RuntimeException(new NullPointerException("There is no elements to find min"));
        return findMin(root).element;
    }
    
    public T findMax() {
        if(isEmpty())
            throw new RuntimeException(new NullPointerException("There is no elements to find max one"));
        return findMax(root).element;
    }
    
    public void insert(T x) {
        root = insert(x, root);
    }
    
    public void remove(T x) {
        root = remove(x, root);
    }
    
    private boolean contains(T x, BinaryNode<T> t) {
        if(t == null)
            return false;
        
        int comareResult = myCompare(x,t.element);
        
        if(comareResult < 0)
            return contains(x,t.left);
        else if(comareResult > 0)
            return contains(x,t.right);
        else
            return true;
    }
    
    private BinaryNode<T> findMin(BinaryNode<T> t){
        if(t == null)
            return null;
        else if(t.left == null)
            return t;
        return findMin(t.left);
    }
    
    private BinaryNode<T> findMax(BinaryNode<T> t){
        if(t != null)
            while(t.right != null)
                t = t.right;
        return t;
    }
    
    private BinaryNode<T> insert(T x, BinaryNode<T> t){
        if(t == null)
            return new BinaryNode<T>(x);
        int compareResult = myCompare(x,t.element);
        if(compareResult < 0)
            t.left = insert(x,t.left);
        else if(compareResult > 0)
            t.right = insert(x,t.right);
        else
            ;// Duplicate; do nothing
        return t;
    }
    
    private BinaryNode<T> remove(T x, BinaryNode<T> t){
        if(t == null)
            return t; //Item not found, do nothing
        
        int compareResult = myCompare(x,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 = findMax(t.right).element;
            t.right = remove(t.element,t.right);
        }else
            t = (t.left != null) ? t.left : t.right;
        return t;
    }
    public static void main(String[] args) {
        BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();
        int[]a = {1,3,5,9,4,6};
        for(int i: a) {
            bst.insert(i);
        }
        int min = bst.findMin();
        System.out.println(min);
        int max = bst.findMax();
        System.out.println(max);
        System.out.println(bst.contains(4));
        bst.remove(9);
        System.out.println(bst.findMax());
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: