在图示中,键标注在节点中,值标注在节点之下。每一个完整的英文单词对应一个特定的整数。Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的。键不需要被显式地保存在节点中。图示中标注出完整的单词,只是为了演示 trie 的原理。

trie 中的键通常是字符串,但也可以是其它的结构。trie 的算法可以很容易地修改为处理其它结构的有序序列,比如一串数字或者形状的排列。比如,bitwise trie 中的键是一串位元,可以用于表示整数或者内存地址。

Trie树是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。Trie的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。

它有3个基本性质:

(1) 根节点不包含字符,除根节点外每一个节点都只包含一个字符。

(2) 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。

(3) 每个节点的所有子节点包含的字符都不相同。

基本思想(以字母树为例):
1、插入过程
对于一个单词,从根开始,沿着单词的各个字母所对应的树中的节点分支向下走,直到单词遍历完,将最后的节点标记为红色,表示该单词已插入Trie树。
2、查询过程
同样的,从根开始按照单词的字母顺序向下遍历trie树,一旦发现某个节点标记不存在或者单词遍历完成而最后的节点未标记为红色,则表示该单词不存在,若最后的节点标记为红色,表示该单词存在。

Trie树的实现

字母树的插入(Insert)、删除( Delete)和查找(Find)都非常简单,用一个一重循环即可,即第i 次循环找到前i个字母所对应的子树,然后进行相应的操作。实现这棵字母树,至于Trie树的实现,可以用数组,也可以用指针动态分配,平时为了方便就用数组,静态分配空间。

1.结构定义:

struct Trie
{
Trie *next[26];
bool isWord;
}Root;

2.插入建树方法:

//插入操作(也是构建Trie树)
void insert(char *tar)
{
Trie* head = &Root;
int id;
while(*tar)
{
id = *tar - 'a';
if(head->next[id] == NULL)
head->next[id] = new Trie();
head = head->next[id];
tar++;
}
head->isWord = true;
}

3.搜索查询方法:

//查找
bool search(char *tar)
{
Trie* head = &Root;
int id;
while(*tar)
{
id = *tar - 'a';
if(head->next[id] == NULL)
return false;
head = head->next[id];
tar++;
}
if(head->isWord)
return true;
else
return false;
}

  

至于结点对儿子的指向,一般有三种方法:

1、对每个结点开一个字母集大小的数组,对应的下标是儿子所表示的字母,内容则是这个儿子对应在大数组上的位置,即标号;

2、对每个结点挂一个链表,按一定顺序记录每个儿子是谁;

3、使用左儿子右兄弟表示法记录这棵树。

三种方法,各有特点。第一种易实现,但实际的空间要求较大;第二种,较易实现,空间要求相对较小,但比较费时;第三种,空间要求最小,但相对费时且不易写。

字典树TrieTree的java实现:

package mine.trietree;

import java.util.ArrayList;
import java.util.List; public class TrieTree {
private int SIZE = 26;
private TreeNode root;
TrieTree(){
root = new TreeNode();
}
class TreeNode{
private TreeNode[] son;
private char data;
private boolean isword;
TreeNode(){
son = new TreeNode[SIZE];
data = ' ';
isword = false;
}
}
protected void InsertTreeNode(String str){
TreeNode node = root;
char[] strdata = str.toCharArray();
for(int i = 0;i < str.length(); i++){
int id = strdata[i]-'a';
if(node.son[id]==null){
node.son[id] = new TreeNode();
node.son[id].data = strdata[i];
}
node = node.son[id];
}
node.isword = true;
}
protected boolean IsInDictionary(String str){
TreeNode node = root;
char[] strdata = str.toCharArray();
for(int i = 0;i < str.length(); i++){
int id = strdata[i]-'a';
if(node.son[id]==null)
return false;
node = node.son[id];
}
if(node.isword==false)
return false;
else
return true;
}
public static void main(String[] args) {
TrieTree root = new TrieTree();
root.InsertTreeNode("hello");
root.InsertTreeNode("world");
root.InsertTreeNode("lixiaokun");
root.InsertTreeNode("haoxue");
if(root.IsInDictionary("lixiaokun"))
System.out.println("Yes");
else
System.out.println("No");
if(root.IsInDictionary("haoxue"))
System.out.println("Yes");
else
System.out.println("No");
} }

java完整版:

package mine.trietree;

import java.util.ArrayList;
import java.util.List; public class TrieTree {
private int SIZE = 26;
private TreeNode root;
TrieTree(){
root = new TreeNode();
}
class TreeNode{
private TreeNode[] son;
private char data;
private boolean isword;
private int num;
TreeNode(){
son = new TreeNode[SIZE];
data = ' ';
isword = false;
int num = 0;
}
}
protected void InsertTreeNode(String str){
TreeNode node = root;
char[] strdata = str.toCharArray();
for(int i = 0;i < str.length(); i++){
int id = strdata[i]-'a';
if(node.son[id]==null){
node.son[id] = new TreeNode();
node.son[id].data = strdata[i];
}else
node.son[id].num++;
node = node.son[id];
}
node.isword = true;
}
protected int CountTime(String str){
if(str.length()==0||str==null)
return -1;
char[] strdata = str.toCharArray();
TreeNode node = root;
for(int i = 0;i < str.length(); i++){
int id = strdata[i]-'a';
if(node.son[id]==null)
return 0;
else
node = node.son[id];
}
return node.num;
}
protected boolean IsInDictionary(String str){
TreeNode node = root;
char[] strdata = str.toCharArray();
for(int i = 0;i < str.length(); i++){
int id = strdata[i]-'a';
if(node.son[id]==null)
return false;
node = node.son[id];
}
if(node.isword==false)
return false;
else
return true;
}
protected TreeNode GetRoot(){
return this.root;
}
protected void PreVisit(TreeNode node){
if(node!=null){
System.out.print(node.data+" ");
for(TreeNode temp:node.son)
PreVisit(temp);
}
}
public static void main(String[] args) {
TrieTree root = new TrieTree();
root.InsertTreeNode("hello");
root.InsertTreeNode("world");
root.InsertTreeNode("lixiaokun");
root.InsertTreeNode("haoxue");
root.InsertTreeNode("helloworld");
root.InsertTreeNode("comeon");
root.InsertTreeNode("hehehe");
if(root.IsInDictionary("lixiaokun"))
System.out.println("Yes");
else
System.out.println("No");
if(root.IsInDictionary("haoxue"))
System.out.println("Yes");
else
System.out.println("No");
if(root.IsInDictionary("hehe"))
System.out.println("Yes");
else
System.out.println("No");
root.PreVisit(root.GetRoot());
System.out.println();
System.out.println(root.CountTime("he"));
} }

  

文字引用自:http://www.cnblogs.com/archimedes/p/trie-tree.html

字典树(Trie Tree)的更多相关文章

  1. 字典树Trie Tree

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀 ...

  2. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  3. 字典树trie学习

    字典树trie的思想就是利用节点来记录单词,这样重复的单词可以很快速统计,单词也可以快速的索引.缺点是内存消耗大 http://blog.csdn.net/chenleixing/article/de ...

  4. 『字典树 trie』

    字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...

  5. 字典树(Trie)详解

    详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...

  6. 字典树(Trie树)实现与应用

    一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...

  7. [转载]字典树(trie树)、后缀树

    (1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...

  8. Codevs 4189 字典(字典树Trie)

    4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...

  9. 字典树trie

    字典树经常用于单词搜索,现在网络引擎中也应用了trie树: public class Trie{ private int SIZE = 26; private TrieNode root; Trie( ...

随机推荐

  1. thinkphp测试方法

    1.如果是单个函数可以使用命令行的模式调试. 2.如果是公用函数可以新增一个控制器函数来测试: 如测试这条公共函数

  2. Eclipse中Ctrl+方法名发现无法进入到该方法中……

    我现在的情况是,按住Ctrl点击该方法后,发现进入不到这个方法的定义. 后来我发现,是因为这个项目是在某个项目文件夹中,如下: 这时直接找到server这个项目就没有问题了,如图:

  3. 【转】【技术博客】Spark性能优化指南——高级篇

    http://mp.weixin.qq.com/s?__biz=MjM5NjQ5MTI5OA==&mid=2651745207&idx=1&sn=3d70d59cede236e ...

  4. LR通过snmp监控linux下的mysql

    LR通过snmp监控linux下的mysql 在linux底下安装配置snmp: 1.使用系统盘安装rpm包(这种方式最好) 2.在www.net-snmp.org处下载net-snmp安装(安装后有 ...

  5. 第二个div+css前端项目

    先展示效果图: 为了看全景,截图有点挫.实际效果比这个好一点. 通过 text-overflow可以隐藏多出的文字,而不会吧把div撑开或者溢出. html代码: <!DOCTYPE html& ...

  6. Liferay 6.2 改造系列之十一:默认关闭CDN动态资源

    在行业客户中,一般无法提供CDN服务,因此默认关闭CDN动态资源功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # ...

  7. C# 插件式程序开发

    在网上找了下插件式编程的资料,这里自己先借鉴下别人的,同时发现有自己的看法,不过由于本人水平有限,不一定有参考价值,写出来一方面是为了总结自己,以求提高,另一方面也希望各为朋友看到我的不足,给我提出宝 ...

  8. JavaScript中设置元素class的三种方法小结

    第一.element.setAttribute('class','abc');  第二.element.setAttribute('className', 'abc') : 第三.element.cl ...

  9. 网站迁移时候,发现<head>内容都到body里了

    遇到的问题截图如下: 这个是编码问题,需要把所有涉及的文件保存成UTF-8 without BOM,手动的话可以用notepad++ 如果网站支持php,这边提供了一个php的脚本(clearBom. ...

  10. 关于在C#中构造函数中调用虚函数的问题

    在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数.这是由于C#的运行机制造成的,原因如下: 新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类.构造 ...