前面我们讲过key模型和Key-Value模型就是常见的两种的模型
一、Key模型
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。即就是判断key在不在就可以了。
比如:门禁系统,小区车辆出入系统等等
给一个单词word,判断该单词是否拼写正确,具体方式如下:
以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
我们前面文章中所完成的二叉搜索树就是key模型的二叉搜身
二、Key/Value模型
Key/Value每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:高铁制车票系统等
比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对;
再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对。
现在我们一起来实现一个key-value模型的二叉搜索树。
首先这是我们之前的二叉搜索树
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:
bool insert(const K& key)
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* prev = nullptr;
Node* cur = _root;
while (cur)
{
prev = cur;
if (key > cur->_key)
cur = cur->_right;
else if (key < cur->_key)
cur = cur->_left;
else
return false;
}
cur = new Node(key);
if (key > prev->_key)
prev->_right = cur;
else
prev->_left = cur;
return true;
}
bool insertR(const K& key) //递归版本
{
return _insertR(_root, key);
}
bool find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (key > cur->_key)
cur = cur->_right;
else if (key < cur->_key)
cur = cur->_left;
else
return true;
}
return false;
}
bool findR(const K& key) //递归版本
{
return _findR(_root, key);
}
bool erase(const K& key) //替换法:把目标替换成左子树的最大值或右子树的最小值
{
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else
{
if (cur->_left == nullptr) //左子树为空
{
if (cur == _root)
_root = cur->_right;
else
{
if (cur == parent->_right)
parent->_right = cur->_right;
else
parent->_left = cur->_right;
}
}
else if(cur->_right == nullptr) //右子树为空
{
if (cur == _root)
_root = cur->_left;
else
{
if (cur == parent->_left)
parent->_left = cur->_left;
else
parent->_right = cur->_left;
}
}
else //左右子树都不为空
{
parent = cur;
Node* leftmid = cur->_left;
while (leftmid->_right)
{
parent = leftmid;
leftmid = leftmid->_right;
}
swap(cur->_key, leftmid->_key);
if (parent == cur)
parent->_left = leftmid->_left;
else
parent->_right = leftmid->_left;
cur = leftmid;
}
delete cur;
return true;
}
}
return false;
}
bool eraseR(const K& key) //递归版本
{
return _eraseR(_root, key);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
BSTree() = default;
BSTree(const BSTree<K>& t)
{
_root = copy(t._root);
}
~BSTree()
{
Destroy(_root);
}
private:
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;
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
bool _findR(Node* root, const K& key)
{
if (root == nullptr)
return false;
if (root->_key == key)
return true;
if (key > root->_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 (key > root->_key)
return _insertR(root->_right, key);
else if (key < root->_key)
return _insertR(root->_left, key);
else
return false;
}
bool _eraseR(Node*& root, const K& key)
{
if (root == nullptr)
return false;
if (key > root->_key)
return _eraseR(root->_right, key);
else if (key < root->_key)
return _eraseR(root->_left, key);
else
{
if (root->_left == nullptr)
{
Node* tmp = root;
root = root->_right; //由于引用,root是父节点子树的别名,所以可以直接修改
delete tmp;
}
else if (root->_right == nullptr)
{
Node* tmp = root;
root = root->_left;
delete tmp;
}
else
{
Node* leftmid = root->_left;
while (leftmid->_right)
leftmid = leftmid->_right;
swap(root->_key, leftmid->_key);
return _eraseR(root->_left, key);
}
return true;
}
}
private:
Node* _root = nullptr;
};
首先是Key/Value模型
所以我们要把树的节点改一下
template<class K, class V>
struct BSTreeNode
{
BSTreeNode(const K& key = K(), const V& val = V())
:_key(key)
,_val(val)
,_left(nullptr)
,_right(nullptr)
{}
K _key;
V _val;
BSTreeNode<K,V>* _left;
BSTreeNode<K,V>* _right;
};
类里面也要小改
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K,V> Node;
public:
BSTree()
:_root(nullptr)
{}
BSTree(const BSTree<K,V>& t)
{
_root = Copy(t._root);
}
~BSTree()
{
_Destory(_root);
}
BSTree<K,V>& operator=(BSTree<K,V> t)
{
std::swap(_root, t._root);
return *this;
}
bool Insert(const K& key,const V& val)
{
if (_root == nullptr)
{
_root = new Node(key,val);
return true;
}
else
{
Node* parent = _root;
Node* cur = _root;
while (cur != nullptr)
{
if (cur->_key == key)
{
return false;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
parent = cur;
cur = cur->_right;
}
}
cur = new Node(key,val);
if (parent->_key > key)
{
parent->_left = cur;
}
else if (parent->_key < key)
{
parent->_right = cur;
}
return true;
}
}
void InOrder()
{
_InOrder(_root);
}
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key == key)
{
return cur;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
}
return nullptr;
}
bool Erase1(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
{
if (parent == nullptr)
{
if (cur->_left == nullptr)
{
_root = cur->_right;
delete cur;
return true;
}
else if (cur->_right == nullptr)
{
_root = cur->_left;
delete cur;
return true;
}
else
{
Node* leftMaxParent = cur;
Node* leftMax = cur->_left;
if (leftMax->_right == nullptr)
{
leftMax->_right = cur->_right;
delete cur;
_root = leftMax;
return true;
}
while (leftMax->_right)
{
leftMaxParent = leftMax;
leftMax = leftMax->_right;
}
std::swap(leftMax->_key, cur->_key);
std::swap(leftMax->_val, cur->_val);
leftMaxParent->_right = leftMax->_left;
delete leftMax;
leftMax = nullptr;
return true;
}
}
if (parent->_left == cur)
{
if (cur->_left == nullptr)
{
parent->_left = cur->_right;
delete cur;
return true;
}
else if (cur->_right == nullptr)
{
parent->_left = cur->_left;
delete cur;
return true;
}
else
{
Node* leftMaxParent = cur;
Node* leftMax = cur->_left;
if (leftMax->_right == nullptr)
{
leftMax->_right = cur->_right;
delete cur;
parent->_left = leftMax;
return true;
}
while (leftMax->_right)
{
leftMaxParent = leftMax;
leftMax = leftMax->_right;
}
std::swap(leftMax->_key, cur->_key);
std::swap(leftMax->_val, cur->_val);
leftMaxParent->_right = leftMax->_left;
delete leftMax;
leftMax = nullptr;
return true;
}
}
else
{
if (cur->_left == nullptr)
{
parent->_right = cur->_right;
delete cur;
return true;
}
else if (cur->_right == nullptr)
{
parent->_right = cur->_left;
delete cur;
return true;
}
else
{
Node* leftMaxParent = cur;
Node* leftMax = cur->_left;
if (leftMax->_right == nullptr)
{
leftMax->_right = cur->_right;
delete cur;
parent->_right = leftMax;
return true;
}
while (leftMax->_right)
{
leftMaxParent = leftMax;
leftMax = leftMax->_right;
}
std::swap(leftMax->_key, cur->_key);
std::swap(leftMax->_val, cur->_val);
leftMaxParent->_right = leftMax->_left;
delete leftMax;
leftMax = nullptr;
return true;
}
}
}
}
return false;
}
bool Erase2(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
{
if (cur->_left == nullptr)
{
if (parent == nullptr)
{
_root = cur->_right;
}
else if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else if (parent->_right == cur)
{
parent->_right = cur->_right;
}
}
else if (cur->_right == nullptr)
{
if (parent == nullptr)
{
_root = cur->_left;
}
else if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else if (parent->_right = cur)
{
parent->_right = cur->_left;
}
}
else
{
Node* leftMax = cur->_left;
Node* leftMaxParent = cur;
while (leftMax->_right)
{
leftMaxParent = leftMax;
leftMax = leftMax->_right;
}
std::swap(cur->_key, leftMax->_key);
std::swap(cur->_val, leftMax->_val);
if (leftMaxParent->_left == leftMax)
{
leftMaxParent->_left = leftMax->_left;
}
else
{
leftMaxParent->_right = leftMax->_left;
}
cur = leftMax;
}
delete cur;
return true;
}
}
}
Node* FindR(const K& key)
{
return _FindR(_root, key);
}
bool InsertR(const K& key,const V& val)
{
return _InsertR(_root, key, val);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
private:
Node* Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* Copyroot = new Node(root->_key, root->val);
Copyroot->_left = Copy(root->_left);
Copyroot->_right = Copy(root->_right);
return Copyroot;
}
void _Destory(Node*& root)
{
if (root == nullptr)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
delete root;
root == nullptr;
}
bool _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
{
return false;
}
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* leftMax = root->_left;
while (leftMax->_right)
{
leftMax = leftMax->_right;
}
std::swap(leftMax->_key, root->_key);
std::swap(leftMax->_val, root->_val);
return _EraseR(root->_left, key);
}
delete del;
return true;
}
}
bool _InsertR(Node*& root, const K& key, const V& val)
{
if (root == nullptr)
{
root = new Node(key, val);
return true;
}
if (root->_key < key)
{
return _InsertR(root->_right, key, val);
}
else if (root->_key > key)
{
return _InsertR(root->_left, key, val);
}
else
{
return false;
}
}
Node* _FindR(Node* root, const K& key)
{
if (root == nullptr)
{
return nullptr;
}
if (root->_key == key)
{
return root;
}
else if (root->_key > key)
{
return _FindR(root->_left, key);
}
else
{
return _FindR(root->_right, key);
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " :" << root->_val << endl;
_InOrder(root->_right);
}
private:
Node* _root;
};
如上就是我们的KV模型的二叉搜索树。我们可以使用如下的两个模型,就是我们的这棵树的应用
void test()
{
BSTree<string, string> dic;
dic.Insert("english", "英语");
dic.Insert("chinese", "中文");
dic.Insert("maths", "数学");
dic.Insert("interfere", "干涉");
cout << "请输入单词" << endl;
string str;
while (cin >> str)
{
BSTreeNode<string, string>* ret = dic.Find(str);
if (ret)
{
cout << ret->_val << endl;
}
else
{
cout << "无此单词" << endl;
cout << "请你添加单词的意思:" << endl;
string str_val;
cin >> str_val;
dic.Insert(str, str_val);
}
cout << "请输入单词" << endl;
}
}
运行结果:
我们也可以换一种写法,如下,这个是一个物品的计数:
void test2()
{
string arr[] = { "铅笔", "橡皮", "铅笔", "铅笔", "钢笔", "钢笔", "钢笔","胶带", "胶带", "橡皮", "橡皮" };
BSTree<string, int> ItemCount;
for (auto& e : arr)
{
BSTreeNode<string, int>* ret = ItemCount.Find(e);
if (ret == nullptr)
{
ItemCount.Insert(e, 1);
}
else
{
ret->_val++;
}
}
ItemCount.InOrder();
}
运行结果:
本篇我们讲了二叉搜索树的应用,对我们后续的讲解将有较大的作用,如有错误,欢迎指正