当前位置:网站首页>binary search tree
binary search tree
2022-07-30 01:51:00 【Gy648】
二叉搜索树
Also known as sorted binary tree(sorted binary tree),是指一棵空树或者具有下列性质的二叉树:
若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
任意节点的左、右子树也分别为二叉查找树;
特点:
All nodes on the left subtree have values less than the root node value
All nodes on the right subtree have values greater than the root node value
插入
迭代版本
We can use two pointers to find the previous position of the inserted position and insert it
bool Insert(const T &key) //迭代
{
//如果根节点为NULL
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node *cur = _root;
Node *prev = nullptr;
while (cur)
{
if (cur->_key < key)
{
prev = cur;
cur = cur->_left;
}
else if (cur->_key > key)
{
prev = cur;
cur = cur->_right;
}
else
{
return false; //若是返回fasleIt means that the number has been inserted
}
}
// preis the previous node to be inserted
//将cur放到prev 合适的位置
cur = new Node(key);
if (prev->_key > key)
{
prev->_left = cur;
}
else
{
prev->_right = cur;
}
return true;
}
- 递归
We can also use recursive notation
in tree operations,Understand the idea of pre-inorder traversal,Many recursive operations are well understood
bool InsertR(Node *&root, const T &key)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (root->_key > key)
{
return _InsertR(root->_left, key); //Recursively return to the smallest left subtree
}
else if (root->_key < key)
{
return _InsertR(root->_right, key);//Recursively return to the smallest left subtree
}
else
{
return false;
}
}
删除
- 要删除的节点无孩子节点
- The node to be deleted has only the left child node
- 要删除的节点只有右孩子节点
- The node to be deleted has left and right child nodes
If only the right child
if (cur->_left == nullptr)
{
if (cur == _root) //若是根节点
{
_root = cur->_right;
}
else
{
if (prev->_left == cur)
{
prev->_left = cur->_right;
}
else
{
prev->_right = cur->_right;
}
}
delete cur;
}
如果只有右孩子
else if (cur->_right == nullptr)
{
if (cur == _root)
{
_root = cur->_left;
}
else
{
if (prev->_left == cur)
{
prev->_left = cur->_left;
}
else
{
prev->_right = cur->_left;
}
}
delete cur;
}
既有左孩子又有右孩子
Node *maxleft = cur->_left;
while (maxleft->_right)
{
maxleft = maxleft->_right;
}
T max = maxleft->_key;
Erase(maxleft);
cur->_key = max;
查找
Based on the tree-building characteristics of sorted binary trees,二叉查找树相比于其他数据结构的优势在于查找、插入的时间复杂度较低.为{\displaystyle O(\log n)}O(\log n).But in extreme cases it may be inserted into a singly linked list,查找效率较低
Node *Find(const T &key)
{
Node *cur = _root;
while (cur)
{
if (key < cur->_key)
{
cur = cur->_left;
}
else if(key > cur->_key)
{
cur = cur->_right;
}
else
{
return cur;
}
}
return nullptr;
}
边栏推荐
- SwiftUI SQLite数据库存储使用教程大合集(2022年版)
- vscode 工作区配置插件 配置不同工作环境
- Performance Testing Theory 1 | Sorting out difficult problems in performance testing
- ROS2知识:编译系统ament_cmake
- flutter学习之widget的显示和隐藏
- 05.script_setup中的私有属性
- Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
- Fabric 编写案例 链码
- 实习经历梳理
- 综合设计一个OPPO页面--返回顶部使用--使用链接的锚点a+id
猜你喜欢

Running a Fabric Application

The role of interface testing

泰克Tektronix示波器软件TDS2012|TDS2014|TDS2022上位机软件NS-Scope

Towards Better Understanding of Self-Supervised Representations / Q-Score

Running a Fabric Application

帽式滑环的工作原理

多线程---初阶

利用ESP32构造一个ZIGBEE的网络发送转接

错误:“filesystem“ 不是 “std“ 的成员

图解LeetCode——593. 有效的正方形(难度:中等)
随机推荐
Fabric 编写案例 链码
Postgresql daily operation and maintenance skills, suitable for beginners
接口测试的作用
ufw 设置防火墙规则
CMake Tutorial Tour(0)_Overview
【Vmware NSX-V基本架构及组件安装】
What to test for app testing
MIT6.S081 小结
性能测试理论1 | 性能测试难点问题梳理
Interviews with big factories under the trend of layoffs: "ByteDance"
【SemiDrive源码分析】【MailBox核间通信】43 - 基于Mailbox IPCC RPC 实现核间通信(代码实现篇)
diff和key的作用
OSPF shamlink 解决后门链路问题
[VMWARE--Shared files]
接口测试自动化后起之秀-YApi接口管理平台
泰克Tektronix示波器软件TDS2012|TDS2014|TDS2022上位机软件NS-Scope
English语法_不定代词 -some & any
MPLS VPN跨域-optionC2
JS develops 3D modeling software
经济衰退时期的对比:如今更像历史上的哪段时期?