当前位置:网站首页>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;
}
}
}
边栏推荐
- How to open an account is safe,
- Self reflection of a small VC after two years of entrepreneurship
- 蓝桥:合根植物
- [HCIA continuous update] WAN technology
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- Is it safe to open an account online? is that true?
- Implementation of shell script replacement function
- 【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
- File processing examples of fopen, FREAD, fwrite, fseek
- Five thousand words to clarify team self-organization construction | Liga wonderful talk
猜你喜欢
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
力扣刷题日记/day5/2022.6.27
Halcon template matching
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Grain Mall (I)
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
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)
随机推荐
Thawte通配符SSL证书提供的类型有哪些
General environmental instructions for the project
激进技术派 vs 项目保守派的微服务架构之争
Interview summary of large factory Daquan II
Russia arena data releases PostgreSQL based products
How to download files using WGet and curl
[system disk back to U disk] record the operation of system disk back to U disk
力扣刷题日记/day3/2022.6.25
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
Interpretation of SIGMOD '22 hiengine paper
Five thousand words to clarify team self-organization construction | Liga wonderful talk
力扣刷题日记/day8/7.1
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
爬虫初级学习
为啥有些线上演唱会总是怪怪的?
Redis master-slave replication
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
ITSS运维能力成熟度分级详解|一文搞清ITSS证书