[搜索]Trie树的实现
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树的实现的更多相关文章
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- Trie树(字典树) 最热门的前N个搜索关键词
方法介绍 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- Trie 树——搜索关键词提示
当你在搜索引擎中输入想要搜索的一部分内容时,搜索引擎就会自动弹出下拉框,里面是各种关键词提示,这个功能是怎么实现的呢?其实底层最基本的就是 Trie 树这种数据结构. 1. 什么是 "Tri ...
- cogs 647. [Youdao2010] 有道搜索框 Trie树 字典树
647. [Youdao2010] 有道搜索框 ★☆ 输入文件:youdao.in 输出文件:youdao.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 在有 ...
- 通过trie树实现单词自动补全
/** * 实现单词补全功能 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #incl ...
- Trie树的创建、插入、查询的实现
原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...
- Trie树(c++实现)
转:http://www.cnblogs.com/kaituorensheng/p/3602155.html http://blog.csdn.net/insistgogo/article/detai ...
- [转]双数组TRIE树原理
原文名称: An Efficient Digital Search Algorithm by Using a Double-Array Structure 作者: JUN-ICHI AOE 译文: 使 ...
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
随机推荐
- 微信支付v2开发(9) 标记客户投诉处理状态
本文介绍微信支付中如何标记客户投诉的处理状态. 一.API Api 的 url 为: https://api.weixin.qq.com/payfeedback/update?access_token ...
- PatentTips - Adaptive algorithm for selecting a virtualization algorithm in virtual machine environments
BACKGROUND A Virtual Machine (VM) is an efficient, isolated duplicate of a real computer system. Mor ...
- HTTP网络协议(三)
HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: 首部字段 说明 Cache-Control 控制缓存的行为 Connection 逐跳首部.连接 ...
- python3 求斐波那契数列(Fibonacci sequence)
输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...
- 怎样判断一个P2P平台是否靠谱?
判断一个网站,是否靠谱,是有规律可循的,P2P平台算是个新兴的电商类网站. 网上欺诈类的网站,不限于P2P,实在是太多了,真的有必要总结下最关键的几个靠谱指标. 最关键的2个 1.创始人和 ...
- 程序员的困境 - R中国用户组-炼数成金
原文:http://www.oschina.net/news/43389/the-plight-of-programmer 在大型公司中不能腐蚀自己的学习能力和时间能力. 最近我为一个内核程序员的职位 ...
- iTestin云测工具
软件概述 iTestin是免费服务移动App开发者的真机自动化云测试客户端工具.基于真实的智能终端设备录制一个测试脚本然后运行,并输出运行结果.覆盖Android和iOS两大设备平台,支持Pad/Ph ...
- POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列
POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...
- sublime text2 基本配置及结合Python 环境
参考: http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html http://www.zhihu.com/question/ ...
- 34、JZ2440上WIFI网卡使用
:http://wireless.kernel.org在这个网站上的document中有下面说有内容的介绍 1. 准备工作(虚拟机,开发板)及配置内核选择WIFI驱动1.1 选型:确定网卡的VID,P ...