#pragma once
#include<iostream>
namespace K
{
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();
}
}
namespace KV
{
template<class K, class V>
struct BSTreeNode
{
BSTreeNode(const K& key, const V& value)
:left(nullptr)
, right(nullptr)
, _key(key)
, _value(value)
{}
BSTreeNode<K, V>* left;
BSTreeNode<K, V>* right;
K _key;
V _value;
};
template<class K, class V>
struct BSTree
{
typedef BSTreeNode<K, V> Node;
public:
BSTree()
:_root(nullptr)
{}
bool Insert(const K& key, const V& value)
{
if (_root == nullptr)
{
_root = new Node(key, value);
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, value);
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;
cur->_value = Min_right->_value;
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 << ":";
cout << root->_value << endl;
_InOrder(root->right);
}
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
{
cur = cur->right;
}
}
return nullptr;
}
private:
Node* _root;
};
void TestBSTree1()
{
BSTree<string, string> dict;
dict.Insert("left", "左");
dict.Insert("right", "右");
dict.Insert("sort", "排序");
dict.Insert("map", "地图,映射");
string str;
while (cin >> str)
{
auto ret = dict.Find(str);
if (ret)
{
cout << "对应中文解释是:" << ret->_value << endl;
}
else
{
cout << "查不到" << endl;
}
}
}
void TestBSTree2()
{
string arr[] = { "苹果", "苹果", "苹果", "苹果", "苹果", "草莓", "草莓", "草莓", "香蕉" };
BSTree<string, int> count_tree;
for (auto& str : arr)
{
auto ret = count_tree.Find(str);
if (ret)
{
ret->_value++;
}
else
{
count_tree.Insert(str, 1);
}
}
count_tree.InOrder();
}
}