当前位置:网站首页>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;
}
}
}
边栏推荐
- [209] go language learning ideas
- 庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
- I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
- 力扣刷题日记/day3/2022.6.25
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
- 基于NCF的多模块协同实例
- 同事悄悄告诉我,飞书通知还能这样玩
- Wireshark抓包TLS协议栏显示版本不一致问题
- 激进技术派 vs 项目保守派的微服务架构之争
猜你喜欢

Li Kou brush question diary /day4/6.26

Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg

力扣刷题日记/day4/6.26

Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers

Stars open stores, return, return, return

Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组

Wireshark抓包TLS协议栏显示版本不一致问题

Halcon template matching
![[HCIA continuous update] WAN technology](/img/31/8e9ed888d22b15eda5ddcda9b8869b.png)
[HCIA continuous update] WAN technology
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
随机推荐
mysql5.7安装教程图文详解
Introduction of time related knowledge in kernel
Russia arena data releases PostgreSQL based products
SIGMOD’22 HiEngine论文解读
[210] usage of PHP delimiter
力扣刷题日记/day8/7.1
How to improve development quality
五千字讲清楚团队自组织建设 | Liga 妙谈
2022年DCMM认证全国各地补贴政策汇总
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
How to open an account is safe,
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
General environmental instructions for the project
上市公司改名,科学还是玄学?
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
被忽视的问题:测试环境配置管理
Android uses sqliteopenhelper to flash back
Wireshark抓包TLS协议栏显示版本不一致问题
【Hot100】31. Next spread