目录
二、二叉搜索树增删查的代码实现(_key-_value型的二叉搜索树)
一、基本介绍
二叉搜索树是一棵空树,或者是具有以下性质的二叉树:
1、若左子树不为空,则左子树上所有节点的值都小于等于根节点的值
2、若右子树不为空,则右子树上所有节点的值都大于等于根节点的值
3、他的左右子树也分别为二叉搜索树
——和堆还不一样,它不区分左右子树,要么都小,要么都大,或者相等
二叉搜索树的结构决定了查找一个值时,只要根的值比查找的值大,就往左走;
只要根的值比查找的值小,就往右走;直至最后走到空,若还没有找到,就结束,没有这个值
时间复杂度是(N)——最坏情况,形成了一个类似于链表的结构
(logN)——最优情况
为了应对最坏情况,就有了平衡搜索树、红黑树、B树系列
二、二叉搜索树增删查的代码实现(_key-_value型的二叉搜索树)
以下为一个树节点
template <class K, class V>
struct BSTNode
{
K _key;
V _value;//与增删查的实现无关
BSTNode<K, V>* _left;
BSTNode<K, V>* _right;
BSTNode(const K& key, const V& value)
: _key(key)
, _value(value)
, _left(nullptr)
, _right(nullptr)
{}
};
以下为增删查的代码实现
template <class K, class V>
class BSTree
{
using Node = BSTNode<K, V>;
public:
BSTree() = default;
BSTree(const BSTree& t)
{
_root = Copy(t._root);
}
BSTree& operator=(BSTree tmp)
{
swap(_root, tmp._root);
return *this;
}
~BSTree()
{
_Destroy(_root);
_root = nullptr;
}
//二叉搜索树的插入
bool Insert(const K& key, const V& value)
{
if (!_root)
{
_root = new Node(key, value);
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;
}
}
Node* newnode = new Node(key, value);
//这里已经是parent是叶子结点了
if (parent->_key < key)
{
parent->_right = newnode;
}
else
{
parent->_left = newnode;
}
return true;
}
//二叉搜索树的中序遍历以及private成员函数的调用方法
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//二叉搜索树的查找
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key == key)
{
return cur;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
}
return nullptr;
}
//二叉搜索树的删除
bool Erase(const K& key)
{
if (_root == nullptr)
{
return false;
}
Node* parent = nullptr;
Node* cur = _root;
while (cur)//先找到要删除的节点
{
if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
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;
return true;
}
else if(cur->_right == nullptr)
{
if (_root == cur)
{
_root = cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
return true;
}
//这里是两边都有孩子的情况
else
{
//替代法,找右子树的最小值,就满足了根节点比左子树都大,比右子树都小
Node* replace = cur->_right;
Node* replace_parent = cur;
while (replace->_left)
{
replace_parent = replace;
replace = replace->_left;
}
cur->_key = replace->_key;
if (replace_parent->_left == replace)
replace_parent->_left = replace->_right;
else
replace_parent->_right = replace->_right;
delete replace;
return true;
}
}
}
return false;
}
private:
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " : " << root->_value << " ";
_InOrder(root->_right);
}
void _Destroy(Node* root)
{
if (root == nullptr)
{
return;
}
_Destroy(root->_left);
_Destroy(root->_right);
delete root;
}
Node* Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* newRoot = new Node(root->_key, root->_value);
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
return newRoot;
}
Node* _root = nullptr;
};