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. Android 为什么要有handler机制?handler机制的原理

    为什么要有handler机制? 在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过 ...

  2. 洛谷 P1192 台阶问题

    P1192 台阶问题 题目描述 有N级的台阶,你一开始在底部,每次可以向上迈最多K级台阶(最少1级),问到达第N级台阶有多少种不同方式. 输入输出格式 输入格式: 输入文件的仅包含两个正整数N,K. ...

  3. Tomcat源代码阅读#1:classloader初始化

    Bootstrap 通过Tomcat的启动脚本能够看到启动的入口是在Bootstrap,来看下Bootstrap的main方法, /** * Main method and entry point w ...

  4. 16.用Spring Boot颠覆Java应用开发

    转自:https://www.cnblogs.com/aishangJava/p/5971288.html Java开发概述: 使用Java做Web应用开发已经有近20年的历史了,从最初的Servle ...

  5. 【Educational Codeforces Round 31 A】Book Reading

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

  6. 10.4 android输入系统_框架、编写一个万能模拟输入驱动程序、reader/dispatcher线程启动过程源码分析

    1. 输入系统框架 android输入系统官方文档 // 需FQhttp://source.android.com/devices/input/index.html <深入理解Android 卷 ...

  7. C++学习笔记(达内视频版)

    达内C++(陈宗权主讲) 第一天: 课程分为Core C++(标准C++.不依赖操作系统)和Unix C++. 1.配置bash,运行.sh文件. vi bash_profile 在"pat ...

  8. 数据结构与算法实验题 4.2 Who is the strongest

    数据结构与算法实验题 4.2 Who is the strongest ★实验任务 在神奇的魔法世界,召唤师召唤了一群的魁偶.这些魁偶排成一排,每个魁偶都有一个 战斗值.现在该召唤师有一个技能,该技能 ...

  9. 将OpenCV捕获的摄像头加载到picture控件中

    CRect rect; CStatic* pStc; CDC* pDC; HDC hDC; pStc = (CStatic*)GetDlgItem(IDC_CAM);//IDC_CAM是Picture ...

  10. ZYNQ7000 LVDS接口输出配置

    xilinx 7系列芯片不再支持LVDS33电平,在VCCO电压为3.3V的情况下无法使用LVDS25接口. 有些设计者想通过在软件中配置为LVDS25,实际供电3.3V来实现LVDS33也是无效的, ...