Trie 树(转自 http://hi.baidu.com/luyade1987/item/7c1977f5e9015cdf6225d224)
来源:互联网 发布:软件项目计划书医疗 编辑:程序博客网 时间:2024/05/17 01:39
Trie树|字典树的简介及实现Trie,又称字典树、单词查找树,是一种树形结构,用于保存大量的字符串。它的优点是:利用字符串的公共前缀来节约存储空间。相对来说,Trie树是一种比较简单的数据结构.理解起来比较简单,正所谓简单的东西也得付出代价.故Trie树也有它的缺点,Trie树的内存消耗非常大.当然,或许用左儿子右兄弟的方法建树的话,可能会好点.其基本性质可以归纳为:1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。 2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。 3. 每个节点的所有子节点包含的字符都不相同。其基本操作有:查找 插入和删除,当然删除操作比较少见.我在这里只是实现了对整个树的删除操作,至于单个word的删除操作也很简单.搜索字典项目的方法为:(1) 从根结点开始一次搜索;(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。(4) 迭代过程……(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。其他操作类似处理./*Name: Trie树的基本实现 Author: MaiK Description: Trie树的基本实现 ,包括查找 插入和删除操作(卫星数据可以因情况而异) */#include<algorithm>#include<iostream>using namespace std;const int sonnum=26,base='a';struct Trie{ int num;//to remember how many word can reach here,that is to say,prefix bool terminal;//If terminal==true ,the current point has no following point struct Trie *son[sonnum];//the following point};Trie *NewTrie()// create a new node{ Trie *temp=new Trie; temp->num=1;temp->terminal=false; for(int i=0;i<sonnum;++i)temp->son[i]=NULL; return temp;}void Insert(Trie *pnt,char *s,int len)// insert a new word to Trie tree{ Trie *temp=pnt; for(int i=0;i<len;++i) { if(temp->son[s[i]-base]==NULL)temp->son[s[i]-base]=NewTrie(); else temp->son[s[i]-base]->num++; temp=temp->son[s[i]-base]; } temp->terminal=true;}void Delete(Trie *pnt)// delete the whole tree{ if(pnt!=NULL) { for(int i=0;i<sonnum;++i)if(pnt->son[i]!=NULL)Delete(pnt->son[i]); delete pnt; pnt=NULL; }}Trie* Find(Trie *pnt,char *s,int len)//trie to find the current word{ Trie *temp=pnt; for(int i=0;i<len;++i) if(temp->son[s[i]-base]!=NULL)temp=temp->son[s[i]-base]; else return NULL; return temp;}
- Trie 树(转自 http://hi.baidu.com/luyade1987/item/7c1977f5e9015cdf6225d224)
- sqlite使用手册(转自:http://hi.baidu.com/zifengshen/blog/item/671240f7e148f428720eec32.html)
- 关于sqlite3的性能(转自:http://hi.baidu.com/snailzone/blog/item/da9368662bc94f25aa184c2b.html)
- Q_DECLARE_METATYPE与qRegisterMetaType学习(转自:http://hi.baidu.com/cyclone/blog/item/01108bd40599b00fa18)
- BMP图像结构(转自:http://hi.baidu.com/zybabiao/blog/item/1357d5fb21c06567034f56ee.html)
- javax.servlet.Filter详解(一)(转自:http://hi.baidu.com/jxnuywf/blog/item/33d96889c1dfa7b00f2444c2.html)
- Qt/MFC迁移框架(Qt/MFC Migration Framework)(转自:http://hi.baidu.com/tigerwooz/blog/item/cc6105fa65808612a)
- sturts 标签 (转自 http://hi.baidu.com/%B7%C9%CE%C3/blog/item/40fef731b54cacab5fdf0e68.html)
- javax.servlet.Filter详解(二) 转自:http://hi.baidu.com/jxnuywf/blog/item/f3834aee93e2fdf8b2fb95cc.html
- SQL Server中动态执行SQL语句(转自:http://hi.baidu.com/senty/blog/item/5d6d36d3dc89a5073bf3cf3e.html)
- C# 静态构造函数详解(转自:http://hi.baidu.com/jiang_yy_jiang/blog/item/07d5cbe9ff6b4d32b90e2d5a.html)
- location.hash实现跨域iframe自适应(转自:http://hi.baidu.com/leolance/blog/item/2ec6f3ee5a7152c3d439c923.html)
- window下注册服务的命令(转自:http://hi.baidu.com/lijunliang/blog/item/cca6fb19179d306cdbb4bdf7.html)
- ScottGu: 宣布微软 AJAX CDN(转自:http://hi.baidu.com/jzwspace/blog/item/a63551a5d88f44fc9152ee5d.html)
- currentStyle IE和ff 获取外联样式(转自:http://hi.baidu.com/aganle/blog/item/86109bcf27d2b70b93457e41.html)
- currentStyle IE和ff 获取外联样式(转自:http://hi.baidu.com/aganle/blog/item/86109bcf27d2b70b93457e41.html)
- 修改vs2005,vs2008,vs2010调试默认浏览器(转自:http://hi.baidu.com/iaskall/blog/item/17c05a60b35f0551eaf8f803.htm)
- 人体24小时使用手册(转自:http://hi.baidu.com/zhaolianyuok/blog/item/08e79d126c086c1b34fa4169.html)
- 关于分离线程 .
- java中泛型学习2之类型参数(T)
- 【Tomcat】与Servlet之间的关系
- 单例模式 线程安全 singleton security
- gcc生成静态库和动态库
- Trie 树(转自 http://hi.baidu.com/luyade1987/item/7c1977f5e9015cdf6225d224)
- Wireshark中的一些SNMP相关的过滤器
- 感染者(并查集入门)
- 河南要求撤并学校前需进行听证-河南-撤并-听证
- 响应状态码
- xml小结
- android 两个程序之间如何通过 service 来监听广播
- 安全思维导图
- 上海撞死5名养护工肇事者称捡眼镜时打歪方向-撞死-养护工