前言
二叉树在前面C数据结构阶段我们已经简单学过了,这里我们继续学习二叉树的进阶内容,搜索二叉树:
- map和set特性需要先铺垫二叉搜索树,而二叉搜索树也是一种树形结构
- 二叉搜索树的特性了解,有助于更好的理解map和set的特性
所以本章学习二叉搜索树,是对二叉树部分进行收尾总结。
一、二叉搜索树
1. 二叉搜索树概念
二叉搜索树又称二叉排序树,或者是一棵空树。
搜索二叉树的每一棵子树都满足以下性质:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
为什么他叫搜索二叉树?——》顾名思义,搜索二叉树就是方便查找,左边小右边大,最多找高度次。
最多找高度次:不要理想化的把它认为就是满二叉树的高度O(logN),但也有一种最坏的情况斜树的高度O(N)
对于这种最坏的情况,解决方案是AVL树和红黑树,他们就是找O(logN)
2. 二叉搜索树实现
tip:为什么喜欢叫二叉搜索树,不叫搜索二叉树——》我们将BinarySearchTree简写就知道了
//这里二叉搜索树的模版参数我们喜欢用K,因为插入的值喜欢叫关键字——插入的时候要做比较
template<class K>
class BinarySearchTreeNode
{};
template<class K>
class BinarySearchTree
{};
//名字太长了了我们简写一下,就发现我们为什么写二叉搜索树
template<class K>
class BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
,_right(nullptr)
,_key(key)
{}
};
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
private:
Node* _root;
public:
BSTree()
:_root(nullptr)
{}
};
2.1 二叉搜索树的前中后序
- 前序:根左右
- 特点:前序先访问根,可以确保树的完整结构信息
- 应用:所以使用前序来完成搜索二叉树深拷贝
- 中序:左根右
- 特点:二叉搜索树也称为二叉排序树,因为它左小右大的性质,我们按照中序遍历(左根右),就可以得到一个按升序排列的结果。
- 应用:验证搜索二叉树的有效性
- 后序:左右根
- 特点:先处理子节点再处理父节点,在删除节点时可以避免内存泄漏
- 应用:所以使用中序来完成搜索二叉树的析构(释放)
二叉搜索树前中序的递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()
:_root(nullptr)
{}
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
//中序:左根右
//递归调用要传类自己的成员变量作为参数,所以我们再封装一层
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//析构
~BSTree()
{
Destroy(_root);
}
private:
//前序:根左右
Node* Copy(Node* root)
{
if (nullptr == root)
return nullptr;
Node* copyroot = new Node(root->_key);
copyroot->_left = Copy(root->_left);
copyroot->_right = Copy(root->_right);
return copyroot;
}
//中序
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
//后序:左右根
void Destroy(Node*& root)
{
if (nullptr == root)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
Node* _root;
};
tip:递归调用要传类自己的成员变量作为参数,所以我们再封装一层
2.2 二叉搜索树的插入
插入过程具体如下:
- 树为空,则直接新增节点,赋值给root指针。
- 树不为空,按二叉搜索树性质查找插入位置,插入新节点。

二叉搜素数插入的非递归实现:
插入非递归实现
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
private:
Node* _root;
public:
//插入
bool Insert(const K& key)
{
//空树:直接插入
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
//非空树:左小右大(注已经有了就不插入)
Node* cur = _root;
Node* parent = cur;
//找插入的位置(到空即为插入的位置)
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if(cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
//创建结点并链接
cur = new Node(key);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
};
注意:二叉搜索树不会插入重复数据,即已经有了就不插入
二叉搜素数插入的递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
//递归:插入
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
private:
bool _InsertR(Node*& root, const K& key)
{
//走到空,到了插入的位置
if (nullptr == root)
{
root = new Node(key);
return true;
}
if (root->_key < key)
{
return _InsertR(root->_right, key);
}
else if (root->_key > key)
{
return _InsertR(root->_left, key);
}
else
{
//已经存在了,不要重复插入
return false;
}
}
Node* _root;
};
非递归实现:需要parent指针保存插入节点的父节点,用于链接
递归实现:我们root递归到空,就找到插入位置了!但我们不需要知道parent,因为root使用了引用,root就是插入位置的别名,所以天然就是链接,直接创建结点即可!即root是别名,修改别名就修改了自身,所以我们不需要再去找父节点
注意:循环里不可以使用引用,因为C++引用不能改变指向;递归可以是因为每一次递归都会创建栈帧,在栈帧里面创建新的引用
2.3 二叉搜索树的查找
查找分为两种情况:空树和非空树
- 情况一:空树 ——》直接返回false即可
- 情况二:非空树
- 从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找,即左小右大。
- 最多查找高度次,走到空,还没有找到,这个值不存在。
二叉搜索树查找的非递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
//查找
bool Find(const K& key)
{
//空树
if (_root == nullptr)
{
return false;
}
//非空树:左小右大
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return true;
}
}
return false;
}
private:
Node* _root;
};
二叉搜索树查找的递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
//递归:查找
bool FindR(const K& key)
{
return _FindR(_root, key);
}
private:
bool _FindR(Node* root, const K& key)
{
//走到空树,说明没有查找到
if (root == nullptr)
return false;
if (root->_key < key)
{
return _FindR(root->_right, key);
}
else if (root->_key > key)
{
return _FindR(root->_left, key);
}
else
{
return true;
}
}
Node* _root;
};
2.4 二叉搜索树的删除
首先查找元素是否在二叉搜索树中,如果不存在,则直接返回false,如果存在,待删除结点有如下四种情况:
- 待删除结点没有孩子
- 待删除结点只有左孩子
- 待删除结点只有右孩子
- 待删除结点有两个孩子
前三种情况都好删除,都是把孩子给它父亲,即托孤!所以情况1可以和情况2/情况3合并。
第四种情况就不好删除了,直接把孩子给父亲,可能不满足二叉树的性质,所以使用替换法——》将待删除结点与左子树的最大结点(即最右结点)或者右子树的最小结点(即最左结点)交换。
归纳:最后删除情况有3种,具体思路如下
- 待删结点的左孩子为空(包含了没有孩子的情况)

待删除结点的左子树为空:
- 判断链接的方向——即删除结点是在父节点的左还是右
- 让父节点链接待删除结点的右孩子(如果待删除结点在父节点的左边则父节点的左孩子链接,反之在父节点的右边则父节点的右孩子链接)——即托孤
- 最后释放删除结点
- 待删除结点的右子树为空

待删除结点的右子树为空:
- 判断链接的方向
- 让父节点链接待删除结点的左孩子
- 最后释放
注意:
情况1和2有一个特殊情况:待删除结点为根节点时,直接让根节点指向待删除结点的孩子
- 待删除结点有两个孩子

待删除结点有两个孩子,使用替换法进行删除(将待删除结点与左子树的最大结点(即最右结点)或者右子树的最小结点(即最左结点)交换)
- 找替代结点:这里我们找左子树的最大节点,即最右结点
- 交换:待删除结点的值与找到的替换节点的值交换
- 删除替代结点:替代结点最多只有一个孩子,所以只需要把替代结点的孩子托孤给它父亲即可
注意:替代结点的父亲初始化不能为空,若刚好左子树的根结点就是最大节点的情况,就会出现bug
二叉搜索树删除的非递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
//删除
bool Erase(const K& key)
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
//找到了
//情况1:待删结点的左孩子为空(包含了没有孩子的情况)
if (cur->_left == nullptr)
{
//再判断链接的方向——即删除结点是在父节点的左还是右
if (cur == _root)
{
//特殊:删除结点是根结点
_root = cur->_right;
}
else
{
//删除结点在父节点右边,则父节点的右边链接
if (parent->_right == cur)
{
parent->_right = cur->_right;
}
else
{
parent->_left = cur->_right;
}
}
}
//情况2:待删结点的右孩子为空
else if (cur->_right == nullptr)
{
//再判断链接的方向
if (cur == _root)
{
_root = cur->_left;
}
else
{
if (parent->_right == cur)
{
parent->_right = cur->_left;
}
else
{
parent->_left = cur->_left;
}
}
}
//有两个孩子——替换法:左子树的最大节点,或右子树的最小结点
else
{
//找替换结点:找左子树的最大节点(即最右结点)
Node* parent = cur;//注意不能为空,刚好左子树的根结点就是最大节点的情况
Node* leftMax = cur->_left;
while (leftMax->_right)
{
parent = leftMax;
leftMax = leftMax->_right;
}
swap(cur->_key, leftMax->_key);
//托孤:将替换节点的孩子给它父亲
if (parent->_left == leftMax)
{
parent->_left = leftMax->_left;
}
else
{
parent->_right = leftMax->_left;
}
cur = leftMax;
}
//释放
delete cur;
return true;
}
}
//走到空,找不到,返回false
return false;
}
private:
Node* _root;
};
二叉搜索树删除的递归实现:
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
private:
bool _EraseR(Node*& root, const K& key)
{
//走到空,说明没有key
if (nullptr == root)
return false;
if (root->_key < key)
{
_EraseR(root->_right, key);
}
else if (root->_key > key)
{
_EraseR(root->_left, key);
}
else
{
//root是别名,修改别名就修改了自身,所以我们不需要再去找父节点
//但注意:保住修改之前的root,一会释放,即先托孤完成了,再释放
Node* del = root;
//1、左为空
if (nullptr == root->_left)
{
root = root->_right;
}
//2、右为空
else if (nullptr == root->_right)
{
root = root->_left;
}
//3、左右都不为空
else
{
//替换法:
//找到左子树的最大结点
Node* leftMax = root->_left;
while (leftMax->_right)
{
leftMax = leftMax->_right;
}
//交换
swap(leftMax->_key, root->_key);
//再递归去左子树删除即可
//注意不能传leftMax,如果刚好要删除结点就是leftMax,那链接就乱了,链接给leftMax了
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
Node* _root;
};
递归删除: root是别名,修改别名就修改了自身,所以我们不需要再去找父节点
注意:
- 保存修改之前的root,一会释放,即先托孤完成了,再释放
- 有两个孩子,使用替换法,替换之后再递归去替换的那个棵子树即可。注意不能传给临时变量leftMax,如果刚好要删除结点就是leftMax,那链接就乱了,链接给leftMax了
3. 二叉搜索树的应用
3.1 K模型
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。
比如:给一个单词word,判断该单词是否拼写正确,具体方式如下
- 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
- 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
,_right(nullptr)
,_key(key)
{}
};
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()
:_root(nullptr)
{}
//析构
~BSTree()
{
Destroy(_root);
}
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
//递归:插入
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
//中序:左根右
//递归调用要传类自己的成员变量作为参数,所以我们再封装一层
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//递归:查找
bool FindR(const K& key)
{
return _FindR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
private:
//前序:根左右
Node* Copy(Node* root)
{
if (nullptr == root)
return nullptr;
Node* copyroot = new Node(root->_key);
copyroot->_left = Copy(root->_left);
copyroot->_right = Copy(root->_right);
return copyroot;
}
//后序:左右根
void Destroy(Node*& root)
{
if (nullptr == root)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
bool _EraseR(Node*& root, const K& key)
{
//走到空,说明没有key
if (nullptr == root)
return false;
if (root->_key < key)
{
_EraseR(root->_right, key);
}
else if (root->_key > key)
{
_EraseR(root->_left, key);
}
else
{
//root是别名,修改别名就修改了自身,所以我们不需要再去找父节点
//但注意:保住修改之前的root,一会释放,即先托孤完成了,再释放
Node* del = root;
//1、左为空
if (nullptr == root->_left)
{
root = root->_right;
}
//2、右为空
else if (nullptr == root->_right)
{
root = root->_left;
}
//3、左右都不为空
else
{
//替换法:
//找到左子树的最大结点
Node* leftMax = root->_left;
while (leftMax->_right)
{
leftMax = leftMax->_right;
}
//交换
swap(leftMax->_key, root->_key);
//再递归去左子树删除即可
//注意不能传leftMax,如果刚好要删除结点就是leftMax,那链接就乱了,链接给leftMax了
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
bool _InsertR(Node*& root, const K& key)
{
//走到空,到了插入的位置
if (nullptr == root)
{
root = new Node(key);
return true;
}
if (root->_key < key)
{
return _InsertR(root->_right, key);
}
else if (root->_key > key)
{
return _InsertR(root->_left, key);
}
else
{
//已经存在了,不要重复插入
return false;
}
}
bool _FindR(Node* root, const K& key)
{
//走到空树,说明没有查找到
if (root == nullptr)
return false;
if (root->_key < key)
{
return _FindR(root->_right, key);
}
else if (root->_key > key)
{
return _FindR(root->_left, key);
}
else
{
return true;
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
Node* _root;
};
3.2 KV模型
KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。
KV模型在现实生活中非常常见:
- 比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对
- 再比如统计水果出现次数,统计成功后,给定水果就可快速找到其出现的次数,水果与其出现次数就是<string, count>就构成一种键值对
namespace key_value
{
template<class K, class V>
struct BSTreeNode
{
BSTreeNode<K, V>* _left;
BSTreeNode<K, V>* _right;
K _key;
V _value;
BSTreeNode(const K& key, const V& value)
:_left(nullptr)
, _right(nullptr)
, _key(key)
,_value(value)
{}
};
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
BSTree()
:_root(nullptr)
{}
//递归:插入
bool InsertR(const K& key, const V& value)
{
return _InsertR(_root, key, value);
}
//中序:左根右
//递归调用要传类自己的成员变量作为参数,所以我们再封装一层
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//递归:查找
Node* FindR(const K& key)
{
return _FindR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
private:
bool _EraseR(Node*& root, const K& key)
{
//走到空,说明没有key
if (nullptr == root)
return false;
if (root->_key < key)
{
_EraseR(root->_right, key);
}
else if (root->_key > key)
{
_EraseR(root->_left, key);
}
else
{
//root是别名,修改别名就修改了自身,所以我们不需要再去找父节点
//但注意:保住修改之前的root,一会释放
Node* del = root;
//1、左为空
if (nullptr == root->_left)
{
root = root->_right;
}
//2、右为空
else if (nullptr == root->_right)
{
root = root->_left;
}
//3、左右都不为空
else
{
//替换法:
//找到左子树的最大结点
Node* leftMax = root->_left;
while (leftMax->_right)
{
leftMax = leftMax->_right;
}
//交换
swap(leftMax->_key, root->_key);
//再递归去左子树删除即可
//注意不能传leftMax,如果刚好要删除结点就是leftMax,那链接就乱了,链接给leftMax了
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
bool _InsertR(Node*& root, const K& key, const V& value)
{
//走到空,到了插入的位置
if (nullptr == root)
{
root = new Node(key, value);
return true;
}
if (root->_key < key)
{
return _InsertR(root->_right, key, value);
}
else if (root->_key > key)
{
return _InsertR(root->_left, key, value);
}
else
{
//已经存在了,不要重复插入
return false;
}
}
Node* _FindR(Node* root, const K& key)
{
//走到空树,说明没有查找到
if (root == nullptr)
return nullptr;
if (root->_key < key)
{
return _FindR(root->_right, key);
}
else if (root->_key > key)
{
return _FindR(root->_left, key);
}
else
{
return root;
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << ":" << root->_value << endl;;
_InOrder(root->_right);
}
Node* _root;
};
void TestBSTree1()
{
BSTree<string, string> dict;
dict.InsertR("insert", "插入");
dict.InsertR("sort", "排序");
dict.InsertR("right", "右边");
dict.InsertR("left", "左边");
dict.InsertR("date", "日期");
string str;
cout << "请输入要翻译的单词,结束请ctrl+z+换行" << endl;
while (cin >> str)
{
auto ret = dict.FindR(str);
if (ret)
{
cout << ret->_value << endl;
}
else
{
cout << "查无此单词" << endl;
}
}
}
void TestBSTree2()
{
// 统计水果出现的次数
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
BSTree<string, int> countTree;
for (auto& str : arr)
{
auto ret = countTree.FindR(str);
if (nullptr == ret)
{
countTree.InsertR(str, 1);
}
else
{
ret->_value++;
}
}
countTree.InOrder();
}
}



2191

被折叠的 条评论
为什么被折叠?



