目录
常规递归写法:
循环写法:
namespace key
{
//二叉链节点
template
struct BSTreeNode
{
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
};
template
class BSTree
{
typedef BSTreeNode Node;
public:
//————————————————————————————--递归玩法——————————————————————————————————————
//默认构造函数
BSTree() = default;//强制生成默认构造
//拷贝构造
BSTree(const BSTree& t)
{
_root = Copy(t._root);
}
//赋值(深拷贝)
BSTree& operator=(BSTree t)//传参调用拷贝构造进行深拷贝;
{
swap(_root, t._root);
return *this;
}
//析构函数
~BSTree()
{
Destroy(_root);
_root = nullptr;
}
//查找
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);
}
//中序遍历
void InOrder()
{
_InOrder(_root);
}
private:
Node* _root = nullptr;
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
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;
}
}
bool _InsertR(Node*& root, const K& key)
{
//root引用父亲节点的左右孩子指针,直接可以连接新建结点;
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 _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;
}
//root的左子树的最大节点一定没有右结点,所以一定会走这里;
else if (root->_right == nullptr)
{
root = root->_left;
}
else
{
Node* maxleft = root->_left;
while (maxleft->_right)
{
maxleft = maxleft->_right;
}
swap(maxleft->_key, root->_key);//交换当前节点的左子树的最大节点与当前节点的值;
return _EraseR(root->_left, key);//再递归调用一次;
}
delete del;
return true;
}
}
void Destroy(Node* root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
void Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* newNode = new Node(root->_key);
newNode->_left = Copy(root->_left);
newNode->_right = Copy(root->_right);
return newNode;
}
};
}
namespace key
{
//二叉链节点
template
struct BSTreeNode
{
BSTreeNode* _left;
BSTreeNode* _right;
K _key;
BSTreeNode(const K& key)
:_left(nullptr)
, _right(nullptr)
, _key(key)
{}
};
template
class BSTree
{
typedef BSTreeNode Node;
public:
//-------------------------------循环玩法-----------------------------------------------
//插入
bool Insert(const K& key)
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else
{
//相等;
return false;
}
}
cur = new Node(key);
if (parent->_key > key)
parent->_left = cur;
else
parent->_right = cur;
return true;
}
}
//删除
bool Erase(const K& key)
{
// 原则:
// 1.根据key值先找到这个这个结点,存在就删除它,不存在就返回false;
// 2.观察这个需要被删除的结点(爸爸结点)有几个孩子,根据孩子数量处理好善后工作,
// 3.如果爸爸结点只有一个孩子(孙子结点)那么就把他给爸爸结点的父亲(爷爷结点);
// 4.如果爸爸结点有两个孩子,那么就找爸爸结点左子树的最大结点或者右子树的最小结点来替换爸爸结点并删除左大或者右小;
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
{
//根据这个即将被删除的结点的孩子的数量判断
//该结点没有左孩子
if (cur->_left == nullptr)
{
//如果要删除的节点为根节点,那么他没有父亲结点;
if (cur == _root)
{
_root = cur->_right;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_right;
}
else if (parent->_right == cur)
{
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 if (parent->_right == cur)
{
parent->_right = cur->_left;
}
}
delete cur;
}
//该结点两个孩子都有
else
{
//右子树的最小key值结点
Node* minRight = cur->_right;
Node* pminRight = cur;
while (minRight->_left)
{
pminRight = minRight;
minRight = minRight->_left;
}
//minRight此时一定是没有左孩子了,但可能存在右孩子,所以需要minRight的父亲结点接管右孩子;
swap(minRight->_key, cur->_key);
if (pminRight->_right == minRight)
{
pminRight->_right = minRight->_right;
}
else
{
pminRight->_left = minRight->_right;
}
delete minRight;
}
return true;
}
}
return false;
}
//查找
bool Find(const K& key)
{
if (_root == nullptr)
return false;
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);
}
//默认构造函数
BSTree() = default;//强制生成默认构造
//拷贝构造
BSTree(const BSTree& t)
{
_root = Copy(t._root);
}
//赋值(深拷贝)
BSTree& operator=(BSTree t)//传参调用拷贝构造进行深拷贝;
{
swap(_root, t._root);
return *this;
}
//析构函数
~BSTree()
{
Destroy(_root);
_root = nullptr;
}
private:
Node* _root = nullptr;
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
void Destroy(Node* root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
void Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* newNode = new Node(root->_key);
newNode->_left = Copy(root->_left);
newNode->_right = Copy(root->_right);
return newNode;
}
};
}