- 二叉搜索树
- 二叉搜索树概念
- 二叉树的操作
- 插入
- 查找
- 删除
- 遍历(中序)
- 整体实现
- 搜索二叉树的应用
- 整体实现
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
时间复杂度:O(N),只有当树的形状接近完全二叉树或者满二叉树,才能达到 logN
搜索二叉树延伸:AVLTree 红黑树(对搜索二叉树左右高度提出要求,非常接近完全二叉树,效率可达到 O(logN))
上面一般用于在内存中查找,当数据在磁盘中时,对树高度进一步提出要求-》衍生B树系列
bool Insert(const K& key)
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
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
{
return false;
}
}
cur = new Node(key);
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->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return NULL;
}
删除
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
{
//找到,准备删除
//左为空或右为空,可直接删除,把另一个孩子交给父亲管理,删除自己
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;
}
}
}
else//左右都不为空,替换法删除
{
找到右子树最小的节点去替换
//Node* minparent = cur;
//Node* minRight = cur->_right;
//while (minRight->_left)
//{
// minparent = minRight;
// minRight = minRight->_left;
//}
保存替换节点
//cur->_key = minRight->_key;
删除替换节点
//if (minparent->_left == minRight)
//{
// minparent->_left = minRight->_right;
//}
//else
//{
// minparent->_right = minRight->_right;
//}
//delete minRight;
Node* minRight = cur->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
K min = minRight->_key;
//递归调用自己去删除替换节点
this->Erase(min);
cur->_key = min;
}
return true;
}
}
return false;
}
遍历(中序)
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_key << endl;
_Inorder(root->_right);
}
void Inorder()
{
_Inorder(_root);
cout << endl;
}
整体实现
#include搜索二叉树的应用#include using namespace std; template
struct BSTreeNode { BSTreeNode * _left; BSTreeNode * _right; K _key; BSTreeNode(const K& key) :_left(nullptr) , _right(nullptr) , _key(key) {} }; template class BSTree { typedef BSTreeNode Node; private: 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; } } 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; } if (root->_key < key) { return _EraseR(root->_right, key); } else if (root->_key > key) { return _EraseR(root->_left, key); } else { //找到了,root就是要删除的节点 if (root->_left == nullptr) { Node* del = root; root = root->_right; delete del; } else if (root->_right == nullptr) { Node* del = root; root = root->_left; delete del; } else { 找右子树最小节点 //Node* minParent = root; //Node* minRight = root->_right; //while (minRight->_left) //{ // minParent = minRight; // minRight = minRight->_left; //} 保存替换节点的值 //root->key = minRight->_key; //if (minParent->_left == minRight) //{ // minParent->_left = minRight->_right; //} //else //{ // minParent->_right = minRight->_right; //} //delete minRight; Node* minRight = root->_right; while (minRight->_left) { minRight = minRight->_left; K min = minRight->_key; } _EraseR(root->_right, min); root->_key = min; } return ture; } } void _Destory(Node* root) { if (root == NULL) { return; } _Destory(root->_left); _Destory(root->_right); delete root; } Node* _Copy(Node* root) { if (root == nullptr) { return nullptr; } Node* copyNode = new Node(root->_key); copyNode->_left = _Copy(root->_left); copyNode->_right = _Copy(root->_right); return copyNode; } public: BSTree() :_root(nullptr) {} BSTree(const BSTree & t) { _root = _Copy(t._root); } ~BSTree() { _Destory(_root); _root = nullptr; } BSTree & operator=(BSTree t) { swap(_root, t._root); return *this; } bool InsertR(const K& key)//递归版本 { return _InsertR(_root, key); } Node* FindR(const K& key)//递归版本 { return _FindR(_root, key); } bool EraseR(const K& key)//递归版本 { return _EraseR(_root, key); } bool Insert(const K& key) { if (_root == nullptr) { _root = new Node(key); return true; } 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 { return false; } } cur = new Node(key); 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->_right; } else if (cur->_key > key) { cur = cur->_left; } else { return cur; } } return NULL; } void _Inorder(Node* root) { if (root == nullptr) { return; } _Inorder(root->_left); cout << root->_key << endl; _Inorder(root->_right); } void Inorder() { _Inorder(_root); cout << endl; } 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 { //找到,准备删除 //左为空或右为空,可直接删除,把另一个孩子交给父亲管理,删除自己 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; } } } else//左右都不为空,替换法删除 { 找到右子树最小的节点去替换 //Node* minparent = cur; //Node* minRight = cur->_right; //while (minRight->_left) //{ // minparent = minRight; // minRight = minRight->_left; //} 保存替换节点 //cur->_key = minRight->_key; 删除替换节点 //if (minparent->_left == minRight) //{ // minparent->_left = minRight->_right; //} //else //{ // minparent->_right = minRight->_right; //} //delete minRight; Node* minRight = cur->_right; while (minRight->_left) { minRight = minRight->_left; } K min = minRight->_key; //递归调用自己去删除替换节点 this->Erase(min); cur->_key = min; } return true; } } return false; } private: Node* _root; };
-
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。比如:给一个单词word,判断该单词是否拼写正确,具体方式如下: 以单词集合中的每个单词作为key,构建一棵二叉搜索树在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
-
.KV模型:每一个关键码key,都有与之对应的值Value,即
的键值对。该种方式在现实生活中非常常见:比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文 就构成一种键值对;再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是 就构成一种键值对。 比如:实现一个简单的英汉词典dict,可以通过英文找到与其对应的中文,具体实现方式如下 <单词,中文含义>为键值对构造二叉搜索树,注意:二叉搜索树需要比较,键值对比较时只比较 Key 查询英文单词时,只需给出英文单词,就可快速找到与其对应的key
查找
void test1()
{
KV::BSTreedict;
dict.InsertR("string", "字符串");
dict.InsertR("tree", "树");
dict.InsertR("left", "左边,剩余");
dict.InsertR("right", "右边");
dict.InsertR("sort", "排序");
//插入词库中单词
string str;
while(cin >> str)
{
KV::BSTreeNode* ret = dict.FindR(str);
if (ret == nullptr)
{
cout << "单词拼写错误,词库中没有这个单词" << str << endl;
}
else
{
cout << str << "->" << ret->_value << endl;
}
}
}
int main()
{
test1();
return 0;
}
统计出现的次数
void test2()
{
//统计水果出现的次数
string arr[] = { "苹果", "桃子", "香蕉", "桃子", "苹果", "西瓜", "桃子" };
KV::BSTree countfriut;
for (const auto e : arr)
{
//先查找在不在搜索树里
//1、不在-》插入<水果,1>
//2、在-》
//KV::BSTreeNode* ret = countfriut.FindR(e);
auto ret = countfriut.FindR(e);
if (ret == nullptr)
{
countfriut.InsertR(e,1);
}
else
{
ret->_value++;
}
}
countfriut.Inorder();
}
int main()
{
test2();
return 0;
}
整体实现
namespace KV
{
template
struct BSTreeNode
{
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
V _value;
BSTreeNode(const K& key,const V& value)
:_left(nullptr)
, _right(nullptr)
, _key(key)
, _value(value)
{}
};
template
class BSTree
{
typedef BSTreeNode Node;
private:
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;
}
}
bool _InsertR(Node*& root, const K& key,const V& value)
{
if (root == nullptr)
{
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;
}
}
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
{
//找到了,root就是要删除的节点
if (root->_left == nullptr)
{
Node* del = root;
root = root->_right;
delete del;
}
else if (root->_right == nullptr)
{
Node* del = root;
root = root->_left;
delete del;
}
else
{
Node* minRight = root->_right;
while (minRight->_left)
{
minRight = minRight->_left;
K kmin = minRight->_key;
V vmin = minRight->_value;
}
_EraseR(root->_right, kmin);
root->_key = kmin;
root->_value = vmin;
}
return ture;
}
}
void _Destory(Node* root)
{
if (root == NULL)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
delete root;
}
Node* _Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* copyNode = new Node(root->_key,root->_value);
copyNode->_left = _Copy(root->_left);
copyNode->_right = _Copy(root->_right);
return copyNode;
}
public:
BSTree()
:_root(nullptr)
{}
BSTree(const BSTree& t)
{
_root = _Copy(t._root);
}
~BSTree()
{
_Destory(_root);
_root = nullptr;
}
BSTree& operator=(BSTree t)
{
swap(_root, t._root);
return *this;
}
bool InsertR(const K& key,const V& value)//递归版本
{
return _InsertR(_root, key,value);
}
Node* FindR(const K& key)//递归版本
{
return _FindR(_root, key);
}
bool EraseR(const K& key)//递归版本
{
return _EraseR(_root, key);
}
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_key<<":"<_value << endl;
_Inorder(root->_right);
}
void Inorder()
{
_Inorder(_root);
cout << endl;
}
private:
Node* _root;
};
}



