1.二叉搜索树概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:
--若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
--若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
--它的左右子树也分别为二叉搜索树
2.二叉搜索树的应用
2.1 K模型
K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值。
比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:
.以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树
.在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误。
2.2KV模型
每一个关键码key,都有与之对应的值Value,即的键值对。
该种方式在现实生活中非常常见:
1.比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英 文单词与其对应的中文就构成一种键值对;
2.再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出 现次数就是就构成一种键值对。
3.二叉搜索树的模拟实现
创建二叉搜索树结点
template<class K, class V>
struct BSTreeNode
{
typedef BSTreeNode<K, V> Node;
Node* _left;
Node* _right;
K _key;
V _value;
BSTreeNode(const K& key, const V& value)
:_left(nullptr)
, _right(nullptr)
, _key(key)
, _value(value)
{
}
};
构造,拷贝,析构,赋值函数
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
BSTree()
:_root(nullptr)
{
}
BSTree(const BSTree<K, V>& cp)
{
_root = Copy(cp._root);
}
Node* Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* cproot = new Node(root->_key, root->_value);
Copy(root->_left);
Copy(root->_right);
return cproot;
}
BSTree<K, V>& operator=(BSTree<K, V> t)
{
swap(_root, t._root);
return *this;
}
~BSTree()
{
Destory(_root);
}
void Destory(Node*& root)
{
if (root == nullptr)
{
return;
}
Destory(root->_left);
Destory(root->_right);
delete root;
root = nullptr;
}
private:
Node* _root = nullptr;
};
插入函数
插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点
//递归,有KV值
bool Insert(const K& key, const V& value)
{
return _Insert(_root, key, value);
}
bool _Insert(Node*& root, const K& key, const V& value)
{
//如果根为空
if (root == nullptr)
{
root = new Node(key, value);
return true;
}
if (root->_key > key)
{
return _Insert(root->_left, key, value);
}
else if (root->_key < key)
{
return _Insert(root->_right, key, value);
}
else
{
//找到key值相等的结点,则插入失败
return false;
}
}
查找函数
a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b、最多查找高度次,走到到空,还没找到,这个值不存在。
Node* Find(const K& key)
{
return _Find(_root, key);
}
Node* _Find(Node* root, const K& key)
{
if (root == nullptr)
{
return nullptr;
}
if (root->_key > key)
{
return _Find(root->_left, key);
}
else if (root->_key < key)
{
return _Find(root->_right, key);
}
else
{
return root;
}
}
删除函数
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
看起来有待删除节点有4中情况,实际情况a可以与情况b或者c合并起来,因此真正的删除过程如下:
情况1:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点--直接删除--
情况2:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点--直接删除--
情况3:在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点 中,再来处理该结点的删除问题--替换法删除
bool Erase(const K& key)
{
return _Erase(_root, key);
}
bool _Erase(Node*& root, const K& key)
{
if (root == nullptr)
{
return false;
}
if (root->_key > key)
{
return _Erase(root->_left, key);
}
else if (root->_key < key)
{
return _Erase(root->_right, key);
}
else
{
//找到了要删除的结点
Node* dele = root;
if (root->_left == nullptr) //被删除结点的左孩子为空
{
root = root->_right;
}
else if (root->_right == nullptr) //被删除结点的右孩子为空
{
root = root->_left;
}
else
{
//左右孩子都存在
Node* leftMax = root->_left; //找到被删除结点的左孩子树最大的结点
if (leftMax)
{
leftMax = leftMax->_right;
}
std::swap(leftMax->_key, root->_key);
std::swap(leftMax->_value, root->_value);
return _Erase(root->_left, key);
}
delete dele;
return true;
}
}
中序遍历
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " :" << root->_value << endl;
_InOrder(root->_right);
}
4.二叉搜索树的性能分析
插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二 叉搜索树的深度的函数,即结点越深,则比较次数越多。
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:
最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树)
最差情况下,二叉搜索树退化为单支树(或者类似单支)