#pragma once
template<class K>
struct BSTreeNode
{
BSTreeNode(const K& key)
:left(nullptr)
, right(nullptr)
, _key(key)
{}
BSTreeNode<K>* left;
BSTreeNode<K>* right;
K _key;
};
template<class K>
struct BSTree
{
typedef BSTreeNode<K> Node;
public:
BSTree()
:_root(nullptr)
{}
bool Insert(const K& key)
{
if (_root == nullptr)
{
_root = new Node(key);
return true;
}
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
parent = cur;
if (cur->_key > key)
{
cur = cur->left;
}
else if (cur->_key < key)
{
cur = cur->right;
}
else
{
return false;
}
}
cur = new Node(key);
if (parent->_key > key)
{
parent->left = cur;
}
else
{
parent->right = cur;
}
return true;
}
bool Earse(const K& key)
{
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 (parent == nullptr)
{
_root = cur->right;
return true;
}
Node* child = cur->right;
if (parent->left == cur)
{
parent->left = child;
}
else
{
parent->right = child;
}
delete cur;
}
else if (cur->right == nullptr)
{
if (parent == nullptr)
{
_root = cur->left;
return true;
}
Node* child = cur->left;
if (parent->left == cur)
{
parent->left = child;
}
else
{
parent->right = child;
}
delete cur;
}
else
{
Node* Min_parent = cur;
Node* Min_right = cur->right;
while (Min_right->left)
{
Min_parent = Min_right;
Min_right = Min_right->left;
}
cur->_key = Min_right->_key;
if (Min_parent->left == Min_right)
{
Min_parent->left = Min_right->right;
}
else
{
Min_parent->right = Min_right->right;
}
delete Min_right;
}
return true;
}
}
return false;
}
void InOrder()
{
_InOrder(_root);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->left);
cout << root->_key << " ";
_InOrder(root->right);
}
bool Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_key == key)
{
return true;
}
else if (cur->_key > key)
{
cur = cur->left;
}
else
{
cur = cur->right;
}
}
return false;
}
bool InsertR(const K& key)
{
_InsertR(key, _root);
return true;
}
bool _InsertR(const K& key, Node*& root)
{
if (root == nullptr)
{
root = new Node(key);
return true;
}
if (root->_key > key)
{
return _InsertR(key, root->left);
}
else if (root->_key < key)
{
return _InsertR(key, root->right);
}
else
{
return false;
}
}
Node* FindR(const K& key)
{
return _FindR(key, _root);
}
Node* _FindR(const K& key, Node* root)
{
if (key == root->_key || key == nullptr)
{
return root;
}
if (root->_key > key)
{
_FindR(key, root->left);
}
if (root->_key < key)
{
_FindR(key, root->right);
}
}
bool EarseR(const K& key)
{
return _EarseR(key, _root);
}
bool _EarseR(const K& key, Node*& root)
{
if (root == nullptr)
{
return false;
}
if (root->_key > key)
{
_EarseR(key, root->left);
}
else if (root->_key < key)
{
_EarseR(key, root->right);
}
else
{
Node* del = root;
if (root->left == nullptr)
{
root = root->right;
delete del;
}
else if (root->right == nullptr)
{
root = root->left;
delete del;
}
else
{
Node* min_right = root->right;
while (min_right->left)
{
min_right = min_right->left;
}
swap(root->_key, min_right->_key);
_EarseR(key, root->right);
}
}
}
private:
Node* _root;
};
void TestBSTree()
{
BSTree<int> b;
int a[] = { 5, 3, 6, 2, 4, 7 };
for (auto e : a)
{
b.InsertR(e);
}
b.EarseR(7);
b.InOrder();
}
|