目录
一、搜素二叉树的基本概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
下面这张图就是一个搜索二叉树:
二、搜索二叉树的基本性质
- 有序性质:
每个节点都包含一个键值。
对于每个节点,其左子树中的所有节点的键值小于该节点的键值。
对于每个节点,其右子树中的所有节点的键值大于该节点的键值。
因此,中序遍历搜索二叉树会得到一个有序的序列。 - 唯一性:
在搜索二叉树中,不存在两个节点具有相同的键值。 - 平衡性:
在极端情况下,搜索二叉树可能会退化为链表,导致查找、插入和删除操作的时间复杂度为 O(n)。为了保持树的平衡性,可以使用平衡二叉搜索树(如 AVL 树、红黑树等)。
三、搜索二叉树的应用
1.K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值
2.KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见:
比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对。
四、模拟实现
4.1 插入操作
插入的具体过程如下:
- 树为空,则直接新增节点,赋值给root指针
- 树不空,按二叉搜索树性质查找插入位置,插入新节点
bool Insert(const K& key, const V& value)
{
Node* pre = nullptr;
Node* cur = _root;
Node* tmp = new Node(key, value);
if (cur == nullptr)
{
_root = tmp;
return true;
}
while (cur)
{
if (key > cur->_key)
{
pre = cur;
cur = cur->right;
}
else
{
pre = cur;
cur = cur->left;
}
}
if (key > pre->_key)
{
pre->right = tmp;
}
else
pre->left = tmp;
return true;
}
这是非递归版本的,递归版本的大家可以自己尝试一下。
4.2 删除操作
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
- 要删除的结点无孩子结点
- 要删除的结点只有左孩子结点
- 要删除的结点只有右孩子结点
- 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程如下:
- 情况1:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除
- 情况2:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除
- 情况3:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中,再来处理该结点的删除问题--替换法删除
bool Erase(const K& key)
{
Node* pre = nullptr;
Node* cur = _root;
while (cur->_key != key)
{
if (key > cur->_key)
{
pre = cur;
cur = cur->right;
}
else if (key < cur->_key)
{
pre = cur;
cur = cur->left;
}
}
if (cur->left==nullptr)//左孩子为空
{
if (pre == nullptr)
{
_root = cur->right;
}
else if (pre->_key > key)
{
pre->left = cur->right;
}
else
{
pre->right = cur->right;
}
delete cur;
}
else if(cur->right==nullptr)//右孩子为空
{
if (pre == nullptr)
{
_root = cur->left;
}
else if (pre->_key > key)
{
pre->left = cur->left;
}
else
{
pre->right = cur->left;
}
delete cur;
}
else if (cur->left && cur->right)//左右孩子都不为空
{
pre = cur;
Node* tmp = cur->right;
while (tmp->left)
{
pre = tmp;
tmp = tmp->left;
}
swap(cur->_key, tmp->_key);
swap(cur->_value, tmp->_value);
if (pre != cur)
pre->left = tmp->right;
else
pre->right = tmp->right;
delete tmp;
}
return true;
}
4.3 查找操作
从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
最多查找高度次,走到到空,还没找到,这个值不存在。
Node* 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 cur;
}
return nullptr;
}
4.4 遍历操作
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->left);
cout << root->_key << " " << root->_value << endl;
_InOrder(root->right);
}
4.5 整体框架
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class K, class V>
struct BSTreeNode
{
K _key;
V _value;
BSTreeNode<K,V>* left;
BSTreeNode<K,V>* right;
BSTreeNode(K key,V value)
:_key(key)
,_value(value)
,left(nullptr)
,right(nullptr)
{}
};
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
bool Insert(const K& key, const V& value)
{
return Insert(key, value, _root);
/*Node* pre = nullptr;
Node* cur = _root;
Node* tmp = new Node(key, value);
if (cur == nullptr)
{
_root = tmp;
return true;
}
while (cur)
{
if (key > cur->_key)
{
pre = cur;
cur = cur->right;
}
else
{
pre = cur;
cur = cur->left;
}
}
if (key > pre->_key)
{
pre->right = tmp;
}
else
pre->left = tmp;
return true;*/
}
bool Insert(const K& key, const V& value, Node*& root)
{
if (root == nullptr)
{
root = new Node(key, value);
return true;
}
if (key > root->_key)
return Insert(key, value, root->right);
else
return Insert(key, value, root->left);
}
Node* 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 cur;
}
return nullptr;
}
bool Erase(const K& key)
{
Node* pre = nullptr;
Node* cur = _root;
while (cur->_key != key)
{
if (key > cur->_key)
{
pre = cur;
cur = cur->right;
}
else if (key < cur->_key)
{
pre = cur;
cur = cur->left;
}
}
if (cur->left==nullptr)//左孩子为空
{
if (pre == nullptr)
{
_root = cur->right;
}
else if (pre->_key > key)
{
pre->left = cur->right;
}
else
{
pre->right = cur->right;
}
delete cur;
}
else if(cur->right==nullptr)//右孩子为空
{
if (pre == nullptr)
{
_root = cur->left;
}
else if (pre->_key > key)
{
pre->left = cur->left;
}
else
{
pre->right = cur->left;
}
delete cur;
}
else if (cur->left && cur->right)//左右孩子都不为空
{
pre = cur;
Node* tmp = cur->right;
while (tmp->left)
{
pre = tmp;
tmp = tmp->left;
}
swap(cur->_key, tmp->_key);
swap(cur->_value, tmp->_value);
if (pre != cur)
pre->left = tmp->right;
else
pre->right = tmp->right;
delete tmp;
}
return true;
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->left);
cout << root->_key << " " << root->_value << endl;
_InOrder(root->right);
}
void InOrder()
{
_InOrder(_root);
}
private:
Node* _root = nullptr;
};