简介
二叉搜索树(BST)是满足以下几个性质的二叉树:
1.它的每个结点都有一个关键值(key),且每个结点的key各不相同;
2.若左子树不为空,则左子树上所有结点的key均小于它的根结点的key;
3.若右子树不为空,则右子树上所有结点的key均大于它的根结点的key;
4.左右子树也都为二叉搜索数;
5.空树也是二叉搜索树。
如图
BST结点的定义
二叉搜索树(BST)也是一颗二叉树,拥有左右孩子结点,不同的是它里面存放了两个类型的值key(关键值)和value(数据值),key值在BST中是唯一的,不能重复出现。
template<class K,class V>
struct SearchBinaryTreeNode
{
SearchBinaryTreeNode<K, V>* _left;
SearchBinaryTreeNode<K, V>* _right;
//const K _key;
K _key;//此处在删除结点时会发生替换,不能为const
V _value;
SearchBinaryTreeNode(const K& k, const V& v)
:_left(NULL)
,_right(NULL)
,_key(k)
,_value(v)
{}
};
实现的接口
下面会用C++代码实现二叉搜索树的插入,删除,查找,销毁。
并分别用递归和非递归两种思路实现其插入,删除。
1.插入
思路:
通过key值找到其父节点的位置,然后创造一个新的结点按照性质接入父节点的左或者右。
递归实现
bool InsertR(const K& key, const V& value)
{
return _InsertR(_root, key, value);
}
bool _InsertR(Node*& root, const K& key, const V& value)
{
if (root == NULL)
{
root = new Node(key, value);
return true;
}
if (key < root->_key)
{
return _InsertR(root->_left,key,value);
}
else if (key>root->_key)
{
return _InsertR(root->_right,key,value);
}
else
return false;
}
非递归实现
bool Insert(const K& key,const V& value)
{//非递归的插入
if (_root == NULL)
{
_root = new Node<key, value>;
return true;
}
Node* cur = _root;
Node* parent = NULL;
while (cur)
{
if (_root->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
return false;
}
if (parent->_key < key)
{
parent->_right = new Node(key, value);
}
else if (parent->_key>key)
{
parent->_left = new Node(key, value);
}
else
return false;
return true;
}
2.删除
思路:首先通过遍历找到需要删除的结点,然后再分情况讨论,
⑴要删除的结点(记做cur)的左孩子为空,则把cur的右孩子按照BST的性质链接到cur的父结点的左或者右,再删除cur。
⑵要删除的结点(cur)的右孩子为空,则把cur的左孩子按照BST的性质链接到cur的父节点的左或者右,再删除cur。
⑶要删除的结点(cur)的左右孩子都为空,则直接删除cur。
⑷要删除的结点(cur)的左右孩子都不为空,此时应该找到其中序遍历中cur的后继,用后继结点去替换cur。
第四种情况如图,8为7在这棵树中中序遍历的后继。则用8替换掉7,再删除8。
递归实现
//删除结点递归实现
bool RemoveR(const K& key)
{
return _RemoveR(_root, key);
}
bool _RemoveR(Node*& root,const K& key)
{
if (root == NULL)
{
return false;
}
//找到要删除的结点
if (key < root->_key)
{
return _RemoveR(root->_left, key);
}
else if (key>root->_key)
{
return _RemoveR(root->_right, key);
}
else
{
if (root->_left == NULL)
{//要删除的结点左孩子为空时
root = root->_right;
}
else if (root->_right == NULL)
{//要删除的结点右孩子为空时
root = root->_left;
}
else
{//左右都不为空时
Node* del = root;
Node* parent = root;
Node* subLeft = root->_right;
while (subLeft->_left)
{//找到要删除结点的中序的后继结点
parent = subLeft;
subLeft = subLeft->_left;
}
//使该结点与要删除结点交换
root->_key = subLeft->_key;
root->_value = subLeft->_value;
del = subLeft;
if (parent->_right == NULL)
{
parent->_right = subLeft->_right;
}
else
{
parent->_left = subLeft->_right;
}
delete del;
return true;
}
}
}
非递归实现
bool Remove(const K& key)
{//非递归的删除结点
if (_root == NULL)
return false;
Node* cur = _root;
Node* parent = NULL;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
if (cur->_left == NULL)
{
if (parent == NULL)
{
_root = cur->_right;
}
else if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
else if (cur->_right == NULL)
{
if (parent == NULL)
{
_root = cur->_left;
}
else if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
else
{
parent = cur;
Node* subLeft = cur->_right;
while (subLeft->_left)
{
parent = subLeft;
subLeft = subLeft->_left;
}
cur->_key = subLeft->_key;
cur->_value = subLeft->_value;
if (parent->_left == subLeft)
parent->_left = subLeft->_right;
else
parent->_right = subLeft->_right;
}
delete cur;
}
}
return true;
}
3.查找
思路:遍历BST,通过key值找到所要查找的结点,并返回该结点。
Node* FindR(const K& key)
{
return FindR(_root, key);
}
Node* _FindR(Node* root, const K& key)
{
if (root == NULL)
{
return false;
}
if (key < root->_key)
{
return _FindR(root->_left, key);
}
else if (key > root->_key)
{
return _FindR(root->_right, key);
}
else if(key == root->_key)
{
return root;
}
}
4.销毁和析构函数
~SearchBinaryTree()
{
Destroy(_root);
}
void Destroy(Node*& root)
{
if (root == NULL)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = NULL;
}