当前位置:网站首页>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;
}
边栏推荐
- SSM integration case
- Typora transparent background image
- 泰克Tektronix示波器软件TDS520|TDS1001|TDS1002上位机软件NS-Scope
- 1.2Recyclerview实现Item点击事件
- RAII技术学习
- About offline use of SAP Fiori apps
- 利用ESP32构造一个ZIGBEE的网络发送转接
- Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
- mysql error is too long for user name (should be no longer than 16)
- npm ERR! code ENOTSUPnpm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
猜你喜欢
Elephant Swap:借助ePLATO提供加密市场的套利空间
RAII技术学习
泰克Tektronix示波器软件TDS520|TDS1001|TDS1002上位机软件NS-Scope
日期时间存入数据库会差一天?
Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
Type-C边充电边OTG芯片——LDR6028A
STM32L4R9ZIY6PTR STM32L4高性能嵌入式—MCU
flutter学习之widget的显示和隐藏
【笔记】结巴分词绘制词云图
matlab洗碗机节水模型的优化设计-这是个课题名称,不是买洗碗机,审核的人仔细看下,谢谢
随机推荐
RAII技术学习
anaconda打开闪退解决
记一次搭建conda虚拟环境
泰克Tektronix示波器软件TDS420|TDS430|TDS460上位机软件NS-Scope
错误:“filesystem“ 不是 “std“ 的成员
将镜像推送到阿里云私有仓库
绘图问题记录
Fabric Private Data Case
把@Transactional事务注解用到如此炉火纯青,真的强!
【LeetCode每日一题】——404.左叶子之和
mysql 报错 is too long for user name (should be no longer than 16)
The role of diff and key
my creative day
泰克Tektronix示波器软件TDS520|TDS1001|TDS1002上位机软件NS-Scope
MySQL高级篇(高阳)建表sql语句大全
tcp ip
ufw 设置防火墙规则
网络原理 基础知识
[深入研究4G/5G/6G专题-45]: 5G Link Adaption链路自适应-1-总体架构
go grpc 自定义拦截器