1.二叉搜索树的概念
二叉搜索树又被称为二叉排序树,它符合下面几个性质(空树也为二叉搜索树)
- 若它的左子树不为空,左子树上的所有节点都小于根节点
- 若它的右子树不为空,右子树上的所有节点都大于根节点
- 它的左右子树也为二叉搜索树
2 写一个二叉搜索树
2.1首先创建树节点类
节点需要存储左右树的指针,以及_key值。这里使用struct是我懒得加public:了,也可以写成class加上public限定符。
template<class K>
struct BSTreeNode
{
BSTreeNode<K>* _left;
BSTreeNode<K>* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
};
2.2创建二叉搜索树类
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
private:
Node* _root = nullptr;
}
2.3插入
bool Insert(const K& key)
{
//搜索二叉树为空时直接new一个树节点给_root即可
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
//插入树节点时需要将创建的新的节点与parent节点进行链接
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
{
//二叉搜索树不允许插入相同的_key值节点,插入失败返回false
return false;
}
}
//这一步只是将cur这个变量的值赋为新节点,没有进行链接
cur = new Node(key);
//判断需要插入到parent的左子树还是右子树
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
2.4 删除
bool Erase(const K& key)
{
/*删除一个节点,左为空时,需要parent节点接管右节点,右为空时接管左节点,左右
都为空时,需要寻找这个被删除节点的左子树的最大节点或者右子树的最小节点替换
该节点*/
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
//这里在通过_key值大小查找需要删除的节点
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
// 开始删除
// 1、左为空(这里也处理了左右子树都为空的情况)
if (cur->_left == nullptr)
{
//处理删除的节点为根节点的情况
if (cur == _root)
{
_root = cur->_right;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
cur = nullptr;
}
// 2、右为空
else if (cur->_right == nullptr)
{
if (_root == cur)
{
_root = cur->_left;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
cur = nullptr;
}
// 3、左右都不为空
else
{
Node* minparent = cur;
Node* minright = cur->_right;
//寻找右子树的最小节点,一直往左走就是最小节点
while (minright->_left)
{
minparent = minright;
minright = minright->_left;
}
/*交换这两个节点的值,然后删除交换后的节点,当交换的节点为被
删除节点邻接的节点时,需要由被删除节点的右树接管*/
swap(cur->_key, minright->_key);
if (minparent->_right == minright)
{
minparent->_right = minright->_right;
}
else
{
minparent->_left = minright->_right;
}
delete minright;
}
return true;
}
}
2.5 查询
查询很简单直接根据大小走就行了
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return true;
}
}
return false;
}
2.6 拷贝构造
我们需要使用递归拷贝,递归时又需要有一个Node*的root参数作为递归条件,而我们使用拷贝构造时时没有_root参数的,所以这里我们通过调用子函数copy的方式完成拷贝。(我们写其他递归函数(比如递归插入,递归删除)时可以通过写一个GetRoot()的函数再调用时,以这样的形式调用递归对象.(GetRoot()))这里的拷贝构造无法通过GetRoot()的方法解决。
public:
//强制编译器生成默认构造函数
BSTree() = default;
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
private:
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
Node* newroot = new Node(root->_key);
newroot->_left = Copy(root->_left);
newroot->_right = Copy(root->_right);
return newroot;
}
我们如果不写拷贝构造,你会发现依旧是可以拷贝的,这是由于没有写析构函数,使用系统的值拷贝,如果写了析构函数,析构函数对同一个对象析构两次,这时就会出现错误了。
public:
~BSTree()
{
_Destory(_root);
}
private:
void _Destory(Node*& root)
{
if (root == nullptr)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
delete root;
root = nullptr;
}
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 BinarySearchTree
template<class K>
class BSTree
{
typedef BSTreeNode<K> Node;
private:
Node* _root = nullptr;
public:
bool Insert(const K& key)
{
//搜索二叉树为空时直接new一个树节点给_root即可
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
//插入树节点时需要将创建的新的节点与parent节点进行链接
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
{
//二叉搜索树不允许插入相同的_key值节点,插入失败返回false
return false;
}
}
//这一步只是将cur这个变量的值赋为新节点,没有进行链接
cur = new Node(key);
//判断需要插入到parent的左子树还是右子树
if (parent->_key < key)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
return true;
}
bool Erase(const K& key)
{
//删除一个节点,左为空时,需要parent节点接管右节点,右为空时接管左节点,左右都为空时,需要寻找这个被删除节点的左子树的最大节点
//或者右子树的最小节点替换该节点
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
//这里在通过_key值大小查找需要删除的节点
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
// 开始删除
// 1、左为空(这里也处理了左右子树都为空的情况)
if (cur->_left == nullptr)
{
if (cur == _root)
{
_root = cur->_right;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
cur = nullptr;
}
// 2、右为空
else if (cur->_right == nullptr)
{
if (_root == cur)
{
_root = cur->_left;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
delete cur;
cur = nullptr;
}
// 3、左右都不为空
else
{
Node* minparent = cur;
Node* minright = cur->_right;
//寻找右子树的最小节点,一直往左走就是最小节点
while (minright->_left)
{
minparent = minright;
minright = minright->_left;
}
//交换这两个节点的值,然后删除交换后的节点,当交换的节点为被删除节点邻接的节点时,需要由被删除节点的右树接管
swap(cur->_key, minright->_key);
if (minparent->_right == minright)
{
minparent->_right = minright->_right;
}
else
{
minparent->_left = minright->_right;
}
delete minright;
}
return true;
}
}
return false;
}
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key < key)
{
cur = cur->_right;
}
else if (cur->_key > key)
{
cur = cur->_left;
}
else
{
return true;
}
}
return false;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
///
bool InsertR(const K& key)
{
return _InsertR(_root, key);
}
bool FindR(const K& key)
{
_FindR(_root, key);
}
bool EraseR(const K& key)
{
return _EraseR(_root, key);
}
~BSTree()
{
_Destory(_root);
}
//强制编译器生成默认构造函数
BSTree() = default;
BSTree(const BSTree<K>& t)
{
_root = Copy(t._root);
}
BSTree<K>& operator=(BSTree<K> t)
{
swap(_root, t._root);
return *this;
}
private:
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
Node* newroot = new Node(root->_key);
newroot->_left = Copy(root->_left);
newroot->_right = Copy(root->_right);
return newroot;
}
void _Destory(Node*& root)
{
if (root == nullptr)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
delete root;
root = nullptr;
}
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* del = root;
if (root->_left == nullptr)
{
root = root->_right;
}
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
Node* min = root->_right;
while (min->_left)
{
min = min->_left;
}
swap(root->_key, min->_key);
return _EraseR(root->_right, key);
}
delete del;
return true;
}
}
bool _InsertR(Node*& root, const K& key)
{
if (root == nullptr)
{
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 _FindR(Node* root,const K& key)
{
if (root == nullptr)
return false;
if (root->_key > key)
return _FindR(root->_left, key);
else if (root->_key < key)
return _FindR(root->_right, key);
else
return true;
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
};
void TestTree1()
{
BSTree<int> t;
int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
for (auto e : a)
{
t.Insert(e);
}
t.InOrder();
for (auto e : a)
{
t.EraseR(e);
t.InOrder();
}
}
void TestTree2()
{
BSTree<int> t;
int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
for (auto e : a)
{
t.Insert(e);
}
t.InOrder();
BSTree<int> copy = t;
copy.InOrder();
BSTree<int> f;
f.Insert(1);
f.Insert(2);
f.Insert(3);
copy = f;
copy.InOrder();
}