您的位置:首页 > 其它

查找字典中具有某个公共前缀的所有单词

2015-12-26 13:58 411 查看
## Trie.h ##


#include<set>
#include<string>

#define BRANCH  26
using namespace std;
struct  Node
{
Node * next[BRANCH];
int prefix;   //此前缀的单词个数
bool isStr;
Node() : prefix(0), isStr(false)
{
memset(next, 0, sizeof(next));

}

};
class Trie
{
private:
Node * root;
set<string> *preSet;

void DFS(Node * ptr, string preStr)
{
for (int i = 0; i < BRANCH; ++i)
{
if (ptr->next[i] != NULL)
{
char chr[] = { 'a' + i, '\0' };
DFS(ptr->next[i], preStr + string(chr));
}

if (ptr->isStr)  //如果存在当前前缀的单词
{
this->preSet->insert(preStr);
}
}
}
//释放next数组
void freeNextArr(Node * ptr)
{
for (int i = 0; i < BRANCH; i++)
{
if (ptr->next[i] != NULL)
{
freeNextArr(ptr->next[i]);
}
}
delete ptr;
}

public:
Trie()
{
root = new Node();
preSet = new set<string>();

}

//插入一个单词;
void insert(const char * word)
{

if (word == NULL)
{
return;
}
Node * ptr = root;

while (*word)
{
if (ptr->next[*word - 'a'] == NULL)
{
ptr->next[*word - 'a'] = new Node();
}
ptr->prefix++;
ptr = ptr->next[*word - 'a'];
++word;
}

ptr->isStr = true;
}

//find the location of the word

Node* search(const char * word)
{
if (word == NULL)
{
return NULL;
}
Node* ptr = root;
while (ptr&&*word)
{
ptr = ptr->next[*word - 'a'];
++word;

}
return ptr;
}

//find the common prefix word set

set<string>* findCommonPrefix_set(const char * prefix)
{
if (prefix == NULL)
{
return NULL;
}

Node* ptr = search(prefix);  //find the loication of prefix
if (ptr == NULL)
{
return NULL;
}
DFS(ptr, string(prefix));

return this->preSet;
}

//clear the preSet
void clearPreSet()
{
this->preSet->clear();
}

//free the memory
void freeMemory()
{
this->freeNextArr(root);
delete this->preSet;

}

};


## 测试程序 ##


#include<iostream>
#include "Trie.h"
void main()
{
Trie comPreTrie;
comPreTrie.insert("abc");
comPreTrie.insert("abcd");
comPreTrie.insert("abcde");
comPreTrie.insert("abcdef");
comPreTrie.insert("abcdefg");
comPreTrie.insert("cde");

/*test the common prefix "abcd"
* 注:测试时输入合法字符,即a-z;
*本文程序不会做出检查
*/

Node* ptr = comPreTrie.search("abcd");

if (ptr)
{
printf("num of words prefix by \"abcd\" is %d\n", ptr->isStr ? ptr->prefix + 1 : ptr->prefix);
}
else
{
printf("ptr for \"abcd\" is null");
}

set<string> *comPreSet = comPreTrie.findCommonPrefix_set("abcd");
if (comPreSet)

{
for (set<string>::iterator it = comPreSet->begin(); it != comPreSet->end();++it)
{
printf("%s\n", (*it).c_str());
}

}
else
{
printf("comPreSet for \"abcd\" is null");
}
comPreTrie.clearPreSet();
printf("--------------------------------\n");

ptr = comPreTrie.search("word");
if (ptr)
{
printf("num of words prefix by \"word\" is %d\n",
ptr->isStr ? ptr->prefix + 1 : ptr->prefix);
}
else
{
printf("ptr for \"word\" is null\n");
}

comPreSet = comPreTrie.findCommonPrefix_set("word");
if (comPreSet)
{
for (set<string>::iterator it = comPreSet->begin();
it != comPreSet->end(); ++it)
{
printf("%s\n", (*it).c_str());
}
}
else
{
printf("comPreSet for \"word\" is null\n");
}
comPreTrie.clearPreSet();

// free memory
comPreTrie.freeMemory();
}


## 运行结果 ##


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: