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
- Trie 前缀树的c 实现
- Trie树的实现
- Trie树的实现
- 字典树(Trie树)的C程序实现代码
- Trie树数据结构的实现
- 一个Trie树的实现
- Trie树的C++实现
- Trie 树的简单实现
- Trie树的C++实现
- 字典树 Trie 的实现
- Trie树的C++实现
- Trie树的C++实现
- Trie树的C++实现
- Trie树的一种实现
- Trie树的java实现
- Trie树的c++实现
- Trie树的Java实现
- Trie树的DAT实现
- varchar和Nvarchar区别
- 为什么营养保健品需要坚持用3-6个月才会达到明显效果呢?
- 利用JS弹出层实现简单的动态提示“正在加载中,请稍等...
- Codeforces #282 div 1 C Helping People 题解
- Chrome多进程调试
- Trie树的C++实现
- 石子合并问题 (朴素区间DP&&GarsiaWachs算法)
- Bootstrap入门Demo——制作路径导航栏
- 利用RMAN跨平台迁移数据库
- Global Financial Applications uses the following Public tables
- jQuery EasyUI API 中文文档 - DataGrid数据表格
- 全国省市级联数据sql语句 mysql版
- "HelloWorld"
- 母版页和布局页