二叉搜索树的实现(代码版实现)

public class BinarySearchTree {
 //二叉搜说树代码实现
    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;
 
        TreeNode(int key) {
            this.key = key;
        }
    }
 
    public TreeNode root;
 
    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if(root==null){
            root=new TreeNode(key);
        }
        TreeNode cur=root;
        TreeNode parent=null;
        while(cur!=null){
            if(cur.key<key){
                parent=cur;
                cur=cur.right;
            }else if(cur.key==key){
                return  true;
            }
            else{
                parent=cur;
                cur=cur.left;
            }
        }
        
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur=root;
        while(cur!=null){
            if(cur.key<key){
                cur=cur.right;
            }else if(cur.key==key){
                return cur;
            }else{
                cur=cur.left;
            }
        }
      
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.key == key) {
                removeNode(parent,cur);
                return true;
            }else if(cur.key  < key) {
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        return false;
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        //1cur.left==null

        //cur为删除的结点,且cur是根结点,
        //
       if(cur.left==null){
           if(cur==root){
               root=root.right;
           }else if(parent.left==cur){//cur不是根节点
               parent.left=cur.right;
           }else{
               parent.right=cur.right;
           }
       }
       if(cur.right==null){
           if(cur==root){
               root=root.left;
           }else if(parent.left==null){
               parent.left=cur.left;
           }else{
               parent.right=cur.left;
           }
       }
       if(cur.left!=null&&cur.right!=null){
           //需要使用替换删除
           TreeNode target=cur.right;
           TreeNode targetP=cur;
           while(target.left!=null){
               targetP=target;
               target=target.left;
           }
           cur.key=target.key;
           if(target == targetP.left) {
               targetP.left = target.right;
           }else {
               targetP.right = target.right;
           }
       }
    }


}

今天由于时间紧迫,我先写一个代码版的,之后再写这个分析版的哈

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值