当前位置:网站首页>二叉搜索树
二叉搜索树
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;边栏推荐
- 使用cpolar建立一个商业网站(1)
- Implementation of queue
- Test 123
- 首先看K一个难看的数字
- How does crmeb mall system help marketing?
- 测试1234
- 巨杉数据库首批入选金融信创解决方案!
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- Jerry is the custom background specified by the currently used dial enable [chapter]
- 从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
猜你喜欢

Blue Bridge Cup real question: one question with clear code, master three codes

图之广度优先遍历

SQL injection - access injection, access offset injection

None of the strongest kings in the monitoring industry!

Maixll dock camera usage

Some understandings of tree LSTM and DGL code implementation

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

Why does wechat use SQLite to save chat records?

F200 - UAV equipped with domestic open source flight control system based on Model Design

Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
随机推荐
A method of sequentially loading Unity Resources
Interpreting cloud native technology
Splay
Declval (example of return value of guidance function)
2022 Summer Project Training (III)
Huawei 0 foundation - image sorting
【LeetCode第 300 场周赛】
徐翔妻子应莹回应“股评”:自己写的!
小程序在产业互联网中的作用
std::true_ Type and std:: false_ type
测试123
Five data structures of redis
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
Penetration test information collection - site architecture and construction
Virtual machine VirtualBox and vagrant installation
UDP协议:因性善而简单,难免碰到“城会玩”
C language exchanges two numbers through pointers
Maixll-Dock 摄像头使用
Cobra 快速入门 - 专为命令行程序而生