[搜索]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. 常见的树形结构 ...
随机推荐
- [D3] Add image to the node
We can create node with 'g' container, then append 'image' to the nodes. // Create container for the ...
- NHibernate之旅(3):探索查询之NHibernate查询语言(HQL)
本节内容 NHibernate中的查询方法 NHibernate查询语言(HQL) 1.from子句 2.select子句 3.where子句 4.order by子句 5.group by子句 实例 ...
- 编程——C语言的问题,堆栈
堆和栈的区别一.预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结 ...
- 小程序踩坑记- tabBar.list[3].selectedIconPath 大小超过 40kb
重新启动微信小程序编辑器的时候遇到了这样的一个问题: tabBar.list[3].selectedIconPath 大小超过 40kb 微信小程序开发的过程之中总会出现这样或者那样的错误,需要我们耐 ...
- git 配置用户名email地址,设置密码
- lettuce--Advanced Redis client
redis官方提供的java client: git地址:https://github.com/mp911de/lettuceAdvanced Redis client for thread-safe ...
- AMP Physical Link Creation And Disconnect
A flow diagram of the AMP link establishment and detachment of a connection between two devices is s ...
- iOS Objc Runtime 教程+实例Demo
样例Demo 欢迎给我star!我会继续分享的. 概述 Objc Runtime使得C具有了面向对象能力,在程序执行时创建,检查.改动类.对象和它们的方法.Runtime是C和汇编编写的,这里http ...
- spark1.3.1使用基础教程 分类: B8_SPARK 2015-04-28 11:10 1651人阅读 评论(0) 收藏
spark可以通过交互式命令行及编程两种方式来进行调用: 前者支持scala与python 后者支持scala.python与java 本文参考https://spark.apache.org/d ...
- Ubuntu 16.04/16.10安装 KDE Plasma
KDE目前的最新版本是Plasma 5.7(2016-10).Ubuntu默认的桌面环境是Unity,下面记录怎么在Ubuntu 16.04/16.10上安装KDE. Ubuntu 16.04/16. ...