当前位置:网站首页>Deleting nodes in binary search tree
Deleting nodes in binary search tree
2022-07-04 18:36:00 【Wind energy thermal underwear】
List of articles
The deletion of binary search tree is cumbersome , But calm down and believe that it will soon be absorbed ! It is mainly divided into two steps : First, find the node to be deleted , Then delete this node .
We use it cur Variable search , use parent Variable representation curr The parent node of
Search for nodes to be deleted
Search is not the focus of this article , Let's go straight to the code
public boolean delete(int key) {
// wirte code here, There are mainly three situations
Node parent = null;
Node cur = root;//cur Be responsible for finding the node that needs to be deleted
while (cur != null) {
if (cur.key < key) {
parent = cur;
cur = cur.right;
} else if (cur.key > key) {
parent = cur;
cur = cur.left;
} else {
//△ Come here , We found the node to be deleted , namely cur
deleteNode(parent, cur);
//deleteNode The way is to delete cur node
return true;
}
}
return false;
}
Be careful : To the code above triangle It's about , We The node to be deleted is found cur
Text ( Delete cur node )
We have three situations :(cur The node is the node to be deleted ,parent yes cur The parent node of , It will not be repeated below )
- cur.left = null;
- cur.right = null;
- cur.left != null && cur.right != null;
situation 1:cur.left = null
This time, the situation is divided into 3 In this case
- cur yes The root node
- cur yes parent The left child
- cur yes parent The right child
① cur Root node
In this case , Let's go straight to root = root.right
that will do
② cur by parent The left child
Pictured , We let parent.left = cur.right
that will do
③ cur by parent The right child
Pictured , We let parent.right = cur.right
that will do
situation 2:cur.right = null
- situation 2 And circumstances 1 The idea is basically the same , Let's repeat and consolidate
- cur yes The root node
- cur yes parent The left child
- cur yes parent The right child
① cur Root node
Empathy , just root = root.left
that will do
② cur by parent The left child
Pictured , We let parent.left = cur.left
③ cur by parent The right child
Pictured , We let parent.right = cur.left
situation 3:cur.left != null && cur.right != null
The core idea : stay cur Find the node with the lowest value in the right subtree of , Then assign the node value to cur , Then delete the smallest node
- So there are two steps :① look for cur Right subtree minimum node , And assign the minimum value to cur ② Delete the smallest node
step ①
First define target = cur.right, let targetParent by target The parent node of
Final : Give Way target Point to cur The smallest node of the right subtree , Give Way targetParent Point to target My parents
following ↓ The code is to make target Point to cur Right subtree The minimum node
Node target = cur.right;// find cur The smallest node in the right subtree , Replace cur node , And delete the smallest node
Node targetParent = cur; //target Parent node of node
while (target.left != null) {
// Find the smallest node of the right subtree
targetParent = target;
target = target.left;
}
And then assign a value :cur.key = target.key;
, Pictured
step ②
Here we are , Our task is to delete target The node , At this time, it can be divided into two situations , It's also easier to understand
- target == targetParent.left
- tartget == targetParent.right
situation 1 : Only targetParent.left = target.right
situation 2: Only targetParent.right = target.right
complete !
Delete cur Node code
private void deleteNode(Node parent, Node cur) {
// thus ,cur Point to the node to be deleted
if (cur.left == null) {
// situation 1, If the left child is empty
if (cur == root) {
root = cur.right;
} else if (parent.left == cur) {
parent.left = cur.right;
} else {
parent.right = cur.right;
}
} else if (cur.right == null) {
// situation 2, If the right child is empty
if (cur == root) {
root = cur.left;
} else if (parent.left == cur) {
parent.left = cur.left;
} else {
parent.right = cur.left;
}
} else {
// situation 3, If the left and right children are not empty
Node target = cur.right;// find cur The smallest node in the right subtree , Replace cur Node values , And delete the smallest node
Node targetParent = cur; //target Parent node of node
while (target.left != null) {
// Find the smallest node of the right subtree
targetParent = target;
target = target.left;
}
cur.key = target.key; // Cover cur value
if (target == targetParent.left) {
targetParent.left = target.right;
} else {
targetParent.right = target.right;
}
}
}
Full code
public boolean delete(int key) {
// wirte code here, There are mainly three situations
Node parent = null;
Node cur = root;//cur Be responsible for finding the node that needs to be deleted
while (cur != null) {
if (cur.key < key) {
parent = cur;
cur = cur.right;
} else if (cur.key > key) {
parent = cur;
cur = cur.left;
} else {
deleteNode(parent, cur);
return true;
}
}
return false;
}
private void deleteNode(Node parent, Node cur) {
// thus ,cur Point to the node to be deleted
if (cur.left == null) {
// If the left child is empty
if (cur == root) {
root = cur.right;
} else if (parent.left == cur) {
parent.left = cur.right;
} else {
parent.right = cur.right;
}
} else if (cur.right == null) {
// If the right child is empty
if (cur == root) {
root = cur.left;
} else if (parent.left == cur) {
parent.left = cur.left;
} else {
parent.right = cur.left;
}
} else {
// If the left and right children are not empty
Node target = cur.right;// find cur The smallest node in the right subtree , Replace cur node , And delete the smallest node
Node targetParent = cur; //target Parent node of node
while (target.left != null) {
// Find the smallest node of the right subtree
targetParent = target;
target = target.left;
}
cur.key = target.key; // Cover
if (target == targetParent.left) {
targetParent.left = target.right;
} else {
targetParent.right = target.right;
}
}
}
边栏推荐
- 【系统盘转回U盘】记录系统盘转回U盘的操作
- 一、C语言入门基础
- [go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
- Pytoch deep learning environment construction
- 如何使用 wget 和 curl 下载文件
- 力扣刷题日记/day8/7.1
- Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
- Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
- 力扣刷题日记/day4/6.26
- 【209】go语言的学习思想
猜你喜欢
输入的查询SQL语句,是如何执行的?
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
为啥有些线上演唱会总是怪怪的?
一、C语言入门基础
被忽视的问题:测试环境配置管理
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Digital "new" operation and maintenance of energy industry
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
如何提高开发质量
随机推荐
vbs或vbe如何修改图标
Imitation of numpy 2
力扣刷題日記/day6/6.28
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
五千字讲清楚团队自组织建设 | Liga 妙谈
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
Introduction of time related knowledge in kernel
力扣刷题日记/day8/7.1
中国农科院基因组所汪鸿儒课题组诚邀加入
Wireshark抓包TLS协议栏显示版本不一致问题
力扣刷题日记/day1/2022.6.23
一、C语言入门基础
File processing examples of fopen, FREAD, fwrite, fseek
[210] usage of PHP delimiter
[209] go language learning ideas
Redis主从复制
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Large scale service exception log retrieval
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用