日常生活中我们经常使用字典来查找单词的中文意思,也会根据中文来查找所对应的汉语。前面我们知道二叉树实现可以迅速查找一个数据,而且可以插入和删除。
这里我们可以用搜索二叉树实现简单的字典,查找英文单词对应的汉语意思。
问题的描叙:
1:如何判断英文单词是否拼写正确;
2:如何统计单词出现的次数
这里我们可以用二叉树的K V算法;
v(value)是key的附属信息,这里我们可以理解为汉语意思。
一:字典的声明:
struct BinarySearchTreeNode
{
K _key;
V _value;//附加信息
BinarySearchTreeNode<K, V>* _left;
BinarySearchTreeNode<K, V>* _right;
BinarySearchTreeNode(const K&key, const V& value)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
{}
};
我们可以通过查找算法来实现,来判断单词是否拼写正确:
统计单词出现的次数,我们也可以通过查找来实现。
Node* _Find(Node* root, const K&key)
{
Node*cur = root;
while (cur)
{
if (cur->_key < key)//右边
{
return _Find(cur->_right, key);
}
else if (cur->_key>key)//左边
{
return _Find(cur->_left, key);
}
else
{
return cur;
}
}
return NULL;
}
二:字典的实现:
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
template<class K,class V>
struct BinarySearchTreeNode
{
K _key;
V _value;//附加信息
BinarySearchTreeNode<K, V>* _left;
BinarySearchTreeNode<K, V>* _right;
BinarySearchTreeNode(const K&key, const V& value)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
{}
};
template<class K,class V>
class BinarySearchTree
{
typedef BinarySearchTreeNode<K, V> Node;
public:
BinarySearchTree()
:_root(NULL)
{}
BinarySearchTree(const BinarySearchTree<K,V>&tree)//拷贝构造
:_root(NULL)
{
if (tree._root == NULL)
{
return;
}
queue<Node*>q;
q.push(tree._root);
while (!q.empty())
{
Node*cur = q.front();
Insert(cur->_key);
q.pop();
if (cur->_left)
{
q.push(cur->_left);
}
if (cur->_right)
{
q.push(cur->_right);
}
}
}
BinarySearchTree <K,V>&operator =(BinarySearchTree<K, V>&tree)
{
BinarySearchTree<K,V> tmp(tree);
swap(root, tmp._root);
return *this;
}
~BinarySearchTree()
{
_Destroy(_root);
}
Node* Find(const K&key)
{
return _Find(_root, key);
}
void Insert(const K&key,const V&value)
{
_Insert(_root, key, value);
}
void InOrder()
{
_InOrder(_root);
}
void RemoveR(const K&key)
{
_RemoveR(_root, key);
}
protected:
void _InOrder(Node*root)
{
Node*cur = root;
if (root == NULL)
{
return ;
}
_InOrder(cur->_left);
cout << cur->_key << " "<<cur->_value<<endl;
_InOrder(cur->_right);
}
bool _Insert(Node*& root, const K&key,const V&value)
{
if (root == NULL)
{
root = new Node(key, value);
return true;
}
if (root->_key < key)
{
return _Insert(root->_right, key, value);
}
else if (root->_key>key)
{
return _Insert(root->_left, key, value);
}
else
{
return NULL;//插入值相等
}
}
Node* _Find(Node* root, const K&key)
{
Node*cur = root;
while (cur)
{
if (cur->_key < key)//右边
{
return _Find(cur->_right, key);
}
else if (cur->_key>key)//左边
{
return _Find(cur->_left, key);
}
else
{
return cur;
}
}
return NULL;
}
//递归实现
bool RemoveR(Node*&root, const K&key)
{
if (root == NULL)
{
return NULL;
}
if (root->_key < key)
{
return RemoveR(root->_left, key);
}
else if (root->_key>key)
{
return RemoveR(root->_left, key);
}
else
{
if (root->_left == NULL)//左为空
{
root = root->_right;
}
else if (root->_right == NULL)//右为空
{
root = root->_left;
}
else
{
//左右都不为空
Node*parent = cur;
Node*subRight = cur->_right;
while (subRight->_left)
{
parent = subRight;
subRight = subRight->_left;
}
parent->_key = subRight->_key;
if (parent->_right = subRight)
{
parent->_right = subRight->_right;
}
else
{
parent->_left = subRight->_right;
}
delete subRight;
}
return true;//删除成功;
}
}
bool Remove(Node* &root, const K&key)
{
if (root == NULL)
{
return NULL;
}
Node*cur = root;
Node*parent = NULL;
while (cur)
{
if (cur->_key < key)//右边
{
return _Remove(cur->_right, key);
}
else if (cur->_key>key)//左边
{
return _Remove(cur->_left, key);
}
else
{
if (cur->_left ==NULL)//左为空
{
if (parent == NULL)
{
_root = cur->_right;
}
else
{
if (parent->_left == cur)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_right;
}
}
delete cur;
}
else if (cur->_right == NULL)//右为空
{
if (parent == NULL)
{
_root = cur->_left;
}
else
{
if (parent->_right == cur)
{
parent->_right = cur->_right;
}
else
{
parent->_left = cur->_left;
}
}
delete cur;
}
else
{
//左右都不为空(替换法删除)
Node*subParent = cur;
Node*subRight = cur->_right;
while (subRight->_left)
{
subParent = subRight;
subRight = subRight->_left;
}
subParent->_key = subRight->_key;//交换两个值
if (subParent->_right == cur)
{
subRight->_right = subParent->_right;
}
else
{
subRight->_left = subParent->_left;
}
delete subRight;
}
return true;
}
}
return false;//没找到
}
void _Destroy(Node*root)
{
Node*cur = root;
while (cur)
{
_Destroy(cur->_left);
_Destroy(cur->_right);
delete cur;
}
}
protected:
Node*_root;
};
int main()
{
BinarySearchTree<string, string> b;
b.Insert("sort", "排序");
b.Insert("tree", "树");
b.Insert("string", "字符串");
b.Insert("search", "搜索");
b.InOrder();
//查找单词
BinarySearchTreeNode<string, string> *ret =b.Find("sort");
cout << ret->_value << endl;
//统计单词出现的次数
string a[] = { "string", "string", "string", "string" };
BinarySearchTree<string,int> CountTree;
for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
BinarySearchTreeNode<string, int> *Node = CountTree.Find(a[i]);
if (Node)
{
Node->_value += 1;
}
else
{
CountTree.Insert(a[i], 1);
}
}
system("pause");
return 0;
}