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. 微信支付v2开发(10) 全网发布

    关键字:微信公众平台 微信支付 全网发布 作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/wxpay-publish.html 在这篇微信公众平台开发教程中, ...

  2. Android Service com.android.exchange.ExchangeService has leaked ServiceConnection

    启动Android项目的时候,clean  Project的时候,报错: android.app.ServiceConnectionLeaked: Service com.android.exchan ...

  3. JQuery的index()函数

    1.index(),这里的索引从0开始计数. jQueryObject.index( [ object ] ):1.1 如果没有指定参数object,则返回当前元素在其所有同辈元素中的索引位置.1.2 ...

  4. 洛谷——P1179 数字统计

    https://www.luogu.org/problem/show?pid=1179 题目描述 请统计某个给定范围[L, R]的所有整数中,数字 2 出现的次数. 比如给定范围[2, 22],数字 ...

  5. amazeui-datatables(登录注册界面用到)

    amazeui-datatables(登录注册界面用到) 一.总结 amazeui-datatables:DataTables 插件 Amaze UI 集成,只修改了样式和默认显示语言,其他参数同官方 ...

  6. CSS笔记 - fgm练习 2-8 - 简易日历

    <style> *{margin: 0; padding: 0} .outer{ width: 240px; margin: 10px auto; background: #f0f0f0; ...

  7. javascript面对对象编程 之继承

    上一篇博客中为大家介绍了javascript面向对象编程原则的封装,今天为大家介绍继承.在javascript中没有类的概念,全部不能像c#.java语言那样.直接的用类去继承类.比方如今有比方.如今 ...

  8. mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure

    err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...

  9. keil出现蓝色小箭头

  10. 26、驱动调试之根据oops信息和堆栈确定出错的代码

    a.驱动作为模块:1. 根据pc值确定该指令属于内核还是外加的模块pc=0xbf000018 它属于什么的地址?是内核还是通过insmod加载的驱动程序?先判断是否属于内核的地址: 看System.m ...