当前位置:网站首页>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;
}
}
}
边栏推荐
- Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
- MySQL common add, delete, modify and query operations (crud)
- MySQL常用增删改查操作(CRUD)
- 力扣刷题日记/day6/6.28
- Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
- 力扣刷题日记/day1/2022.6.23
- Pytoch deep learning environment construction
- Once the "king of color TV", he sold pork before delisting
- 机器学习概念漂移检测方法(Aporia)
- 78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
猜你喜欢

With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute

力扣刷题日记/day5/2022.6.27

力扣刷题日记/day7/6.30

Li Kou brush question diary /day3/2022.6.25

The controversial line of energy replenishment: will fast charging lead to reunification?

Imitation of numpy 2

Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)

The block:usdd has strong growth momentum

五千字讲清楚团队自组织建设 | Liga 妙谈

Stars open stores, return, return, return
随机推荐
LD_ LIBRARY_ Path environment variable setting
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
Self reflection of a small VC after two years of entrepreneurship
fopen、fread、fwrite、fseek 的文件处理示例
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
ITSS运维能力成熟度分级详解|一文搞清ITSS证书
TCP两次挥手,你见过吗?那四次握手呢?
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
俄罗斯 Arenadata 发布基于PostgreSQL的产品
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
力扣刷題日記/day6/6.28
Achieve animation effect through event binding
[209] go language learning ideas
Thawte通配符SSL证书提供的类型有哪些
Five thousand words to clarify team self-organization construction | Liga wonderful talk
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
未来几年中,软件测试的几大趋势是什么?
Load test practice of pingcode performance test
Machine learning concept drift detection method (Apria)