trie这种树也被称为线索,搜索树。

正如图

以下是用stl 的map来实现

class trie_item_c
{
public:
trie_item_c(){}
trie_item_c(const char nm)
{
name = nm;
} void set_name(const char nm)
{
name = nm;
} trie_item_c * get_child(const char nm)
{
map<const char ,trie_item_c*>::const_iterator it = children.find(nm);
if(it != children.end())
return it->second;
else
{
trie_item_c *cld = new trie_item_c(nm);
children.insert(pair <const char ,trie_item_c*>(nm,cld));
return cld;
}
}
bool find(const char* dic)
{
if(!dic || *dic == '\0')
{
if(children.end() != children.find('\0'))
return true;
return false;
}
const char* temp = dic;
map<const char ,trie_item_c*>::const_iterator it = children.find(*temp);
if(it == children.end())
return false;
return it->second->find(++temp);
}
void print()
{
printf("%c",name);
map<const char ,trie_item_c*>::const_iterator it = children.begin();
for(;it != children.end();it++)
{
it->second->print();
}
printf("\n");
}
virtual ~trie_item_c()
{
map<const char ,trie_item_c*>::const_iterator it = children.begin();
while(it != children.end())
{
delete it->second;
children.erase(it);
it = children.begin();
}
}
private:
map<const char ,trie_item_c*> children;
char name; };

以下代码是构建树的过程

for(const char* dic = lst.first_string();dic;dic = lst.next_string())
{ trie_item_c *temp = root;
for(int i=0;i<str_temp.str_len();i++)
{
char c = str_temp.get(i);
trie_item_c *item = temp->get_child(c);
temp = item;
}
//add one null child
temp->get_child('\0');
}

推断是否一个字root在,只需要调用root->find("book");您可以。



[搜索]Trie树的实现的更多相关文章

  1. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  2. Trie树(字典树) 最热门的前N个搜索关键词

    方法介绍 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  3. Trie 树——搜索关键词提示

    当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Tri ...

  4. cogs 647. [Youdao2010] 有道搜索框 Trie树 字典树

    647. [Youdao2010] 有道搜索框 ★☆   输入文件:youdao.in   输出文件:youdao.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 在有 ...

  5. 通过trie树实现单词自动补全

    /** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...

  6. Trie树的创建、插入、查询的实现

    原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...

  7. Trie树(c++实现)

    转:http://www.cnblogs.com/kaituorensheng/p/3602155.html http://blog.csdn.net/insistgogo/article/detai ...

  8. [转]双数组TRIE树原理

    原文名称: An Efficient Digital Search Algorithm by Using a Double-Array Structure 作者: JUN-ICHI AOE 译文: 使 ...

  9. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

随机推荐

  1. 3.lombok系列3:lombok的实验类特性

    转自:https://blog.csdn.net/54powerman/article/details/72516755 lombok除了已经推荐使用的基本功能,还维护了一个创新型的注解,有些功能有违 ...

  2. golang iota

    package main import ( "fmt" ) const ( Low = * (iota + ) Medium High ) func main() { //iota ...

  3. $(dom).each(index,ele){} 真正的jquery对象

    1.$(ele)才是each真正的jquery对象,而不是ele.$('.mt-story-info').each(function(index,ele){ if($('.mt-story-info' ...

  4. 有关Canvas的一点小事—canvas数据和像素点

    1.  canvas生成base64数据 canvas.toDataURL()生成的数据可以直接给image对象使用作为<img>显示在前端,也可以传给后台生成图片保存.前端生成保存图片的 ...

  5. 洛谷——P1598 垂直柱状图

    https://www.luogu.org/problem/show?pid=1598 题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字 ...

  6. [Firebase] Firebase Cloud Functions

    Firebase cloud functions is similar to AWS lambda or serverless. You can deploy you code which wrote ...

  7. 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例

    (本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...

  8. 以流的形式接收Http请求

    IEnumerable<ReportObject> IHttpReceiveReport.OnPost(System.Web.HttpContextBase context) { //[{ ...

  9. C#中防止程序多次运行

    C#中如何防止程序多次运行?只要在程序入口点函数Main()中的开始部分添加如注释部分的代码,就能快捷实现.   示例代码如下: using System; using System.Collecti ...

  10. disabled的值无法传递到action层

    假设想让表单不可输入的状态,我将表单设置为了: style="cursor:not-allowed;" disabled 可是这样设置之后就发现,在后台的action怎么都没有办法 ...