1.二叉搜索树概念
二插搜索树,也称为二叉查找树(BST),是一种特殊的二叉树,它的每个节点都包含一个可进行比较的键及其对应的值,且每个节点的左子树中所有键都小于该节点的键,右子树中所有键都大于该节点的键。
二叉搜索树是一种递归数据结构,它或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不为空,则左子树上所有节点的值都小于它的根节点;若它的右子树不为空,则右子树上所有节点的值都大于它的根节点;它的左右子树也分别为二叉搜索树。
2.二叉树的操作
- 二叉搜索树的查找
a、从根开始比较,查找,比根大则往右边走查找,比根小则往左边走查找。
b、最多查找高度次,走到到空,还没找到,这个值不存在。 - 二叉搜索树的插入
插入的具体过程如下:
a. 树为空,则直接新增节点,赋值给root指针
b. 树不空,按二叉搜索树性质查找插入位置,插入新节点。 - 二叉搜索树的删除(主要d情况)
首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:
a. 要删除的结点无孩子结点
b. 要删除的结点只有左孩子结点
c. 要删除的结点只有右孩子结点
d. 要删除的结点有左、右孩子结点
其中情况a可以与情况b或者c合并起来,所以真正的删除过程如下:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
情况d:这种情况用替换法删除较为简单,在它的右子树中寻找最小结点,(或者左子树最大节点)用它的值填补到被删除节点中,最后删除该节点即可–替换法删除
3.二叉搜索树的实现
- 首先我们需要一个结点,且这个结点类数据的类型用模版来实现较好;
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
};
- 然后构建一个二叉搜索树的类,如下:
class BSTree
{
typedef BSTreeNode<K> Node;//typedef一下,方便书写
public:
//构造函数
BSTree()
:_root(nullptr)
{}
//析构函数
~BSTree()
{
Destroy(_root);
}
protected://不希望在类外面掉调到的函数用protected保护起来
void Destroy(Node*& root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
private:
Node* _root;
}
- 插入数据:若树为空,则直接新增节点,赋值给root指针;若树不为空,比树的值大,在树的右边寻找,比树的值小,在树的左边寻找;找到位置后,需要将插入数据和树连接起来,所以还需要该节点的父节点,最后在判断要插入父节点的左边还是右边;
非递归:
bool Insert(const K& key)
{
if (_root == NULL)
{
_root = new Node(key);
return true;
}
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
{
return false;
}
}
cur = new Node(key);
//插入父节点的左边还是右边
if (parent->_key > key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
return true;
}
递归:
递归一般要在类里面套一层,因为递归调位需要传参(_root),而在类外不能访问_root;bool _InsertR(Node*& root, const K& key),要引用root,这样就容易连接起来;
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
protected:
bool _InsertR(Node*& root, const K& key)
{
if (root == NULL)
{
root = new Node(key);
return true;
}
if (root->_key > key)
{
return _InsertR(root->_left, key);
}
else if (root->_key < key)
{
return _InsertR(root->_right, key);
}
else
{
return false;
}
}
- 查找:查找较简单,比树的值大,在树的右边寻找,比树的值小,在树的左边寻找;
非递归:
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
递归:
bool FindR(const K& key)
{
return _FindR(_root, key);
}
protected:
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->_left, key);
}
else
{
return _FindR(root->_right, key);
}
}
- 删除:
情况b:删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点–直接删除
情况c:删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点–直接删除
情况d:这种情况用替换法删除较为简单,在它的右子树中寻找最小结点,(或者左子树最大节点)用它的值填补到被删除节点中,最后删除该节点即可–替换法删除
非递归:
bool Erase(const K& key)
{
Node* parent = _root;
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;
}
//右为空
else if (cur->_right == nullptr)
{
if (cur == _root)
{
_root == cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
//都不为空,
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;
//可以是左树最大节点替代
Node* pmaxLeft = cur;
Node* maxLeft = cur->_left;
while (maxLeft->_right)
{
pmaxLeft = maxLeft;
maxLeft = maxLeft->_right;
}
cur->_key = maxLeft->_key;
if (pmaxLeft->_left == maxLeft)
{
pmaxLeft->_left = maxLeft->_left;
}
else
{
pmaxLeft->_right = maxLeft->_left;
}
delete maxLeft;
}
return true;
}
}
return false;
}
递归:
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
protected:
bool _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
return false;
if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else
{
Node* cur = root;
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
Node* maxLeft = root->_left;
while (maxLeft->_right)
{
maxLeft = maxLeft->_right;
}
swap(cur->_key, maxLeft->_key);
return _EraseR(root->_left, key);
}
delete cur;
return true;
}
}
完整代码如下:
//结点
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()
:_root(nullptr)
{}
//BSTree() = default;//制定强制生成默认构造参数
void InOrder()
{
_InOrder(_root);
cout << endl;
}
~BSTree()
{
Destroy(_root);
}
bool Insert(const K& key)
{
if (_root == NULL)
{
_root = new Node(key);
return true;
}
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
{
return false;
}
}
cur = new Node(key);
if (parent->_key > key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
return true;
}
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key > key)
{
cur = cur->_left;
}
else if (cur->_key < key)
{
cur = cur->_right;
}
else
{
return true;
}
}
return false;
}
bool Erase(const K& key)
{
Node* parent = _root;
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;
}
//右为空
else if (cur->_right == nullptr)
{
if (cur == _root)
{
_root == cur->_left;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
}
//都不为空,
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;
//可以是左树最大节点替代
Node* pmaxLeft = cur;
Node* maxLeft = cur->_left;
while (maxLeft->_right)
{
pmaxLeft = maxLeft;
maxLeft = maxLeft->_right;
}
cur->_key = maxLeft->_key;
if (pmaxLeft->_left == maxLeft)
{
pmaxLeft->_left = maxLeft->_left;
}
else
{
pmaxLeft->_right = maxLeft->_left;
}
delete maxLeft;
}
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);
}
protected:
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->_left, key);
}
else
{
return _FindR(root->_right, key);
}
}
bool _InsertR(Node*& root, const K& key)
{
if (root == NULL)
{
root = new Node(key);
return true;
}
if (root->_key > key)
{
return _InsertR(root->_left, key);
}
else if (root->_key < key)
{
return _InsertR(root->_right, key);
}
else
{
return false;
}
}
bool _EraseR(Node*& root, const K& key)
{
if (root == nullptr)
return false;
if (root->_key > key)
{
return _EraseR(root->_left, key);
}
else if (root->_key < key)
{
return _EraseR(root->_right, key);
}
else
{
Node* cur = root;
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
Node* maxLeft = root->_left;
while (maxLeft->_right)
{
maxLeft = maxLeft->_right;
}
swap(cur->_key, maxLeft->_key);
return _EraseR(root->_left, key);
}
delete cur;
return true;
}
}
void Destroy(Node*& root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
root = nullptr;
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};