一、二叉搜索树的概念:
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
二 、二叉搜索树的操作:
2.1、主体框架:
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_key(key)
, _left(nullptr)
, _right(nullptr)
{}
};
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
public:
private:
Node* _root = nullptr;
};
2.2、中序遍历:
对于二叉搜索树来说,中序遍历就是排好序的形式,如果中序遍历之后需要换行,那可以用子函数的形式完成递归,并放在私有防止直接使用。
2.3、二叉搜索树的查找:
a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b、最多查找高度次,走到到空,还没找到,这个值不存在。
2.4、二叉搜索树的插入:
插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点
2.5、二叉搜索树的删除:(!!!)
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来:
1. 删除节点的右为空
2. 删除节点的左为空
3. 删除节点的左右都不为空
因此真正的删除过程如下:
- 情况1:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除。
- 情况2:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
- 情况3:在它的右子树中寻找中序下的第一个结点(关键码最小,即右子树的最小节点),用它的值填补到被删除节点中,再来处理该结点的删除问题–替换法删除
(或者在他的左子树寻找最右的节点替换,一个道理,但我们以上面的情况演示)
当然,这几种也都有递归版本,还有一些默认成员函数的实现,由于整体看才能更好理解,因此这些都通过下面的代码形式展示:
三、二叉搜索树的模拟实现:
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() = default; //指定强制生成的默认构造
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
~BSTree()
{
Destory(_root);
}
bool Insert(const K& key)
{
//第一个插入的节点为头节点
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
//寻找目标值应在树的位置
Node* cur = _root; //从头节点开始遍历
Node* parent = nullptr; //记录目标数节点的父节点,以确保链接
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;
}
bool 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 true;
}
}
return false;
}
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 = _root->_right;
}
else
{
if (parent->_left == cur) //被删节点本身在整棵树的左枝,被删节点的左子节点一定比其父节点小
{
parent->_left = cur->_left;
}
else//被删节点本身在整棵树的右枝,被删节点的右子节点一定比其父节点大
{
parent->_right = cur->_right;
}
}
delete cur;
}//2.被删节点的右节点为空
else if (cur->_right == nullptr)//同理
{
if (cur == _root) //同上
{
_root = _root->_left;
}
else
{
if (parent->_right == cur)
{
parent->_right = cur->_right;
}
else
{
parent->_left = cur->_left;
}
}
delete cur;
}//3.被删节点左右节点都不为空
else
{
// 找右树最小节点替代,也可以是左树最大节点替代
Node* pminright = cur;
Node* minright = cur->_right;
while (minright->_left)
{
pminright = minright;
minright = minright->_left;
}
//替换删除
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 Destory(Node*& root)
{
if (root == nullptr)
return;
Destory(root->_left);
Destory(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;
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即可
{
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); //用swap而不是赋值,确保递归子树时,子树中的最大值是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;
};
四、二叉搜索树的应用:
4.1、K模型:
K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。 比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
- 以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
- 在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
这种实现方式通过上面的代码的Find就可以完成,只不过模板参数转化成了string类型。
4.2、KV模型:
KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方
式在现实生活中非常常见:
- 如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英
文单词与其对应的中文<word, chinese>就构成一种键值对; - 再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出
现次数就是<word, count>就构成一种键值对。
4.3 KV模型代码实现 :
namespace KV//通过一个值找另一个值key value,也就是map
{
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)
:_key(key)
, _value(value)
, _left(nullptr)
, _right(nullptr)
{}
};
//BSTree<string, vector<string>> 字典查找
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
bool insert(const K& key, const V& value)
{
if (_root == nullptr)
{
_root = new Node(key, value);
return true;
}
Node* cur = _root;
Node* parent = _root;
while (cur)
{
parent = cur;
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
}
void Inorder()
{
_Inorder(_root);
}
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 nullptr;
}
private:
void _Inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_key << ":" << root->_value << endl;
_Inorder(root->_right);
}
Node* _root = nullptr;
};
}
- 映射查找:
void TestBsTree3()
{
//词库中单词都放进这个搜索树中
//Key的搜索模型,判断在不在?
//场景:检查单词是否正确/车库初入系统/...
//K::BSTree<string> dict;
// Key/Value的搜索模型,通过Key查找或修改Value
KV::BSTree<string, string> dict;
dict.insert("sort", "排序");
dict.insert("string", "字符串");
dict.insert("left", "左边");
dict.insert("right", "右边");
string str;
while (cin >> str)
{
KV::BSTreeNode<string, string>* ret = dict.Find(str);
if (ret)
{
cout << ret->_value << endl;
}
else
{
cout << "无此单词" << endl;
}
}
}
ctrl c快捷键结束
2.统计单词次数:
void TestBsTree4()
{
// 统计水果出现的次数
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
KV::BSTree<string, int> countTree;
for (auto e : arr)
{
auto* ret = countTree.Find(e);
if (ret == nullptr)
{
countTree.insert(e, 1);
}
else
{
ret->_value++;
}
}
countTree.Inorder();
}
五、二叉搜索树的性能分析:
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。
问题:如果退化成单支树,二叉搜索树的性能就失去了。那能否进行改进,不论按照什么次序插入关键码,二叉搜索树的性能都能达到最优?那么我们后续章节学习的AVL树和红黑树就可以上场了。