二叉搜索树(二叉排序树)

一.概念

二叉搜索树又称二叉排序树,具有以下性质:

  • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
  • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
  • 它的左右子树也分别为二叉搜索树

注意:二叉搜索树中序遍历的结果是有序的
在这里插入图片描述

二、基本操作

1.查找元素

思路:二叉搜索树的左子树永远是比根节点小的,而它的右子树则都是比根节点大的值。当前节点比要找的大就往左走,当前元素比要找的小就往右走

	public Node search(int key) {
        if(root == null) {
            return null;
        }
        Node cur = root;
        while (cur != null) {
            if(cur.val == key) {
                return cur;
            }else if(cur.val > key) {
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return null;
    }

2.插入元素

如果是空树直接把元素插入root位置就好了
思路:因为是二叉搜索树就不能插入重复的元素了,且每次插入都是插入到叶子节点的位置。定义一个 cur 从root开始,插入的元素比当前位置元素小就往左走,比当前位置元素大就往右走,直到为空,所以就需要再定义一个变量parent 记住 cur 的前面的位置。
最后再判断插入到parent 的左子树还是右子树位置

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

	public boolean insert(int key) {
        Node node = new Node(key);
        if(root == null) {
            this.root = node;
            return true;
        }
        Node parent = null;
        Node cur = root;
        while (cur != null) {
            if(cur.val == key) {
                //有相同的元素直接return
                return false;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }
        if (parent.val > key) {
            parent.left = node;
        }else{
            parent.right = node;
        }
        return true;
    }

3.删除元素

删除元素是一个比较难的点,要考虑到很多种情况

  1. 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
  2. cur.right == null

    1. cur 是 root,则 root = cur.left
    2. cur 不是 root,cur 是 parent.left,则 parent.left = cur.left
    3. cur 不是 root,cur 是 parent.right,则 parent.right = cur.left
  3. cur.left != null && cur.right != null
    采用替罪羊的方式删除

    1. 找到要删除节点,右树最左边的节点或者找到左树最右边的节点,替换这个两个节点的val值。
    2. 这样才能保证,删除后左树一定比根节点小,右树一定比根节点大
      在这里插入图片描述
	public boolean remove(int key) {
        if(this.root == null) {
            return false;
        }
        Node parent = null;
        Node cur = this.root;
        while (cur != null) {
            if(cur.val == key) {
                removeKey(parent,cur);
                return true;
            }else if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        return false;
    }
    public void removeKey(Node parent,Node cur) {
        if(cur.left == null) {
            if(cur == this.root) {
                this.root = this.root.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(this.root == cur) {
                this.root = this.root.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{//左右都不为空的情况
            Node targetParent = cur;
            Node target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.right;
            }else{
                targetParent.right = target.right;
            }
        }
    }

4.性能分析

插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:

最好情况:二叉搜索树为完全二叉树,其平均比较次数为 O(log 2 _2 2n)

在这里插入图片描述

最坏情况:二叉搜索树退化为单支树,其平均比较次数为:O(n)

在这里插入图片描述

所有代码:

public class BinarySearchTree {
    public static class Node {
        int val;
        Node left;
        Node right;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node root = null;
    /**
     * 查找某个节点
     * @param key
     */
    public Node search(int key) {
        if(root == null) {
            return null;
        }
        Node cur = root;
        while (cur != null) {
            if(cur.val == key) {
                return cur;
            }else if(cur.val > key) {
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        return null;
    }

    /**
     * 插入元素
     * @param key
     * @return
     */
    public boolean insert(int key) {
        Node node = new Node(key);
        if(root == null) {
            this.root = node;
            return true;
        }
        Node parent = null;
        Node cur = root;
        while (cur != null) {
            if(cur.val == key) {
                //有相同的元素直接return
                return false;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }
        if (parent.val > key) {
            parent.left = node;
        }else{
            parent.right = node;
        }
        return true;
    }

    /**
     * 删除元素
     * @param key
     */
    public boolean remove(int key) {
        if(this.root == null) {
            return false;
        }
        Node parent = null;
        Node cur = this.root;
        while (cur != null) {
            if(cur.val == key) {
                removeKey(parent,cur);
                return true;
            }else if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        return false;
    }
    public void removeKey(Node parent,Node cur) {
        if(cur.left == null) {
            if(cur == this.root) {
                this.root = this.root.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(this.root == cur) {
                this.root = this.root.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
            Node targetParent = cur;
            Node target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.right;
            }else{
                targetParent.right = target.right;
            }
        }
    }
}

<think>嗯,用户想了解二叉排序树二叉搜索树的区别和联系。首先,我需要确认这两个术语的定义。根据提供的引用,引用[2]、[4]提到二叉搜索树二叉排序树、二叉查找树是同一概念的不同名称。比如引用[2]明确说“二叉搜索树(BST,Binary Search Tree),也称二叉排序树或二叉查找树”,而引用[4]也指出“二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree)”。这说明它们是同一个数据结构的不同翻译或称呼,可能没有本质区别。 接下来,用户可能想知道为什么会有不同的名称。可能是中文翻译的不同,比如“排序”和“搜索”对应不同的侧重点,但实际结构特性一致。根据引用中的描述,无论是二叉排序树还是二叉搜索树,都满足左子树节点小于根,右子树节点大于根的特性,并且中序遍历得到有序序列。例如引用[2]提到“对二叉搜索树进行中序遍历可以得到按关键字排序的有序序列”,这也适用于二叉排序树。 然后,我需要检查是否有引用提到两者的区别。但根据提供的资料,所有引用都统一认为它们是同一概念的不同名称,没有提到区别。例如引用[4]直接说“二叉排序树又称二叉查找(搜索)树”,引用[2]也做了同样的说明。因此可以确定它们是同一数据结构的不同名称,没有本质区别。 此外,用户可能还想了解它们的应用场景和操作,比如查找、插入、删除等。根据引用[5]中的代码示例,二叉搜索树的查找操作利用了左小右大的特性,这和二叉排序树的操作一致。引用[3]提到二叉查找树在大量数据下查询速度较快,这也适用于二叉排序树。 总结来说,用户的问题可能源于不同资料中使用不同术语导致的困惑,需要明确两者是同一概念的不同称呼,没有区别。同时,可以进一步解释它们的特性和操作,帮助用户全面理解。</think>### 二叉排序树 vs 二叉搜索树:定义、区别与联系 #### 1. **定义** - **二叉排序树(Binary Sort Tree)** 是一种特殊的二叉树结构,满足以下性质: 1. 若左子树非空,则左子树所有结点的值均小于根结点的值; 2. 若右子树非空,则右子树所有结点的值均大于根结点的值; 3. 左、右子树本身也是二叉排序树[^4][^2]。 - **二叉搜索树(Binary Search Tree, BST)** 定义与二叉排序树完全一致,仅名称不同。其核心性质是通过二分思想实现高效查找、插入和删除操作[^2][^5]。 #### 2. **区别与联系** - **名称差异** - **二叉排序树**:强调中序遍历结果是有序序列(即对数据排序的支持)。 - **二叉搜索树**:强调基于二分法的快速搜索能力[^3]。 - **本质一致性** 两者是同一数据结构的不同名称,**无本质区别**。中文资料中常将二者混用,英文统称为 *Binary Search Tree (BST)*[^4]。 #### 3. **核心特性** - **有序性**:中序遍历结果为升序序列,例如对树 $$T$$ 进行中序遍历可得到 $$a_1 < a_2 < \dots < a_n$$[^4]。 - **高效操作**: - **查找**:时间复杂度为 $O(h)$($h$ 为树的高度),最优情况下为 $O(\log n)$(平衡树)。 - **插入/删除**:需保持有序性,可能导致树结构变化(如调整子树)[^5]。 #### 4. **应用场景** 1. 数据库索引结构(如B树的基础)。 2. 动态数据集合的快速检索与维护[^5]。 3. 排序算法(通过中序遍历直接输出有序序列)。 #### 5. **代码示例(查找操作)** ```c++ bool Find(const K& key) { Node* cur = _root; while (cur) { if (key < cur->_key) cur = cur->_left; // 左子树查找 else if (key > cur->_key) cur = cur->_right; // 右子树查找 else return true; // 找到目标 } return false; // 未找到 } ``` ---
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱敲代码的三毛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值