java数据结构——二叉排序树的实现

二叉树查找树的定义:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,
则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
在自定义的结构中,使用compareTo()来对节点和节点的左右子树进行比较
public class BinarySearchTree<T extends Comparable<? super  T>> {

    //创建一个节点类,类似于链表中的节点
    private class BinaryNode<T>{
        private T element;         //节点中的元素存储域
        private BinaryNode<T> left;             //指向左子树
        private BinaryNode<T> right;            //指向右子树
        //两种类型的构造参数
        private BinaryNode(T ele){
            this(ele,null,null);
        }
        private BinaryNode(T ele,BinaryNode<T> le,BinaryNode<T> ri){
            element=ele;
            left=le;
            right=ri;
        }
    }

    //定义根节点
    public  BinaryNode<T> root;

    public BinaryNode<T> getRoot(){
        return root;
    }

    //初始化容器,同时初始化根节点的值
    public BinarySearchTree(T r){
        root = new BinaryNode(r);
        root.right=null;
        root.left=null;

    }
    //使整个树为空
    public void makeEmpty(){
        root=null;
    }

    //判断树是否为空
    public boolean isEmpty(){
        return root==null;
    }
    //判断树是否包含某个元素
    public boolean contains(T x){
        return contains(x,root);
    }
    //判断指定节点下是否包含某个元素
    public boolean contains(T x,BinaryNode<T> node){
        if (x==null) {
            return false;
        }
        int comresult = x.compareTo(node.element);
        if(comresult<0){
            return contains(x,node.left);
        }else if (comresult>0){
            return contains(x,node.right);
        }else{
            return true;
        }
    }
    //树中寻找值最小的节点
    public T findMin(){
        if (isEmpty())
            throw  new BufferUnderflowException();
        return findMin(root).element;
    }

    //指定节点下寻找值最小的节点
    public BinaryNode<T> findMin(BinaryNode<T> node){
        if (node==null){                                    //如果节点为空,返回空
            return null;
        }
        else if(node.left==null) {                                  //如果节点左子树为空,则节点最小,返回该节点
            return node;
        }else{
            return findMin(node.left);        //以该树的右子树为基础节点递归查找
        }
    }

    //寻找树中值最大的节点
    public BinaryNode<T> findMax(){
        if (isEmpty()){
            throw new BufferUnderflowException();
        }else{
            return findMax(root);
        }
    }

    //指定节点下寻找值最大的节点
    public BinaryNode<T> findMax(BinaryNode<T> node){
        if (node==null){
            return null;
        }else if(node.right==null){
            return node;                            //节点右子树为空,则节点最大,返回
        }else{
            return findMax(node.right);             //右子树的值都比节点大
        }
    }

    //往树中插入值
    public void insert(T x){
        insert(x,root);
    }
    //指定节点下插入值,并返回该值插入后所在的节点
    public BinaryNode<T> insert(T x,BinaryNode<T> node){
        if (node == null){
            return new BinaryNode <>(x,null,null);
        }
        int comResult = x.compareTo(node.element);   //将插入值和节点的值进行比较,确定插入左子树还是右子树
        if(comResult<0)
            node.left = insert(x,node.left);        //以左子树为基础节点递归查找
        if (comResult>0)
            node.right=insert(x,node.right);
        else                      //和节点值相等,无操作
            ;
        return node;
    }
    //删除指定节点下的为指定值的节点
    public BinaryNode<T> remove(T x,BinaryNode<T> node){
        if (node==null){
            return node;
        }
        int comResult = x.compareTo(node.element);
        if (comResult<0) {       //如果x小于node.element
            node.left = remove(x,node.left);
        }else if (comResult>0){
            node.right=remove(x,node.right);
        }else if(node.left !=null && node.right !=null) {      //两个儿子
            node.element = findMin(node.right).element;
            node.right = remove(node.element,node.right);
        } else {
            node=(node.left !=null)? node.left:node.right;
        }
            return node;
    }
    //遍历整个树
    public void printAllTree(){
        printTree(root);
    }
    //输出指定节点下的所有节点
    public void printTree(BinaryNode<T> node){
        if (node != null){
            //中序遍历:左根右
            printTree(node.left);
            System.out.println(node.element);
            printTree(node.right);
        }
    }
    public static void main(String[] args){
        BinarySearchTree<Integer> binarySearchTree=new BinarySearchTree <>(50);
        binarySearchTree.insert(10);
        binarySearchTree.insert(20);
        binarySearchTree.insert(30);
        binarySearchTree.insert(40);
        binarySearchTree.insert(50);
        binarySearchTree.insert(60);
        binarySearchTree.insert(70);
        binarySearchTree.insert(80);
        binarySearchTree.insert(90);
        binarySearchTree.printAllTree();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40692753/article/details/82997763