Trie树又称字符树,是一种树形结构,经常用于存储字典中的单词。
<span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif;">1)查找速度快,利用字符串的公共前缀来减小查找时间;</span>
<span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif;">#include<iostream></span>
using namespace std;
static const int maxnum=26;
//trie树节点
class trienode
{
public:
char* word;//节点存放的单词
int count;//统计该前缀的个数
trienode* nextbrantch[maxnum];//指向后续26个字符节点的指针
trienode():word(NULL),count(0)
{
for(int i=0;i<maxnum;i++)
nextbrantch[i]=NULL;
}
};
//trie树的实现
class trie
{
public:
trienode *proot;
trie(){ proot=new trienode;}
~trie(){ destroy(proot);}
void insert(const char *str);//插入单词
bool search(const char *str);//查询单词是否存在
void deleteword(const char* str);//删除单词
void printpre(const char* str);//输出以str为前缀的单词
void printall();//输出所有单词
int searchpre(const char *str);//返回以str为前缀的单词的个数
private:
void print(trienode *proot);//打印以proot为根的所有单词
void destroy(trienode* proot);//删除trie树
};
//trie树中插入单词
void trie::insert(const char *str)
{
if(NULL==str)
return;
int len=strlen(str);
int i=0,k=0;
trienode *temp=proot;
proot->count++;
while(i<len)
{
k=str[i]-'a';
if(k<0||k>maxnum-1)
return;
if(NULL==temp->nextbrantch[k])
{
temp->nextbrantch[k]=new trienode;
temp=temp->nextbrantch[k];
temp->count++;
}
else
{
temp=temp->nextbrantch[k];
temp->count++;
}
i++;
}
temp->word=new char[len+1];
strcpy(temp->word,str);
}
//查找单词是否在trie树中
bool trie::search(const char *str)
{
if(NULL==str)
return true;
int len=strlen(str);
int i=0,k=0;
trienode* temp=proot;
while(i<len)
{
k=str[i]-'a';
if(k<0||k>maxnum-1)
return false;
if(NULL==temp->nextbrantch[k])
return false;
else
temp=temp->nextbrantch[k];
i++;
}
if(NULL!=temp->word)
return true;
else
return false;
}
//返回以str为前缀的单词的个数
int trie::searchpre(const char *str)
{
if(NULL==str)
return 0;
int len=strlen(str);
int i=0,k=0;
trienode* temp=proot;
while(i<len)
{
k=str[i]-'a';
if(k<0||k>maxnum-1)
return 0;
if(NULL==temp->nextbrantch[k])
return 0;
else
temp=temp->nextbrantch[k];
i++;
}
return temp->count;
}
//删除单词
void trie::deleteword(const char* str)
{
if(NULL==str)
return;
int len=strlen(str);
int i=0,k=0;
trienode *temp=proot;
proot->count--;
while(i<len)
{
k=str[i]-'a';
temp=temp->nextbrantch[k];
temp->count--;
i++;
}
delete [] temp->word;
temp->word=NULL;
}
//输出以str为前缀的单词
void trie::printpre(const char* str)
{
if(NULL==str)
print(proot);
int len=strlen(str);
int i=0,k=0;
trienode *temp=proot;
while(i<len)
{
k=str[i]-'a';
if(NULL==temp->nextbrantch[k])
{
cout<<"没有此前缀的单词"<<endl;
return;
}
else
temp=temp->nextbrantch[k];
i++;
}
if(0!=temp->count)
print(temp);
else
cout<<"没有此前缀的单词"<<endl;
}
//输出所有单词
void trie::print(trienode* proot)
{
if(NULL==proot||0==proot->count)
return;
if(NULL!=proot->word)
cout<<proot->word<<endl;
for(int i=0;i<maxnum;i++)
if(NULL!=proot->nextbrantch[i])
print(proot->nextbrantch[i]);
}
void trie::printall()
{
print(proot);
}
void trie::destroy(trienode *proot)
{
if(proot==NULL)
return;
for(int i=0;i<maxnum;i++)
destroy(proot->nextbrantch[i]);
if(NULL!=proot->word)
{
delete [] proot->word;
proot->word=NULL;
}
delete proot;
proot=NULL;
}