什么是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树(字典树)的部分简单实现的更多相关文章

  1. 剑指Offer——Trie树(字典树)

    剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...

  2. AC自动机——1 Trie树(字典树)介绍

    AC自动机——1 Trie树(字典树)介绍 2013年10月15日 23:56:45 阅读数:2375 之前,我们介绍了Kmp算法,其实,他就是一种单模式匹配.当要检查一篇文章中是否有某些敏感词,这其 ...

  3. Trie(字典树)

    没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...

  4. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

  5. Trie树(字典树)的介绍及Java实现

    简介 Trie树,又称为前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也 ...

  6. Trie树 - 字典树

    1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是最大限 ...

  7. cogs 293. [NOI 2000] 单词查找树 Trie树字典树

    293. [NOI 2000] 单词查找树 ★★☆   输入文件:trie.in   输出文件:trie.out   简单对比时间限制:1 s   内存限制:128 MB 在进行文法分析的时候,通常需 ...

  8. [LintCode] Implement Trie 实现字典树

    Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...

  9. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  10. Trie - leetcode [字典树/前缀树]

    208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...

随机推荐

  1. Oracle练习(一)

    Oracle 1.检索部门编号.部门名称.部门所在地及其每个部门的员工总数. select d.deptno,d.dname,d.loc,count(*) from emp e,dept d wher ...

  2. log4j application.properties 配置文件

    log4j.rootLogger = info,stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appende ...

  3. swagger 的使用

    最近在用 .Net Core 做项目 了解到swagger 是一个不错的工具 简单介绍一下 在使用asp.net core 进行api开发完成后,书写api说明文档对于程序员来说想必是件很痛苦的事情吧 ...

  4. java之JVM学习--简单理解编译和运行的过程之概览

    java代码编译流程图: java字节码执行由JVM执行引擎完成 Java代码编译和执行的整个过程包含了以下三个重要的机制: Java源码编译机制 类加载机制 类执行机制 Java源码编译机制 Jav ...

  5. 16.SpringMVC核心技术-文件上传

    上传单个文件 1.定义具有文件上传功能的页面 index.jsp,其表单的设置需要注意,method 属性为 POST, enctype 属性为 multipart/form-data.另外,需要注意 ...

  6. 编译luacheck Linux版

    最近在写Visual Studio Code的Lua插件,需要把luacheck集成进去.但是luacheck默认只提供了win32版本,见https://github.com/mpeterv/lua ...

  7. 【转载】python format遇上花括号{}

    在format string中, 大括号已经被format占用,想要使用大括号本身,该怎么办? 以下转载自这里. ============ 分割线 ============ 使用format时,字符串 ...

  8. Maven 安装依赖包

    Guide to installing 3rd party JARs Although rarely, but sometimes you will have 3rd party JARs that ...

  9. 使用python2与python3创建一个简单的http服务(基于SimpleHTTPServer)

    python2与python3基于SimpleHTTPServer创建一个http服务的方法是不同的: 一.在linux服务器上面检查一下自己的python版本:如: [root@zabbix ~]# ...

  10. (初级篇)docker基础应用--01

    命令 获取镜像: docker pull //: ,如:docker pull nginx 查看镜像列表: docker images 查看镜像信息: docker inspect