文章目录
二叉搜索树
1. 二叉搜索树的概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
二叉搜索树的结构代码
template<class K>
struct BSTreeNode //用于生成二叉搜索树的节点
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
//结点的构造函数
BSTreeNode(const K& key)
:_right(nullptr)
,_left(nullptr)
,_key(key)
{}
};
template <class K>
struct BSTree //表示整颗二叉搜索树
{
typedef BSTreeNode<K> Node;
//构造函数
//法1:
//BSTree()
// :_root(nullptr)
//{}
//法2: 强制生成默认的拷贝构造
BSTree() = default;
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
//析构
~BSTree()
{
Destroy(_root);
}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
protected:
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
//先拷贝左树再拷贝右树
Node* newRoot = new Node(root->_key);
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
return newRoot;
}
void Destroy(Node*& root)
{
if (root == nullptr)
return;
//先销毁左子树, 再销毁右子树, 最后销毁根节点
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
private:
Node* _root = nullptr;
};
二叉搜索树的中序遍历代码
使用InOrder调用 _ InOrder 的原因是类外面传参传不了私有的_root,所以采用多套一层的方法。
void _InOrder(Node *root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
2. 二叉搜索树操作
2.1 二叉搜索树的查找
a. 从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b. 最多查找高度次,走到到空,还没找到,这个值不存在。
迭代查找代码
bool Find(const K &key)
{
Node *cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
递归查找代码
bool _FindR(Node *root, const K &key)
{
if (root == nullptr)
return false;
if (root->_key == key)
return true;
if (root->_key < key)
return _FindR(root->_right, key);
else
return _FindR(root->_left, key);
}
bool FindR(const K &key)
{
return _FindR(_root, key);
}
2.2 二叉搜索树的插入
插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点
迭代插入代码
bool Insert(const K &key)
{
// 1. 找在树中的插入位置
// 第一次插入(插入根节点)
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node *parent = nullptr;
Node *cur = _root;
while (cur)
{
parent = cur;
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else // 元素已经在树中存在, 无法继续插入
{
return false;
}
}
// 2.链接
cur = new Node(key);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
递归插入代码
递归写法巧就巧在形参是指针的引用,这样插入节点就自动和父节点连接上了。
bool _InsertR(Node *&root, const K &key) //形参是root的引用
{
if (root == 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 InsertR(const K &key)
{
return _InsertR(_root, key);
}
2.3 二叉搜索树的删除(重要)
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
- a. 左右都为空
- b. 右为空
- c. 左为空
- d. 左右都不为空
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,也可以将a单独算一种情况
删除过程如下:
- a. 左右都为空 - 直接删
- b. 右为空 - 删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点
- c. 左为空 - 删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
- d. 左右都不为空 - 在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除
迭代删除代码
bool Erase(const K &key)
{
Node *cur = _root;
Node *parent = nullptr;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
// 3种情况: 1. 左为空 2. 右为空 3. 左右都不为空
if (cur->_left == nullptr) // 左为空
{
if (cur == _root) // 所删的节点是根节点
{
_root = cur->_right;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
else if (cur->_right == nullptr) // 右为空
{
if (cur == _root) // 所删的节点是根节点
{
_root = cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
else
{
// 找左树最大节点或右树最小节点替代
// 这里找右树最小节点替代
// 找右树最小的过程
Node *pminRight = cur;
Node *minRight = cur->_right;
while (minRight->_left)
{
pminRight = minRight;
minRight = minRight->_left;
}
// 找到了, 交换两者的值
swap(cur->_key, minRight->_key);
// 删除原来的节点
if (pminRight->_left == minRight)
{
pminRight->_left = minRight->_right;
}
else
{
pminRight->_right = minRight->_right;
}
delete minRight;
}
return true;
}
}
return false;
}
递归删除代码
bool _EraseR(Node *&root, const K &key)
{
if (root == nullptr)
return false;
// 还是分3种情况
if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else
{
Node *del = root;
// 开始准备删除
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
// 找左树最大结点的过程
Node *maxleft = root->_left;
while (maxleft->_right)
{
maxleft = maxleft->_right;
}
swap(root->_key, maxleft->_key); //必须交换
// 转换成在子树中删除
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
bool EraseR(const K &key)
{
return _EraseR(_root, key);
}
3. 二叉搜索树的应用
3.1 K模型
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。
比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
- 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
- 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
总结:
K模型解决在不在问题,应用场景:
- 门禁系统
- 车库系统
- 检查一篇文章中单词是否拼写正确
K模型代码
//二叉树的结点
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
//结点的构造函数
BSTreeNode(const K& key)
:_right(nullptr)
,_left(nullptr)
,_key(key)
{}
};
template<class K>
struct BSTree
{
typedef BSTreeNode<K> Node;
public:
//构造
//法1:
//BSTree()
// :_root(nullptr)
//{}
//法2: 强制生成默认的拷贝构造
BSTree() = default;
//拷贝构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
//析构
~BSTree()
{
Destroy(_root);
}
//赋值重载
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
//插入
bool Insert(const K& key)
{
//1. 找在树中的插入位置
//第一次插入(插入根节点)
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
parent = cur;
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else //元素已经在树中存在, 无法继续插入
{
return false;
}
}
//2.链接
cur = new Node(key);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent ->_left = cur;
}
return true;
}
//查找
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
//删除
bool Erase(const K& key)
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
// 3种情况: 1. 左为空 2. 右为空 3. 左右都不为空
if (cur->_left == nullptr) //左为空
{
if (cur == _root) //所删的节点是根节点
{
_root = cur->_right;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
else if (cur->_right == nullptr) //右为空
{
if (cur == _root) //所删的节点是根节点
{
_root = cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
else
{
//找左树最大节点或右树最小节点替代
//这里找右树最小节点替代
//找右树最小的过程
Node* pminRight = cur;
Node* minRight = cur->_right;
while (minRight->_left)
{
pminRight = minRight;
minRight = minRight->_left;
}
//找到了, 交换两者的值
swap(cur->_key, minRight->_key);
//删除原来的节点
if (pminRight->_left == minRight)
{
pminRight->_left = minRight->_right;
}
else
{
pminRight->_right = minRight->_right;
}
delete minRight;
}
return true;
}
}
return false;
}
bool FindR(const K& key)
{
return _FindR(_root, key);
}
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
protected:
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
//先拷贝左树再拷贝右树
Node* newRoot = new Node(root->_key);
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
return newRoot;
}
void Destroy(Node*& root)
{
if (root == nullptr)
return;
//先销毁左子树, 再销毁右子树, 最后销毁根节点
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
bool _FindR(Node* root, const K& key)
{
if (root == nullptr)
return false;
if (root->_key == key)
return true;
if (root->_key < key)
return _FindR(root->_right, key);
else
return _FindR(root->_left, key);
}
bool _InsertR(Node*& root, const K& key)
{
if (root == nullptr)
{
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 _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
return false;
//还是分3种情况
if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else if(root->_key > key)
{
return _EraseR(root->_left, key);
}
else
{
Node* del = root;
//开始准备删除
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
//找左树最大结点的过程
Node* maxleft = root->_left;
while (maxleft->_right)
{
maxleft = maxleft->_right;
}
swap(root->_key, maxleft->_key); //必须交换
//转换成在子树中删除
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};
3.2 KV模型
KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:
- 比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;
- 再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对。
KV模型解决通过一个值查找另一个值问题,应用场景:
- 中英文互译字典
- 电话号码查询快递信息/电话号码 + 验证码查询考试成绩
- 统计水果出现的次数
KV模型代码
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)
:_right(nullptr)
, _left(nullptr)
, _key(key)
,_value(value)
{}
};
template<class K, class V>
struct BSTree
{
typedef BSTreeNode<K, V> Node;
public:
//插入
bool Insert(const K& key, const V& value)
{
//1. 找在树中的插入位置
//第一次插入(插入根节点)
if (_root == nullptr)
{
_root = new Node(key,value);
return true;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
parent = cur;
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else //元素已经在树中存在, 无法继续插入
{
return false;
}
}
//2.链接
cur = new Node(key, value);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
//查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return cur;
}
}
return nullptr;
}
//删除
bool Erase(const K& key)
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
{
// 3种情况: 1. 左为空 2. 右为空 3. 左右都不为空
if (cur->_left == nullptr) //左为空
{
if (cur == _root) //所删的节点是根节点
{
_root = cur->_right;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
else if (cur->_right == nullptr) //右为空
{
if (cur == _root) //所删的节点是根节点
{
_root = cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
else
{
//找左树最大节点或右树最小节点替代
//这里找右树最小节点替代
//找右树最小的过程
Node* pminRight = cur;
Node* minRight = cur->_right;
while (minRight->_left)
{
pminRight = minRight;
minRight = minRight->_left;
}
//找到了, 交换两者的值
swap(cur->_key, minRight->_key);
//删除原来的节点
if (pminRight->_left == minRight)
{
pminRight->_left = minRight->_right;
}
else
{
pminRight->_right = minRight->_right;
}
delete minRight;
}
return true;
}
}
return false;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
protected:
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << ":" << root->_value << endl;
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};
}
中英互译
void TestBSTree1()
{
key_value::BSTree<string, string> dict;
dict.Insert("string", "字符串");
dict.Insert("tree", "树");
dict.Insert("left", "左边、剩余");
dict.Insert("right", "右边");
dict.Insert("sort", "排序");
string str;
while (cin >> str)
{
key_value::BSTreeNode<string, string>*ret = dict.Find(str);
//auto ret= dict.Find(str);
if (ret)
{
cout << ":" << ret->_value << endl;
}
else
{
cout << "无此单词" << endl;
}
}
}
统计水果出现次数
void TestBSTree2()
{
// 统计水果出现的次数
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
"苹果", "香蕉", "苹果", "香蕉" };
key_value::BSTree<string, int> countTree;
for (auto& str : arr)
{
// 先查找水果在不在搜索树中
// 1、不在,说明水果第一次出现,则插入<水果, 1>
// 2、在,则查找到的节点中水果对应的次数++
key_value::BSTreeNode<string, int>* ret = countTree.Find(str);
//auto ret = countTree.Find(str);
if (ret == nullptr)
{
countTree.Insert(str, 1);
}
else
{
ret->_value++;
}
}
countTree.InOrder();
}
4. 二叉搜索树的性能分析
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:
最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树),其平均比较次数为: log 2 N \log_2N log2N
最差情况下,二叉搜索树退化为单支树(或者类似单支),其平均比较次数为: N 2 \frac{N}{2} 2N
问题:如果退化成单支树,二叉搜索树的性能就失去了。那能否进行改进,不论按照什么次序插入关键码,二叉搜索树的性能都能达到最优?那么我们后续章节学习的AVL树和红黑树就可以上场了。