当前位置:网站首页>The concept, implementation and analysis of binary search tree (BST)
The concept, implementation and analysis of binary search tree (BST)
2022-07-07 11:06:00 【Exy-】
Catalog
BST The deletion of ( important )
Binary search tree
Concept
Binary search tree is also called binary sort tree , It could be an empty tree , Or a binary tree with the following properties :
- If its left subtree is not empty , Then the values of all nodes on the left subtree are smaller than the values of the root nodes
- If its right subtree is not empty , Then the value of all nodes on the right subtree is greater than the value of the root node
- Its left and right subtrees are also binary search trees
int ar[]={5,3,2,4,8,9,1};
BST Insertion of trees
- If the tree is empty, it will be inserted directly ;
- If it's not empty , Then insert according to the rules of binary search tree .
bool Insert(const K& key, const V& value)
{
if (_root == nullptr)
{
_root = new Node(key, value);
return true;
}
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (cur->_key > parent->_key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
BST Tree search
If the root node _root->_key== You're looking for key Return the found node
If the root node _root->_key> You're looking for key Look on its left sub tree
If the root node _root->_key< You're looking for key Look on its right subtree
If the root node does not exist , Return null pointer
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
BST The deletion of ( important )
First of all, according to the key Find nodes , No return found false;
There are three cases of deletion
1. The left subtree of the node to be deleted is empty
- The root node to delete , Let the root be equal to the right child node
- The left child node that is an ordinary node and its parent node to be deleted is parent->_left = cur->_right
- The right child node that is an ordinary node and its parent node to be deleted is parent->_right= cur->_right
2. The right subtree of the node to be deleted is empty
- The root node to delete , Let the root be equal to the left child node
- The left child node that is an ordinary node and its parent node to be deleted is parent->_left = cur->_left;
- The left child node that is an ordinary node and its parent node to be deleted is parent->_right = cur->_left;
3. The left and right child nodes of the deleted node are not empty :
We use the substitution method here
Find the largest node of the left subtree ( The most right ) Or the smallest node of the right subtree
The code is as follows :
bool Erase(const K& key)
{
//1. Looking for nodes
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
if (cur->_left == nullptr)
{
if (parent == nullptr)// The root node to delete
{
_root = cur->_right;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
else if (cur->_right == nullptr)
{
if (parent == nullptr)
{
_root = cur->_left;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
else
{
// The substitution method deletes The largest node of the left tree ( The rightmost node ) Or the smallest node of the right tree ( Leftmost node )
Node* Parent = cur; // Be careful not to initialize null
Node* minNode = cur->_right;
while (minNode->_left)
{
Parent = minNode;
minNode = minNode->_left;
}
swap(cur->_key, minNode->_key);
// Convert to delete minNode
// because minNode As an empty node , You can delete
if (Parent->_right == minNode)
Parent->_right = minNode->_right;
else
Parent->_left = minNode->_right;
delete minNode;
}
return true;
}
}
return false;
}
application
according to BST We can realize the characteristics of trees
Performance analysis


边栏推荐
- [untitled]
- [pyqt] the cellwidget in tablewidget uses signal and slot mechanism
- 想考中级软考,一般需要多少复习时间?
- Unable to open kernel device '\.\vmcidev\vmx': operation completed successfully. Reboot after installing vmware workstation? Module "devicepoweron" failed to start. Failed to start the virtual machine
- The eighth training assignment
- 简单易修改的弹框组件
- Network engineer test questions and answers in May of the first half of 2022
- 1323: [example 6.5] activity selection
- Arduino board description
- Mpx 插件
猜你喜欢
[untitled]
Shardingsphere sub database and table examples (logical table, real table, binding table, broadcast table, single table)
Basic introduction of yarn and job submission process
"Dream Cup" 2017 Jiangsu information and future primary school summer camp it expert PK program design questions
[pro test feasible] error while loading shared libraries solution
Find the greatest common divisor and the least common multiple (C language)
Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
[OneNote] can't connect to the network and can't sync the problem
[STM32] actual combat 3.1 - drive 42 stepper motors with STM32 and tb6600 drivers (I)
How much review time does it usually take to take the intermediate soft exam?
随机推荐
SQL Server 知识汇集11 : 约束
[untitled]
PR Lecture Notes
[système recommandé 01] rechub
Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
【亲测可行】error while loading shared libraries的解决方案
Cmake learning manual
What are the contents of the intermediate soft test, the software designer test, and the test outline?
Deep understanding of Apache Hudi asynchronous indexing mechanism
CSAPP bomb lab parsing
Qtcreator sets multiple qmake
P1223 queuing for water /1319: [example 6.1] queuing for water
Typescript interface inheritance
P2788 math 1 - addition and subtraction
软考中级有用吗??
The gun startles the dragon, and the crowd "locks" Zhou Zhi
[untitled]
深入理解Apache Hudi异步索引机制
Go Slice 比较