美文网首页
二分查找树

二分查找树

作者: 萌妈码码 | 来源:发表于2018-08-18 20:46 被阅读0次

Binary Search Tree is a special form of a binary tree. The value in each node must be greater than (or equal to) any values in its left subtree but less than (or equal to) any values in its right subtree.

二叉查找树的基本操作

The strength of a BST is that you can perform all search, insertion and deletion operations in O(h) time complexity even in the worst case.

查找一个节点

According to the property of BST, for each node:

1. return the node if the target value is equal to the value of the node;

2. continue searching in the left subtree if the target value is less than the value of the node;

3. continue searching in the right subtree if the target value is larger than the value of the node.

LeetCode 700. Search in a Binary Search Tree

插入一个新节点

Similar to our search strategy, for each node, we will:

1. search the left or right subtrees according to the relation of the value of the node and the value of our target node;

2. repeat STEP 1 until reaching an external node;

3. add the new node as its left or right child depending on the relation of the value of the node and the value of our target node.

In this way, we add a new node and maintain the property of BST.

LeetCode 701. Insert into a Binary Search Tree

删除一个指定节点

1. If the target node has no child, we can simply remove the node.

2. If the target node has one child, we can use its child to replace itself.

3. If the target node has two children, replace the node with its in-order successor or predecessor node and delete that node.

LeetCode 450. Delete Node in a BST

两点注意:

1. 删除节点的方式很多;实际中,如果节点的值是个很复杂的对象,这种通过交换被删除节点和其后继的值的方式,在实际中可能代价会比较大,不如直接交换引用(如以下实现)。

2. 如果被删除节点的左右子树均为空,那么可以将其左子树作为其后继节点的左子树。不过这种方式有个缺点是,可能会加大树的高度,从而使树本身更加不平衡。以下实现中,通过改变左右子树的引用,返回其后继节点作为新的根。

删除指定节点 删除根节点

其他相关算法

非递归中序遍历二叉树模板可以有效地解决一些问题。

LeetCode 94. Binary Tree Inorder Traversal

LeetCode 230. Kth Smallest Element in a BST

LeetCode 98. Validate Binary Search Tree

LeetCode 173. Binary Search Tree Iterator

引用:

Learn one iterative inorder traversal, apply it to multiple tree questions

An easy-understanding O(h) time, O(1) space Java solution.

Iterative solution in Java, O(h) time and O(1) space

相关文章

  • LeetCode 力扣 98. 验证二叉搜索树

    题目描述(中等难度) 输入一个树,判断该树是否是合法二分查找树,95题做过生成二分查找树。二分查找树定义如下: 若...

  • LeetCode 力扣 99. 恢复二叉搜索树

    题目描述(困难难度) 依旧是二分查找树的题,一个合法的二分查找树随机交换了两个数的位置,然后让我们恢复二分查找树。...

  • 二叉搜索树、平衡二叉树

    -二叉搜索树 查找问题:静态查找和动态查找,静态查找可以用二分查找-判定树,那么针对动态查找数据如何组织?(树的动...

  • 查找

    查找一般要掌握顺序查找、二分查找、哈希表查找和二叉排序树查找。要能够快速准确地写出二分查找的代码。 1. 顺序查找...

  • 二叉查找树的基础——插入和查找

    上一篇文章我们讲了如何通过二分查找进行搜索,今天这篇文章,我们介绍二分查找的高级版,即二叉查找树。二叉查找树主要解...

  • 第四讲-树(中)

    树(中) 二叉搜索(排序/查找)树 作用:为了进行二分查找,将数据构建在查找树中,相比与线性结构树的插入删除等动态...

  • LeetCode 力扣 95. 不同的二叉搜索树 II

    题目描述(中等难度) 给一个 n,用1...n 这些数字生成所有可能的二分查找树。所谓二分查找树,定义如下: 若任...

  • 动画 | 什么是二分搜索树(二叉查找树)?

    二分搜索树属性 二分搜索树的又名比较多,有的叫二叉排序树,也有的叫二叉查找树,或者有序二叉查找树。是指一棵空树或者...

  • 数据结构--查找

    查找分类 有序查找(二分查找、插值查找、斐波拉契查找) 线性索引查找 二叉排序树 散列表

  • 数据结构与算法 树 引言 顺序查找 ​ 哨兵的方式 ​ 时间复杂度为O(N) 二分查找查找树的形式我...

网友评论

      本文标题:二分查找树

      本文链接:https://www.haomeiwen.com/subject/cvuabftx.html