对于一个二叉搜索树
template<class K,class V>
struct BSTreeNode
{
typedef BSTreeNode<K, V> Node;
BSTreeNode(const K& key,const V& val):_key(key),_value(val){}
K _key;
V _value;
Node* _left = nullptr;
Node* _right = nullptr;
};
指出二叉搜索树:
指出二分查找:
int num[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
bool Insert(const K& key, const V& value)
{
if (_root == nullptr)
{
_root = new Node(key, value);
return true;
}
else
{
bool right = true;
Node* p = _root;
Node* c = _root;
while (c != nullptr)
{
p = c;
if (key > c->_key)
{
right = true;
c = p->_right;
}
else if(key < c->_key)
{
right = false;
c = p->_left;
}
else
{
return false;
}
}
c = new Node(key, value);
if (right)
{
p->_right = c;
}
else
{
p->_left = c;
}
return true;
}
}
Node* Find(const K& key,Node** p = nullptr)
{
Node* ptr = _root;
while (ptr)
{
if (key > ptr->_key)
{
if (p)*p = ptr;
ptr = ptr->_right;
}
else if (key < ptr->_key)
{
if (p)*p = ptr;
ptr = ptr->_left;
}
else
return ptr;
}
return ptr;
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << '{' << root->_value << '}' << ' ';
_InOrder(root->_right);
}
bool Erase(const K& key)
{
Node* par = _root;
Node* ptr = Find(key,&par);
if (!ptr)return false;
if (ptr == par)
{
delete ptr;
_root = nullptr;
return true;
}
if(!ptr->_left&&!ptr->_right)
{
if(par->_left == ptr)
par->_left = nullptr;
if(par->_right == ptr)
par->_right = nullptr;
delete ptr;
return true;
}
else if (!ptr->_left && ptr->_right)//左空右不空
{
if (par->_left == ptr)
{
par->_left = ptr->_right;
}
if (par->_right == ptr)
{
par->_right = ptr->_right;
}
delete ptr;
return true;
}
else if(ptr->_left && !ptr->_right)
{
if (par->_left == ptr)
{
par->_left = ptr->_left;
}
if (par->_right == ptr)
{
par->_right = ptr->_left;
}
delete ptr;
return true;
}
else//两边均不为空直接替换操作
{
//找左侧最大节点
Node* Max = ptr->_left;
Node* Maxp = ptr;
while (Max->_right != nullptr)
{
Maxp = Max;
Max = Max->_right;
}
//交换
ptr->_key = Max->_key;
ptr->_value = Max->_value;
//删除MAX的位置
if (Maxp == ptr)
{
ptr->_left = Max->_left;
}
else if (Max->_left)
{
Maxp->_right = Max->_left;
}
else
{
Maxp->_right = Max->_right;
}
//删除Max
delete Max;
return true;
}
return false;
}
#pragma once
#include<iostream>
#include<string>
using namespace std;
template<class K,class V>
struct BSTreeNode
{
public:
typedef BSTreeNode<K, V> Node;
BSTreeNode(const K& key,const V& val):_key(key),_value(val){}
K _key;
V _value;
Node* _left = nullptr;
Node* _right = nullptr;
};
template<class K, class V>
class BSTree
{
typedef BSTreeNode<K, V> Node;
public:
bool Insert(const K& key, const V& value)
{
if (_root == nullptr)
{
_root = new Node(key, value);
return true;
}
else
{
bool right = true;
Node* p = _root;
Node* c = _root;
while (c != nullptr)
{
p = c;
if (key > c->_key)
{
right = true;
c = p->_right;
}
else if (key < c->_key)
{
right = false;
c = p->_left;
}
else
{
return false;
}
}
c = new Node(key, value);
if (right)
{
p->_right = c;
}
else
{
p->_left = c;
}
return true;
}
}
Node* Find(const K& key,Node** p = nullptr)
{
Node* ptr = _root;
while (ptr)
{
if (key > ptr->_key)
{
if (p)*p = ptr;
ptr = ptr->_right;
}
else if (key < ptr->_key)
{
if (p)*p = ptr;
ptr = ptr->_left;
}
else
return ptr;
}
return ptr;
}
bool Erase(const K& key)
{
Node* par = _root;
Node* ptr = Find(key,&par);
if (!ptr)return false;
if (ptr == par)
{
delete ptr;
_root = nullptr;
return true;
}
if(!ptr->_left&&!ptr->_right)
{
if(par->_left == ptr)
par->_left = nullptr;
if(par->_right == ptr)
par->_right = nullptr;
delete ptr;
return true;
}
else if (!ptr->_left && ptr->_right)//左空右不空
{
if (par->_left == ptr)
{
par->_left = ptr->_right;
}
if (par->_right == ptr)
{
par->_right = ptr->_right;
}
delete ptr;
return true;
}
else if(ptr->_left && !ptr->_right)
{
if (par->_left == ptr)
{
par->_left = ptr->_left;
}
if (par->_right == ptr)
{
par->_right = ptr->_left;
}
delete ptr;
return true;
}
else//两边均不为空直接替换操作
{
//找左侧最大节点
Node* Max = ptr->_left;
Node* Maxp = ptr;
while (Max->_right != nullptr)
{
Maxp = Max;
Max = Max->_right;
}
//交换
ptr->_key = Max->_key;
ptr->_value = Max->_value;
//删除MAX的位置
if (Maxp == ptr)
{
ptr->_left = Max->_left;
}
else if (Max->_left)
{
Maxp->_right = Max->_left;
}
else
{
Maxp->_right = Max->_right;
}
//删除Max
delete Max;
return true;
}
return false;
}
void InOrder(){_InOrder(_root);}
private:
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_key << '{' << root->_value << '}' << ' ';
_InOrder(root->_right);
}
Node* _root = nullptr;
};
void TestBSTree()
{
BSTree<string, string> dict;
dict.Insert("insert", "插入");
dict.Insert("erase", "删除");
dict.Insert("left", "左边");
dict.Insert("string", "字符串");
string str;
while (cin >> str)
{
auto ret = dict.Find(str);
if (ret)
{
cout << str << ":" << ret->_value << endl;
}
else
{
cout << "单词拼写错误" << endl;
}
}
//string strs[] = { "苹果", "西瓜", "苹果", "樱桃", "苹果", "樱桃", "苹果", "樱桃", "苹果" };
统计水果出现的次
//BSTree<string, int> countTree;
//for (auto str : strs)
//{
// auto ret = countTree.Find(str);
// if (ret == NULL)
// {
// countTree.Insert(str, 1);
// }
// else
// {
// ret->_value++;
// }
//}
//countTree.InOrder();
}