在图示中,键标注在节点中,值标注在节点之下。每一个完整的英文单词对应一个特定的整数。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. Javaweb三大组件之过滤器filter

    Filter的三个方法 void init(FilterConfig):在Tomcat启动时被调用: void destroy():在Tomcat关闭时被调用: void doFilter(Servl ...

  2. 廖雪峰js教程笔记 1

    遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...

  3. (转)qsort和sort

    1.qsort函数: 原 型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)) ...

  4. 安装PyMysql的基本步骤

    X:\Users\**>c: c:\>cd python c:\Python>python ez_setup.py Downloading https://pypi.io/packa ...

  5. 对于for的一些认识

    /*▲            ▲▲            ▲▲▲            ▲▲▲▲            ▲▲▲▲▲            ▲▲▲▲▲▲*/例:如图用for嵌套打印一个三 ...

  6. jQuery-事件以及动画

    事件: 1.//方法1 $(window).load(function(){ }) window.onload=function(){ } //方法2 function one(){ alert(&q ...

  7. C#,往线程里传参数的方法总结

    Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始化 ...

  8. wpf Popup Win8.0 bug HorizontalOffset 弹出位置偏移

    问题描述参考 wpf 客户端[JDAgent桌面助手]开发详解(四) popup控件的win8.0的bug 当开发完程序后,我们在多操作系统测试时候发现:win8.0  系统中 popup 弹出的位置 ...

  9. bash脚本中的普通数组和关联数组

    1. 普通数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0. ...

  10. Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移

    本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0  1.对信息的传输/ ...