二叉搜索树的思想,以及增删查改的实现

搜索树的概念

二叉搜索树又被称为排序树,它或者是一颗空树,或者是一棵具有以下性质的二叉树:

  1. 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  2. 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  3. 它的左右子树也分别为二叉搜索树
    下图就是一棵二叉搜索树,可以对应上面性质加深理解:
    在这里插入图片描述

查找操作

实现思想:
在这里插入图片描述
实现代码:

// O(树的高度)
 public boolean find(int key) {
        Node current = root;
        while (current != null) {
            if (key == current.key) {
                return true;
            } else if (key < current.key) {
                current = current.left;
            } else {
                current = current.right;
            }
        }

        return false;
    }

插入操作

实现思想:

  1. 如果树为空树,即根 == null,直接插入
  2. 如果不时空树,按照查找逻辑确定插入位置,插入新节点,这里需要引入两个变量

实现代码:

// O(树的高度)
 public void insert(int key) {
        if (root == null) {
            root = new Node(key);
            return;
        }
        Node parent = null;
        Node current = root;
        while (current != null) {
            if (key == current.key) {
                throw new RuntimeException("BST 中不允许重复的 key: " + key);
            } else if (key < current.key) {
                parent = current;
                current = current.left;
            } else {
                parent = current;
                current = current.right;
            }
        }

        // 1. 把关键字装入结点中
        Node node = new Node(key);
        if (key < parent.key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
    }

删除操作

设待删除节点为cur,待删除节点的双亲节点为parent
有以下三种情况:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
实现代码:

  // O(树的高度)
    public boolean remove(int key) {
        Node parent = null;
        Node current = root;
        while (current != null) {
            if (key == current.key) {
                // 删除 current 中的 key
                removeNode(parent, current);
                return true;
            } else if (key < current.key) {
                parent = current;
                current = current.left;
            } else {
                parent = current;
                current = current.right;
            }
        }

        return false;
    }

    // O(1)
    private void removeNode(Node parent, Node current) {
        if (current.left == null) {
            if (current == root) {
                root = current.right;
            } else if (current == parent.left) {
                parent.left = current.right;
            } else {
                parent.right = current.right;
            }
        } else if (current.right == null) {
            if (current == root) {
                root = current.left;
            } else if (current == parent.left) {
                parent.left = current.left;
            } else {
                parent.right = current.left;
            }
        } else {
            Node goat = current.right;
            Node goatParent = current;
            while (goat.left != null) {
                goatParent = goat;
                goat = goat.left;
            }

            // 替换
            current.key = goat.key;
            // 删除 goat 结点
            if (goatParent == current) {
                goatParent.right = goat.right;
            } else {
                goatParent.left = goat.right;
            }
        }
    }

改的操作

改的操作和查一模一样,只不过在找到目标节点之后修改它的数值即可。

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有裂痕的石头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值