当前位置:网站首页>Detailed description of binary search tree
Detailed description of binary search tree
2022-07-27 05:08:00 【Code loving students】
Catalog
1.1 The concept of binary search tree
1.2 Binary search tree operation
1.2.2 The insertion of a binary tree
1.2.3 The deletion of binary trees
1. Binary search tree
1.1 The concept of binary search tree
Binary search tree is also called Binary sort tree , It could be a Empty tree , It may also be a binary tree that satisfies the following characteristics :
- If its left subtree is not empty , Then the value on all nodes of the left subtree is less than the value on the root node
- If its right subtree is not empty , Then the value on all nodes of the right subtree is greater than the value on the root node
- Its left and right subtrees also meet the above characteristics

This is a standard binary search tree
1.2 Binary search tree operation
1.2.1 Binary tree search
Implementation steps :
- Start from the root node and find , If the current value is larger than the target value , Then search on the right subtree
- If the current value is smaller than the target value , Then search on the left subtree
- If the current value is equal to the target value , Then return to true
- If the empty node is still not found , Then return to false
Let's see how the code is implemented :
1. Non recursive writing
bool Find(const K& val)
{
// from root Start looking for
Node* cur = root;
// If so cur It's empty , If it is still not found, it returns false
while (cur)
{
// If it is smaller than the current value, search in the left subtree
if (cur->val > val)
{
cur = cur->left;
}
// If it is larger than the current value, search in the right subtree
else if (cur->val < val)
{
cur = cur->right;
}
// If it is equal, it will return to true
else
{
return true;
}
}
return false;
}2. Recursive writing
Because parameters cannot be passed directly within a class , So it encapsulates a layer of functions
bool FindR(const K&val)
{
return _FindR(root,val);
}
bool _FindR(Node*& root, const K& val)
{
// If root If it is empty, it means that , Returns an empty
if (!root)
{
return false;
}
// If it is less than the template value , Then search in the left subtree
if (root->val < val)
{
return _FindR(root->right, val);
}
// If it is greater than the template value , Then search in the right subtree
else if (root->val > val)
{
return _FindR(root->left, val);
}
// If it is equal to the target value , Then return to true
else
{
return true;
}
}1.2.2 The insertion of a binary tree
Implementation steps :
- If the tree is empty , Then assign the inserted node directly to root
- If the tree is not empty , Then find the insertion position according to the nature of the binary search tree

If we need to insert 9 This node , So what are our steps ?
- Start from the root node and find , If the current value is less than 9 Be big , Then search on the right subtree
- If the current value is less than 9 smaller , Then search on the left subtree
- If the current value is equal to 9, Then return to false( The current setting does not allow inserting the same element )
- If you reach the empty node , Then this is the position to insert
How is our code implemented ?
1. Non recursive writing
bool insert(const K& val)
{
// If it's an empty tree
if (!root)
{
root = new Node(val);
return true;
}
// If it's not an empty tree
else
{
// You need to create a parent node to link the inserted node
Node* parent = nullptr;
Node* cur = root;
while (cur)
{
if (cur->val < val)
{
parent = cur;
cur = cur->right;
}
else if (cur->val > val)
{
parent = cur;
cur = cur->left;
}
else
{
return false;
}
}
//cur Went to the empty node , That is where we want to insert
Node* newnode = new Node(val);
// You also need to determine whether you are inserting a left subtree or a right subtree
if (parent->val < val)
{
parent->right = newnode;
}
else
{
parent->left = newnode;
}
return true;
}
}2. Recursive writing
bool InsertR(const K& val)
{
return _InsertR(root, val);
}
bool _InsertR(Node*& root, const K& val)
{
// If it is empty , Is the insertion position
if (!root)
{
root = new Node(val);
return true;
}
// If the current value is less than the target value , Then find the position in the left subtree
if (root->val < val)
{
return _InsertR(root->right, val);
}
// If the current value is greater than the target value , Then find the position in the right subtree
else if (root->val > val)
{
return _InsertR(root->left, val);
}
// If the current value is equal to the target value , return false
else
{
return false;
}
}
Different from ordinary non recursive writing , There is no need to decide whether to insert the left subtree or the right subtree

If we insert it recursively 9 This value , We will find that 9 The last place to insert is 10 The left side of this node , Again because root Of 10 Of Alias of the left node , So we just need to put root Value assignment of 9 This node can
1.2.3 The deletion of binary trees
Implementation steps :
- First, find out whether the element is in the binary search tree , If it doesn't exist , Then return to false,
- Otherwise, the nodes to be deleted may be divided into the following four situations :
- The node to be deleted has no children
- Only the left child node is to be deleted
- Only the right child node is to be deleted
- The node to be deleted has left 、 Right child node
We can find that when there are no child nodes, this situation can also be summarized in the case of only left nodes or right nodes
Then the code implementation is as follows :
1. Non recursive writing
bool erase(const K& val)
{
Node* cur = root;
Node* parent = nullptr;
while (cur)
{
if (cur->val < val)
{
parent = cur;
cur = cur->right;
}
else if (cur->val > val)
{
parent = cur;
cur = cur->left;
}
// If the target value is found
else
{
// Record the deleted nodes
Node* del = cur;
if (!cur->left)
{
if (!parent)
{
cur = cur->right;
}
else
{
if (parent->left == cur)
{
parent->left = cur->right;
}
else
{
parent->right = cur->right;
}
}
delete del;
}
else if (!cur->right)
{
if (!parent)
{
cur = cur->left;
}
else
{
if (parent->left == cur)
{
parent->left = cur->left;
}
else
{
parent->right = cur->left;
}
}
delete del;
}
// Left and right subtrees are not empty
else
{
Node* minparent = cur;
Node* minright = cur->right;
// Find the minimum value of the right subtree
while (minright->left)
{
minparent = minright;
minright = minright->left;
}
// Exchange the size of the minimum value and the target value
swap(cur->val, minright->val);
if (minparent->left == minright)
{
minparent->left = minright->right;
}
else
{
minparent->right = minright->right;
}
// Delete the replaced node
delete minright;
}
return true;
}
}
return false;
}2. Recursive writing
bool EraseR(const K& val)
{
return _EraseR(root, val);
}
bool _EraseR(Node*& root, const K& val)
{
// If it is empty , Then the target value does not exist , return false
if (!root)
{
return false
}
if (root->val < val)
{
return _EraseR(root->right, val);
}
else if (root->val > val)
{
return _EraseR(root->left, val);
}
// Find the target node
else
{
// If the left subtree is empty
if (!root->left)
{
root = root->right;
}
else if (!root->right)
{
root = root->left;
}
else
{
Node* minright = root->right;
while (minright->left)
{
minright = minright->left;
}
swap(root->val, minright->val);
return _EraseR(root->right, minright->val);
}
}
}summary
The difference between recursive and non recursive writing :
- Recursive writing does not need to judge whether the target value is the left subtree or the right subtree of its parent value in the delete and insert operations
- The outstanding thing about recursive writing is the small amount of code
- If the binary search tree is not uniform , Recursive writing may cause stack overflow
边栏推荐
- 探寻通用奥特能平台安全、智能、性能的奥秘!
- MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
- 二维数组求和 练习
- QT menu bar, toolbar and status bar
- 项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
- STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding
- 微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”
- Network protocol details: IP
- 事件总结-常用总结
- Counting Nodes in a Binary Search Tree
猜你喜欢

Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem

Sunset red warm tone tinting filter LUTS preset sunset LUTS 1

Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)

2019 top tennis cup upload

Advantages of smart exhibition hall design and applicable industry analysis
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

TypeScript 详解

ssm框架整合

The digital China Construction Summit ended with a list of massive pictures on site!
![[search] - multi source BFS + minimum step model](/img/42/11b5b89153ab48d837707988752268.png)
[search] - multi source BFS + minimum step model
随机推荐
Another skill is to earn 30000 yuan a month+
Sunset red warm tone tinting filter LUTS preset sunset LUTS 1
STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding
"Photoshop2021 introductory tutorial" flatten "perspective images
节流函数的demo——正则表达式匹配
【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
听过最自律的一句话: 那些我难以言表 不作声响
Plato farm is expected to further expand its ecosystem through elephant swap
vim的基本操作
Idea 如何新建一个groovy的项目(图文详细解释)
Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
How to test the payment process?
Customize the viewport height, and use scrolling for extra parts
Test basis 5
Three paradigms, constraints, some keyword differences,
【Acwing】第61场周赛 题解
1、 MySQL Foundation
对话框简介
Advantages of smart exhibition hall design and applicable industry analysis
2、 MySQL advanced