二叉搜索树

16 篇文章 0 订阅
二叉搜索树:

又称二叉排序树,它或者是一棵空树**,或者是具有以下性质的二叉树:

1.若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
2.若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
3.它的左右子树也分别为二叉搜索树
在这里插入图片描述搜索二叉树的操作主要有:
a) 增加
b) 查找
c) 删除
(PS:为什么没有修改? 因为修改会破坏搜索二叉树的结构,一般都是先删除再插入,或者不修改key的值,只修改value)

代码实现

结点

public static class TreeNode{
        public int key;
        public int value;
        TreeNode left;
        TreeNode right;

        public TreeNode(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

增加:

private static TreeNode root;
    public static boolean put(int key,int value){
        TreeNode NewNode=new TreeNode(key,value);
        if(root == null){
            root=NewNode;
            return true;
        }
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur !=null){
            if(key>cur.key){
                prev=cur;
                cur=cur.right;
            }else if(key < cur.key){
                prev=cur;
                cur=cur.left;
            }else{
                return  false;
            }
        }
        if(key >prev.key){
            prev.right=NewNode;
        }else {
            prev.left=NewNode;
        }
        return true;
    }

查找:

 public static TreeNode Find(int key,int value){
        TreeNode cur=root;
        while(cur != null){
            if(key>cur.key){
                cur=cur.right;
            } else  if(key <cur.key){
                cur=cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }

删除:

public static boolean remove(int key,int value){
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur!=null){
            if(key>cur.key){
                prev=cur;
                cur=cur.right;
            }else if(key <cur.key){
                prev=cur;
                cur=cur.left;
            }else{
                removeHelp(prev,cur);
                return true;
            }
        }
        return false;
}
public static void removeHelp(TreeNode prev,TreeNode cur){
        if(cur.left == null){
            if(cur==root){
                root=root.right;
            }else if(prev.left==cur){
                prev.left=cur.right;
            }else {
                prev.right=cur.right;
            }
        } else if (cur.right == null){
                if(cur == root){
                    root=root.left;
                }else if(cur==prev.left){
                    prev.left=cur.left;
                }else{
                    prev.right=cur.left;
                }
        }else{
            TreeNode goatParents = cur;
            TreeNode scapegoat = cur.right;
            while(scapegoat.left != null){
                goatParents=scapegoat;
                scapegoat=scapegoat.left;
            }
            cur.key=scapegoat.key;
            cur.value=scapegoat.value;
            if(scapegoat == goatParents.left){
                goatParents.left=scapegoat.left;
            } else {
                goatParents.right=goatParents.right;
            }
        }
}

这里删除需要考虑很多种情况(cur是要删除的结点,parent是cur的父亲结点):
一. cur.left == null

  1. cur 是 root,则 root = cur.right
  2. cur 不是 root,cur 是 parent.left,则 parent.left = cur.right
  3. cur 不是 root,cur 是 parent.right,则 parent.right = cur.right
    二. cur.right == null
  4. cur 是 root,则 root = cur.left
  5. cur 不是 root,cur 是 parent.left,则 parent.left = cur.left
  6. cur 不是 root,cur 是 parent.right,则 parent.right = cur.left
    三. cur.left != null && cur.right != null
  7. 需要使用替换法进行删除,即在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被
    删除节点中,再来处理该结点的删除问题
须知:

二叉搜索树的中序遍历是有序的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值