当前位置:网站首页>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
边栏推荐
- Deconstruction and assignment of variables
- Find the root of equation ax^2+bx+c=0 (C language)
- 1321: [example 6.3] deletion problem (noip1994)
- Network engineer test questions and answers in May of the first half of 2022
- mif 文件格式记录
- uniCloud
- What are the test preparation materials and methods for soft exam information processing technicians?
- The difference between monotonicity constraint and anti monotonicity constraint
- Monai version has been updated to 0.9. See what new functions it has
- [pro test feasible] error while loading shared libraries solution
猜你喜欢
JSON format query of MySQL
Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)
[untitled]
[recommendation system 02] deepfm, youtubednn, DSSM, MMOE
软考一般什么时候出成绩呢?在线蹬?
uniCloud
[OneNote] can't connect to the network and can't sync the problem
IDEA快捷键大全
SQL Server 知识汇集9 : 修改数据
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
随机推荐
Static semantic check of clang tidy in cicd
[recommendation system 02] deepfm, youtubednn, DSSM, MMOE
Qtcreator sets multiple qmake
[untitled]
想考中级软考,一般需要多少复习时间?
【pyqt】tableWidget里的cellWidget使用信号与槽机制
[untitled]
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
【C#】WinForm运行缩放(变糊)的解决方法
TypeScript 接口继承
Poj1821 fence problem solving Report
变量的解构赋值
Unity websocket client
Basic introduction of yarn and job submission process
Records on the use of easyflash v3.3
高级软考(网络规划设计师)该如何备考?
长列表性能优化方案 memo
一些线上学术报告网站与机器学习视频
[machine learning 03] Lagrange multiplier method
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)