<pre class="cpp" name="code">#include <iostream>
#include <string.h>
using namespace std;
//实现目标:用trie tree来存储英文单词及其中文意思
typedef enum
{
BRANCH_NODE = 0,
LEAF_NODE
}NK;
typedef struct trieNode
{
int subNum;
char* chinese;
struct trieNode* subTrieNode[26];
}TrieNode, *pTrieNode;
static char* english[10] = {
"hello",
"apple",
"app",
"help",
"blue",
"ultimate",
"professional",
"major",
"idea",
"professor"
};
static char* chinese[10] = {
"你好",
"苹果",
"应用;应用程序",
"帮助;救命",
"蓝色",
"最终的;极限的",
"专业的;职业的",
"专业;主要的",
"主意;想法",
"教师;教授"
};
/* 大写字母转换成小写 */
void UpperToLower(char* srcWord)
{
if(!srcWord)
return;
char* src = srcWord;
while(*src != '\0')
{
if(*src >= 'A' && *src <= 'Z')
*src = *src - 'A' + 'a';
src++;
}
}
static char tmpWord[50] = {'\0'}; //目前最长的单词没有超过50个字母的
/*
pStart参数为开始搜索的节点
word参数为前面已经获得的部分单词字符
endIndex参数为单词的结束位置
*/
void printSubTrieTreeWords(pTrieNode pStart, char* word, int endIndex)
{
if(!pStart)
return;
if(pStart->chinese != NULL){
tmpWord[endIndex] = '\0';
cout << tmpWord << endl;
}
if(pStart->subNum == 0)
return;
int i = 0;
for(; i < 26; i++)
{
if(pStart->subTrieNode[i] != NULL)
{
char c = i + 'a';
tmpWord[endIndex] = c;
printSubTrieTreeWords(pStart->subTrieNode[i], tmpWord, endIndex + 1);
tmpWord[endIndex] = '\0';
}
}
}
bool SearchWordInTrieTree(const char* word, pTrieNode pHead, char* chinese)
{
if(!word || !pHead || !chinese)
return false;
const char* pWord = word;
pTrieNode pCur = pHead;
while(*pWord != '\0')
{
int idx = *pWord - 'a';
if(pCur->subTrieNode[idx] == NULL){
return false;
}
pCur = pCur->subTrieNode[idx];
pWord++;
}
if(pCur != NULL)
{
if(pCur->chinese != NULL){
strcpy(chinese, pCur->chinese);
return true;
}
else
{
int len = strlen(word);
strcpy(tmpWord, word);
tmpWord[len] = '\0';
cout << "找不到单词: " << word << endl;
cout << "类似的单词有: " << endl;
printSubTrieTreeWords(pCur, tmpWord, len);
}
}
return false;
}
bool insertWord(const char* englishWord, const char* chinese, pTrieNode pHead)
{
if(!englishWord || !chinese || !pHead)
return false;
const char* pWord = englishWord;
pTrieNode pCur = pHead;
while(*pWord != '\0')
{
int idx = *pWord - 'a';
if(pCur->subTrieNode[idx] == NULL)
{
pCur->subTrieNode[idx] = (pTrieNode)malloc(sizeof(TrieNode));
memset(pCur->subTrieNode[idx], 0, sizeof(TrieNode));
}
pCur->subNum++;
pCur = pCur->subTrieNode[idx];
pWord++;
}
if(pCur != NULL)
{
pCur->chinese = (char*)malloc(sizeof(char) * (strlen(englishWord) + 1));
strcpy(pCur->chinese, chinese);
}
return true;
}
pTrieNode init()
{
pTrieNode pHead = (pTrieNode)malloc(sizeof(TrieNode));
if(pHead == NULL){
cout << "malloc pHead fail!!!"<< endl;
return NULL;
}
memset(pHead, 0, sizeof(TrieNode));
return pHead;
}
void deInit(pTrieNode pHead)
{
if(pHead != NULL)
{
int i = 0;
if(pHead->subNum != 0){
for(; i < 26; i++)
{
if(pHead->chinese){
free(pHead->chinese);
pHead->chinese = NULL;
}
deInit(pHead->subTrieNode[i]);
}
}
free(pHead);
pHead = NULL;
}
}
int main()
{
pTrieNode pHead = NULL;
pHead = init();
for(int i = 0; i < 10; i++)
{
insertWord(english[i], chinese[i], pHead);
}
char word[50];
char mean[1024];
while(1)
{
cout << "输入要查询的单词: " << endl;
cin >> word;
UpperToLower(word);
if(SearchWordInTrieTree(word, pHead, mean))
{
cout << "单词" << word << "的意思是: " << mean << endl;
}
else
{
#if 0
cout << "需要存入该单词请输入 yes 否则输入 no " << endl;
char flag[10];
cin >> flag;
if(strncmp(flag, "yes", 3) == 0)
{
cout << "请输入该单词的中文意思: " << endl;
cin >> mean;
bool ret = insertWord(word, mean, pHead);
if(ret)
cout << "单词" << word << " 保存成功" << endl;
else
cout << "单词" << word << " 保存失败" << endl;
}
#endif
}
}
deInit(pHead);
return 0;
}
运行结果: