二叉查找树

自己写的一个二叉查找树,对于该树每一个节点,以该节点为根节点的子树的左侧节点均小于根节点,右侧节点均大于根节点。

package Tree.BinarySearchTree;

import java.nio.BufferUnderflowException;

/**
 * @author DearAhri520
 *
 * 二叉查找树*/
public class BinarySearchTree<T extends Comparable<? super T>> {

    /**
     * 二叉树节点
     */
    private static class BinaryNode<T>{
        T element;
        BinaryNode<T> left;
        BinaryNode<T> right;

        BinaryNode(T t){
            this(t,null,null);
        }

        BinaryNode(T t,BinaryNode<T> left,BinaryNode<T> right){
            this.element=t;
            this.left=left;
            this.right=right;
        }
    }

    /**
     * 根节点
     */
    private BinaryNode<T> root;

    public BinarySearchTree(){
        root=null;
    }


    /**
     * 清空二叉树
     */
    public void makeEmpty(){
        root=null;
    }

    /**
     * 返回该树是否为空
     */
    public boolean isEmpty(){
        return root==null;
    }

    /**
     * 返回该树是否包含节点t
     * @param t 查找的节点
     * @return 返回是否包含该节点
     */
    public boolean contains(T t){
        return contains(t,root);
    }

    /**
     * 查找该树最小的节点
     * @return 返回该树最小的节点
     */
    public T findMin(){
        if(isEmpty()){
            throw new BufferUnderflowException();
        }
        return findMin(root).element;
    }

    /**
     * 查找该树最大的节点
     * @return 返回该树最大的节点
     */
    public T findMax(){
        if(isEmpty()){
            throw new BufferUnderflowException();
        }
        return findMax(root).element;
    }

    /**
     * 插入一个数据
     * @param t 需要插入的数据
     */
    public void insert(T t){
        root=insert(t,root);
    }

    /**
     * 移除一个数据
     * @param t 需要移除的数据
     */
    public void remove(T t){
        root=remove(t,root);
    }

    /**
     * 打印该二叉树
     */
    public void printTree(){
        if(isEmpty()){
            System.out.println("Empty tree");
        }else{
            printTree(root);
        }
    }

    /**
     * 私有方法,判断是否包含该节点
     * @param t 查找的数据
     * @param binaryNode 树的根节点
     * @return 返回是否包含该节点
     */
    private boolean contains(T t,BinaryNode<T> binaryNode){
        //如果节点为空,则表明查找不到,返回false
        if(binaryNode==null){
            return false;
        }
        //比较树的节点与查找的元素
        int compareResult=t.compareTo(binaryNode.element);

        //compareResult<0:向左子树查找,compareResult>0:向右子树查找,compareResult=0,返回true
        if(compareResult<0){
            return contains(t,binaryNode.left);
        }else if(compareResult>0){
            return contains(t,binaryNode.right);
        }else {
            return true;
        }
    }

    /**
     * 查找最小的节点
     * @param binaryNode 查找的数据
     * @return 返回最小的节点
     */
    private BinaryNode<T> findMin(BinaryNode<T> binaryNode){
        //一直向左子树查找,返回该树最左的节点
        if(binaryNode==null){
            return null;
        }else if(binaryNode.left==null){
            return binaryNode;
        }
        return findMin(binaryNode.left);
    }

    /**
     * 查找最大的节点
     * @param binaryNode 查找的数据
     * @return 返回最大的节点
     */
    private BinaryNode<T> findMax(BinaryNode<T> binaryNode){
        //一直向右子树查找,返回该树最右的节点
        if(binaryNode!=null){
            while (binaryNode.right!=null){
                binaryNode=binaryNode.right;
            }
        }
        return binaryNode;
    }

    /**
     *插入一个节点,返回被插入节点的引用
     * @param t 想要插入的节点
     * @param root 根节点
     * @return 返回插入节点的引用
     */
    private BinaryNode<T> insert(T t,BinaryNode<T> root){
        //如果root=null,即到达树的底部,插入该节点,返回被插入的节点
        if(root==null){
            return new BinaryNode<>(t,null,null);
        }
        int compareResult = t.compareTo(root.element);
        //插入的元素小于根节点,向左移动
        if(compareResult<0){
            root.left=insert(t,root.left);
        //插入的元素大于根节点,向右移动
        }else if(compareResult>0){
            root.right=insert(t,root.right);
        }else{
            //Duplicate; do nothing
        }
        return root;
    }

    /**
     * 删除一个节点,返回被删除节点的引用
     * @param t 需要删除的节点
     * @param root 根节点
     * @return 返回删除之后节点的引用
     */
    private BinaryNode<T> remove(T t,BinaryNode<T> root){
        if(root==null){
            return null;
        }
        int compareResult = t.compareTo(root.element);
        //删除的元素小于根节点,向左移动
        if(compareResult<0){
            root.left=remove(t,root.left);
        //删除的元素大于根节点,向右移动
        }else if(compareResult>0){
            root.right=remove(t,root.right);
        //想要删除的节点的左节点与右节点均不为空,则将右子树的最小节点赋给该节点,并删除右子树的最小节点
        }else if(root.left!=null&&root.right!=null){
            root.element=findMin(root.right).element;
            root.right=remove(root.element,root.right);
        }else{
            root=(root.left!=null)?root.left:root.right;
        }
        return root;
    }

    /**
     * 打印整个树
     * @param root 根节点
     */
    private void printTree(BinaryNode<T> root){
        if(root!=null){
            printTree(root.left);
            System.out.println(root.element);
            printTree(root.right);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值