trie树(字典树)的部分简单实现
什么是trie树(字典树)?
trie树是一种用于快速检索的多叉树结构。和二叉查找树不同,在trie树中,每个结点上并非存储一个元素。
trie树把要查找的关键词看作一个字符序列。并根据构成关键词字符的先后顺序构造用于检索的树结构。在trie树上进行检索类似于查阅英语词典。 一棵m度的trie树或者为空,或者由m
棵m度的trie树构成。 例如,电子英文词典,为了方便用户快速检索英语单词,可以建立一棵trie树。例如词典由下面的单词构成:a、b、c、aa、ab、ac、ba、ca、aba、abc、baa
bab、bac、cab、abba、baba、caba、abaca、caaba.
下图形象的展示下trie树:

例如在上面图中的trie树中查找单词 aba的流程:
(1)在trie树上进行检索总是始于根结点。
(2)取得要查找关键词的第一个字母(例如 a ),并根据该字母选择对应的子树并转到该子树继续进行检索。
(3)在相应的子树上,取得要查找关键词的第二个字母(例如 b),并进一步选择对应的子树进行检索。
(4) ...
(5)在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。
我的实现代码如下:
#include <iostream>
#include <cstdlib>
#include <cstdio> #define null NULL
const int num_chars = ; /*普通的树结点*/
template <typename Entry>
class tree_node
{
public:
Entry data;
tree_node* first_child;
tree_node* next_sibling;
tree_node():first_child(null),next_sibling(null){}
tree_node(const Entry& x):data(x),first_child(null),next_sibling(null){}
}; /*trie树,是一种用于快速检索的多叉树结构。*/ //trie树的实现
class trie
{
protected:
class trie_node //定义trie树的结点
{
char* data;
trie_node* branch[num_chars]; //常量num_chars = 26
trie_node();
};
trie_node* root; //根节点
public:
trie(); //无参数构造函数
trie(trie& tr); //复制构造函数
virtual ~trie(); //析构函数
int trie_search(const char* word,char* entry) const; //查找操作
int insert(const char* word,const char* entry); //插入操作
int remove(const char* word,char* entry); //删除操作 }; //trie_node的构造函数
trie::trie_node::trie_node()
{
data = null;
for(int i=;i<num_chars;i++)
branch[i] = null;
} //trie的构造函数
trie::trie()
{
root = null;
} //trie的检索,查找在某个字符路径结点上的存放的值,并将其放在entry中。比如找对应"abc"的结点上的值并且存放在entry中。
int trie::trie_search(const char* word,char* entry) const
{
int position = ;
char char_code;
trie_node* loaction = root;
while(location != null && *word != null)
{
if(*word >= 'A' && *word < 'Z') char_code = *word - 'A';
else if(*word >= 'a' && *word <= 'z') char_code = *word - 'a';
else return ;
location = location->branch[char_code];
position++;
word++;
}
if(location != null && location->data != null)
{
strcpy(entry,location->data);
return ;
}
else return ;
} //插入操作,此时是往指定的字符串的结点上存放指定的字符串。比如在"abc"位置上防"jeaven"。
int trie::insert(const char* word,const char* entry)
{
int result = ;
int position = ;
if(root == NULL) root = new tire_node();
char char_code;
tire_node* location = root;
while(location != null && *word != null)
{
if(*word >= 'a' && *word <= 'z') char_code = *word - 'a';
else if(*word >= 'A' && *word <= 'Z') char_code = *word - 'A';
else return ;
if(location->branch[char_code] == null)
location->branch[char_code] = new trie_node();
location = location->branch[char_code];
position++;
word++;
} if(location->branch[char_code] != null) result = ;
else
{
location->data = new char[strlen(entry)+];
strcpy(location->data,entry);
}
return result;
} //删除操作,删除在某字符串路径上的结点存放的值,并且将其放到entry中
int trie::remove(const char* word,char* entry)
{
if(root == null) return ;
trie_node* cur = root;
char char_code;
while(*word != null)
{
if(*word >= 'a' && *word <= 'z') char_code = *word - 'a';
else if(*word >= 'A' && *word <= 'Z') char_code = *word - 'A';
else return ;
if(cur->branch[char_code] != null) cur = cur->branch[char_code];
}
*entry = cur->data;
delete cur;
return ;
}
参考:[1] https://www.cnblogs.com/konrad/p/7746030.html
[2] http://blog.csdn.net/guin_guo/article/details/48858339
trie树(字典树)的部分简单实现的更多相关文章
- 剑指Offer——Trie树(字典树)
剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...
- AC自动机——1 Trie树(字典树)介绍
AC自动机——1 Trie树(字典树)介绍 2013年10月15日 23:56:45 阅读数:2375 之前,我们介绍了Kmp算法,其实,他就是一种单模式匹配.当要检查一篇文章中是否有某些敏感词,这其 ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- Trie树(字典树)的介绍及Java实现
简介 Trie树,又称为前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也 ...
- Trie树 - 字典树
1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是最大限 ...
- cogs 293. [NOI 2000] 单词查找树 Trie树字典树
293. [NOI 2000] 单词查找树 ★★☆ 输入文件:trie.in 输出文件:trie.out 简单对比时间限制:1 s 内存限制:128 MB 在进行文法分析的时候,通常需 ...
- [LintCode] Implement Trie 实现字典树
Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...
- Trie树|字典树(字符串排序)
有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
随机推荐
- NAT 模式下有两个虚拟机 网段不一样,一台可上网,可ping通,一台上不了网且ping不通
NAT 模式下有两个虚拟机 网段不一样,一台可上网,可ping通,一台上不了网且ping不通直接修改网段的话,会登录不上去,解决方法:设置>网络适配器>高级>生成mac地址重新登陆即 ...
- 【搜索】n的约数
题目链接:传送门 [题解]: 考察dfs和质因数分解,首先开一个prime数组. 参数解释: 1.当前值的大小.[利用题目的n来控制范围] 2.控制下界,每次都是以某一个质数开始搜索, pos 3.控 ...
- Angular CDK Overlay 弹出覆盖物
为什么使用Overlay? Overlay中文翻译过来意思是覆盖物,它是Material Design components for Angular中针对弹出动态内容这一场景的封装,功能强大.使用方便 ...
- 并不对劲的CF1194E:Count The Rectangles
题意 有\(n\)(\(n\leq 5000\))个平行于x轴或平行于y轴的线段.求这些线段围成了多少个长方形.由多个长方形拼成的也算. 题解 考虑暴力的做法:先分别计算每条横着的线与哪些竖着的线有交 ...
- HTTP协议探究(序章)
1 HTTP协议基于TCP协议 (1)TCP三次握手连接 HTTP客户端(Chrome浏览器): IP:192.168.1.47 端口:59875 MSS:1460 HTTP服务器(Nginx服务器) ...
- git基本操作及实用工具
//git1.安装客户端git Git-2.9.3-rebase-i-64-bit.exe2.安装完成后打开git bashgit config --global user.name "li ...
- 初学java4 循环的使用
for循环 for(初始条件;循环终止条件;循环结束后所执行代码){ 循环体 } while循环 while(循环终止条件){ 循环体 } do while循环 do{ }while(循环终止条件);
- 2.1 使用JAXP 对 xml文档进行DOM解析
//使用 jaxp 对xml文档进行dom解析 public class Demo2 { //必要步骤 @Test public void test() throws Exception { //1. ...
- 【Day3】项目实战。百度针对Xpath的反爬策略和解决方式
import lxml.etree as le with open('edu.html','r',encoding='utf-8') as f: html = f.read() html_x = le ...
- CAFFE(FAQ.2):Ubuntu 配置caffe 框架之数据库读取,错误解决:ImportError: No module named leveldb解决办法
Z: 在安装了caffe框架后需要读取大量的数据进行学习训练.比如在MNIST识别训练中,一般直接读图片会比较耗时,我们一般将图片转存为数据库中.目前主流的数据库有以下两种选择: LevelDB Lm ...