当前位置:网站首页>二叉搜索树(基操篇)
二叉搜索树(基操篇)
2022-07-07 14:17:00 【Joey Liao】
二叉搜索树(特性篇)介绍了 BST 的基本特性,还利用二叉搜索树「中序遍历有序」的特性来解决了几道题目,本文来实现 BST 的基础操作:判断 BST 的合法性、增、删、查。其中「删」和「判断合法性」略微复杂。
BST 的基础操作主要依赖「左小右大」的特性,可以在二叉树中做类似二分搜索的操作,寻找一个元素的效率很高。比如下面这就是一棵合法的二叉树:
对于 BST 相关的问题,你可能会经常看到类似下面这样的代码逻辑:
void BST(TreeNode root, int target) {
if (root.val == target)
// 找到目标,做点什么
if (root.val < target)
BST(root.right, target);
if (root.val > target)
BST(root.left, target);
}
一、判断 BST 的合法性
力扣第 98 题「 验证二叉搜索树」就是让你判断输入的 BST 是否合法。
需要注意的一点是不能直接比较自己和左右孩子,但这种想法是错误的,BST 的每个节点应该要小于右边子树的所有节点,正确代码如下:
class Solution {
public boolean isValidBST(TreeNode root) {
return traverse(root,null,null);
}
public boolean traverse(TreeNode root,TreeNode minNode,TreeNode maxNode){
if(root==null) return true;
if(minNode!=null&&minNode.val>=root.val) return false;
if(maxNode!=null&&maxNode.val<=root.val) return false;
return traverse(root.left,minNode,root)&&traverse(root.right,root,maxNode);
}
}
在 BST 中搜索元素
力扣第 700 题「 二叉搜索树中的搜索」
很简单的二分思想:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root==null) return null;
if(val<root.val){
return searchBST(root.left,val);
}else if(val>root.val){
return searchBST(root.right,val);
}
return root;
}
}
在 BST 中插入一个数
对数据结构的操作无非遍历 + 访问,遍历就是「找」,访问就是「改」。具体到这个问题,插入一个数,就是先找到插入位置,然后进行插入操作。
上一个问题,我们总结了 BST 中的遍历框架,就是「找」的问题。直接套框架,加上「改」的操作即可。一旦涉及「改」,就类似二叉树的构造问题,函数要返回 TreeNode 类型,并且要对递归调用的返回值进行接收。
TreeNode insertIntoBST(TreeNode root, int val) {
// 找到空位置插入新节点
if (root == null) return new TreeNode(val);
// if (root.val == val)
// BST 中一般不会插入已存在元素
if (root.val < val)
root.right = insertIntoBST(root.right, val);
if (root.val > val)
root.left = insertIntoBST(root.left, val);
return root;
}
在 BST 中删除一个数
这个问题稍微复杂,跟插入操作类似,先「找」再「改」,先把框架写出来再说
TreeNode deleteNode(TreeNode root, int key) {
if (root.val == key) {
// 找到啦,进行删除
} else if (root.val > key) {
// 去左子树找
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
// 去右子树找
root.right = deleteNode(root.right, key);
}
return root;
}
找到目标节点了,比方说是节点 A,如何删除这个节点,这是难点。因为删除节点的同时不能破坏 BST 的性质。有三种情况,用图片来说明。
情况1: A 恰好是末端节点,两个子节点都为空,那么它可以当场去世了。
if (root.left == null && root.right == null)
return null;
情况 2:A 只有一个非空子节点,那么它要让这个孩子接替自己的位置。
if(root.left==null) return root.right;
if(root.right==null) return root.left;
情况 3:A 有两个子节点,麻烦了,为了不破坏 BST 的性质,A 必须找到左子树中最大的那个节点,或者右子树中最小的那个节点来接替自己。我们以第二种方式讲解。
if(root.left!=null&&root.right!=null){
TreeNode minNode=getMin(root.right);
root.val=minNode.val;
root.right=deleteNode(root.right,minNode.val);
}
三种情况分析完毕,填入框架,简化一下代码
TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.val == key) {
// 这两个 if 把情况 1 和 2 都正确处理了
if (root.left == null) return root.right;
if (root.right == null) return root.left;
// 处理情况 3
// 获得右子树最小的节点
TreeNode minNode = getMin(root.right);
// 删除右子树最小的节点
root.right = deleteNode(root.right, minNode.val);
// 用右子树最小的节点替换 root 节点
minNode.left = root.left;
minNode.right = root.right;
root = minNode;
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
}
return root;
}
TreeNode getMin(TreeNode node) {
// BST 最左边的就是最小的
while (node.left != null) node = node.left;
return node;
}
这样,删除操作就完成了。注意一下,上述代码在处理情况 3 时通过一系列略微复杂的链表操作交换 root 和 minNode 两个节点:
// 处理情况 3
// 获得右子树最小的节点
TreeNode minNode = getMin(root.right);
// 删除右子树最小的节点
root.right = deleteNode(root.right, minNode.val);
// 用右子树最小的节点替换 root 节点
minNode.left = root.left;
minNode.right = root.right;
root = minNode;
有的读者可能会疑惑,替换 root 节点为什么这么麻烦,直接改 val 字段不就行了?看起来还更简洁易懂:
仅对于这道算法题来说是可以的,但这样操作并不完美,我们一般不会通过修改节点内部的值来交换节点。因为在实际应用中,BST 节点内部的数据域是用户自定义的,可以非常复杂,而 BST 作为数据结构(一个工具人),其操作应该和内部存储的数据域解耦,所以我们更倾向于使用指针操作来交换节点,根本没必要关心内部数据.
最后总结一下吧,通过这篇文章,我们总结出了如下几个技巧:
- 如果当前节点会对下面的子节点有整体影响,可以通过辅助函数增长参数列表,借助参数传递信息。
- 在二叉树递归框架之上,扩展出一套 BST 代码框架:
void BST(TreeNode root, int target) {
if (root.val == target)
// 找到目标,做点什么
if (root.val < target)
BST(root.right, target);
if (root.val > target)
BST(root.left, target);
}
- 根据代码框架掌握了 BST 的增删查改操作。
边栏推荐
- hellogolang
- Tragedy caused by deleting the console statement
- JS modularization
- thinkphp3.2.3中设置路由,优化url
- [excelexport], Excel to Lua, JSON, XML development tool
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- Set the route and optimize the URL in thinkphp3.2.3
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- 修改配置文件后tidb无法启动
- three.js打造酷炫下雪效果
猜你喜欢

HAVE FUN | “飞船计划”活动最新进展

Xcode Revoke certificate

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."

1亿单身男女“在线相亲”,撑起130亿IPO

Tragedy caused by deleting the console statement

Balanced binary tree (AVL)

Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)
[vulnhub range] thales:1

SPI master RX time out interrupt

The unity vector rotates at a point
随机推荐
Vs tool word highlight with margin
js中复选框checkbox如何判定为被选中
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
ThinkPHP URL 路由简介
IP地址和物理地址有什么区别
Imitate the choice of enterprise wechat conference room
The unity vector rotates at a point
AutoLISP series (3): function function 3
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
AutoLISP series (1): function function 1
logback. XML configure logs of different levels and set color output
[summary of knowledge] summary of notes on using SVN in PHP
iptables只允许指定ip地址访问指定端口
记录Servlet学习时的一次乱码
Asyncio concept and usage
【Android -- 数据存储】使用 SQLite 存储数据
全网“追杀”钟薛高
logback.xml配置不同级别日志,设置彩色输出
Markdown formula editing tutorial
Tragedy caused by deleting the console statement