当前位置:网站首页>二叉搜索树
二叉搜索树
2022-07-06 10:41:00 【m0_62406299】
1. 内容安排说明
二叉树在前面C数据结构阶段已经讲过,本节取名二叉树进阶是因为:
1. map和set特性需要先铺垫二叉搜索树,而二叉搜索树也是一种树形结构
2. 二叉搜索树的特性了解,有助于更好的理解map和set的特性
3. 二叉树中部分面试题稍微有点难度,在前面讲解大家不容易接受,且时间长容易忘
4. 有些OJ题使用C语言方式实现比较麻烦
2. 二叉搜索树
2.1 二叉搜索树概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
它的左右子树也分别为二叉搜索树

2.2 二叉搜索树操作
1. 二叉搜索树的查找

2. 二叉搜索树的插入
插入的具体过程如下:
a. 树为空,则直接插入

b. 树不空,按二叉搜索树性质查找插入位置,插入新节点

3. 二叉搜索树的删除
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程如下:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
情况d:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题
2.3 二叉搜索树的实现
template<class T>
struct BSTNode
{
BSTNode(const T& data = T())
: _pLeft(nullptr) , _pRight(nullptr), _data(data)
{}
BSTNode<T>* _pLeft;
BSTNode<T>* _pRight;
T _data;
};
template<class T>
class BSTree
{
typedef BSTNode<T> Node;
typedef Node* PNode;
public:
BSTree(): _pRoot(nullptr)
{}
// 同学们自己实现,与二叉树的销毁类似
~BSTree();
// 根据二叉搜索树的性质查找:找到值为data的节点在二叉搜索树中的位置
PNode Find(const T& data);
bool Insert(const T& data)
{
// 如果树为空,直接插入
if (nullptr == _pRoot)
{
_pRoot = new Node(data);
return true;
}
// 按照二叉搜索树的性质查找data在树中的插入位置
PNode pCur = _pRoot;
// 记录pCur的双亲,因为新元素最终插入在pCur双亲左右孩子的位置
PNode pParent = nullptr;
while (pCur)
{
pParent = pCur;
if (data < pCur->_data)
pCur = pCur->_pLeft;
else if (data > pCur->_data)
pCur = pCur->_pRight; // 元素已经在树中存在
else
return false;
}
// 插入元素
pCur = new Node(data);
if (data < pParent->_data)
pParent->_pLeft = pCur;
else
pParent->_pRight = pCur;
return true;
}
bool Erase(const T& data)
{
// 如果树为空,删除失败
if (nullptr == _pRoot)
return false;
// 查找在data在树中的位置
PNode pCur = _pRoot;
PNode pParent = nullptr;
while (pCur)
{
if (data == pCur->_data)
break;
else if (data < pCur->_data)
{
pParent = pCur;
pCur = pCur->_pLeft;
}
else
{
pParent = pCur;
pCur = pCur->_pRight;
}
}
// data不在二叉搜索树中,无法删除
if (nullptr == pCur)
return false;
// 分以下情况进行删除,同学们自己画图分析完成
if (nullptr == pCur->_pRight)
{
// 当前节点只有左孩子或者左孩子为空---可直接删除
}
else if (nullptr == pCur->_pRight)
{
// 当前节点只有右孩子---可直接删除
}
else
{
// 当前节点左右孩子都存在,直接删除不好删除,可以在其子树中找一个替代结点,比如:
// 找其左子树中的最大节点,即左子树中最右侧的节点,或者在其右子树中最小的节点,即
右子树中最小的节点
// 替代节点找到后,将替代节点中的值交给待删除节点,转换成删除替代节点
}
return true;
}
//自己实现
void InOrder();
private:
PNode _pRoot;边栏推荐
- 转载:基于深度学习的工业品组件缺陷检测技术
- This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
- AFNetworking框架_上传文件或图像server
- Rb157-asemi rectifier bridge RB157
- Coco2017 dataset usage (brief introduction)
- Prophet模型的简介以及案例分析
- Virtual machine VirtualBox and vagrant installation
- Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
- 【剑指 Offer】 60. n个骰子的点数
- 图片缩放中心
猜你喜欢

Declval of template in generic programming

Penetration test information collection - WAF identification

FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog

2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目

Recommend easy-to-use backstage management scaffolding, everyone open source

Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery

287. Find duplicates

2019 Alibaba cluster dataset Usage Summary

Declval (example of return value of guidance function)

巨杉数据库首批入选金融信创解决方案!
随机推荐
C语言自动预订飞机票问题
Picture zoom Center
Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
2022/02/12
None of the strongest kings in the monitoring industry!
Wchars, coding, standards and portability - wchars, encodings, standards and portability
Implementation of queue
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
Brief description of SQL optimization problems
44所高校入选!分布式智能计算项目名单公示
DOM简要
Example of implementing web server with stm32+enc28j60+uip protocol stack
Penetration test information collection - WAF identification
【剑指 Offer】 60. n个骰子的点数
Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
Automatic reservation of air tickets in C language
C language exchanges two numbers through pointers
2022暑期项目实训(三)
Grafana 9.0 正式发布!堪称最强!