手写一个二叉查找树

对二叉查找树的增删改查进行了java实现

package basicKnowledge.tree;



/**
 * @基本功能:二叉搜索树(二叉排序树)
 * @program:summary
 * @author:peicc
 * @create:2019-07-24 16:52:29
 **/
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
    private BinaryNode<AnyType> root;//根节点
    //构造函数
    public BinarySearchTree(){
        root=null;
    }
    /**
     * @功能:二叉搜索树的结点
     * @Date: 2019/7/24
     */
     static class BinaryNode<AnyType>{
        AnyType element;//数据元素
        BinaryNode<AnyType> left;//左孩子节点
        BinaryNode<AnyType> right;//右孩子节点
        BinaryNode(AnyType element,BinaryNode<AnyType> left,BinaryNode<AnyType> right){
            this.element=element;
            this.left=left;
            this.right=right;
        }
        BinaryNode(AnyType element){
            this(element,null,null);
        }
    }
    /**
     * @功能:判断树是否含有某一元素
     * @Param: [x, root]
     * @return: boolean
     * @Date: 2019/7/24
     */
    public boolean contains(AnyType x,BinaryNode<AnyType> root){
        if(root==null){
            return false;
        }
        int compareResult=x.compareTo(root.element);//比较指定元素与根节点元素的大小
        if(compareResult<0){
            return contains(x,root.left);
        }
        if(compareResult>0){
            return contains(x,root.right);
        }
        return true;
    }
    /**
     * @功能:找到最小的结点(二叉搜索树的最小结点一定是最左结点)
     * @Param: [root]
     * @return: AnyType
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> findMin(BinaryNode<AnyType> root){
        if(root==null){
            return null;
        }
        if(root.left==null){
            return root;
        }else{
            return findMin(root.left);
        }
    }
    /**
     * @功能:找到最大的结点(二叉搜索树的最大结点一定是最右节点 )
     * @Param: [root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> findMax(BinaryNode<AnyType> root){
        if(root==null){
            return null;
        }
        while(root.right!=null){
            root=root.right;
        }
        return root;
    }
    /**
     * @功能:插入结点
     * @Param: [x, root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> insert(AnyType x,BinaryNode<AnyType> root){
        if(root==null){
            return new BinaryNode<>(x,null,null);
        }
        int compareResult=x.compareTo(root.element);
        if(compareResult<0){
            root.left=insert(x,root.left);
        }else if(compareResult>0){
            root.right=insert(x,root.right);
        }else{//结点元素值相等
            ;
        }
        return root;
    }
    /**
     * @功能:删除结点,比较麻烦
     * @Param: [x, root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> remove(AnyType x,BinaryNode<AnyType> root){
        if(root==null){//原树为空,返回空
            return null;
        }
        int compareResult=x.compareTo(root.element);
        if(compareResult<0){//待删除结点的值小于根节点
            root.left=remove(x,root.left);//从左子树中删除并返回删除后的根节点
        }else if(compareResult>0){//待删除结点的值大于根节点
            root.right=remove(x,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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值