Trie树的C++实现

来源:互联网 发布:淘宝店铺彻底释放 编辑:程序博客网 时间:2024/06/05 04:57

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;elsetemp=temp->nextbrantch[k];i++;}if(NULL!=temp->word)return true;elsereturn 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;elsetemp=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;}elsetemp=temp->nextbrantch[k];i++;}if(0!=temp->count)print(temp);elsecout<<"没有此前缀的单词"<<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;}


0 0
原创粉丝点击