在图示中,键标注在节点中,值标注在节点之下。每一个完整的英文单词对应一个特定的整数。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. jvm运行机制与内存管理

    http://blog.csdn.net/lengyuhong/article/details/5953544 http://www.cnblogs.com/nexiyi/p/java_memory_ ...

  2. 【转】Kylin实践之使用Hive视图

    http://blog.csdn.net/yu616568/article/details/50548967 为什么需要使用视图 Kylin在使用的过程中使用hive作为cube的输入,但是有些情况下 ...

  3. 廖雪峰js教程笔记7 基本类型和包装类型

    在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...

  4. NumPy 学习(1): ndarrays

    Numpy 是Numerical Python的简写,用来进行高性能的科学计算以及数据分析的基础包.它是一些高级工具(pandas)的基础.它主要提供以下几个功能: (1). ndarray:计算快, ...

  5. SqlServer事务回滚(2)

    SQL Server 2008中SQL应用系列--目录索引 SQL事务 一.事务概念    事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是 ...

  6. C#资源文件与与资源名称字符串之间的互相转化

    1.使用ResourceManager string st = Properties.Resources.ResourceManager.GetString(tableName);value = Pr ...

  7. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  8. BZOJ2159 : Crash 的文明世界

    $x^k=\sum_{i=1}^k Stirling2(k,i)\times i!\times C(x,i)$ 设$f[i][j]=\sum_{k=1}^n C(dist(i,k),j)$. 则可以利 ...

  9. eclipse 高亮配对的括号

    在编辑代码框右键->preference,直接就可以看到Matching brackets highlights

  10. OpenResty 安装 drizzle-nginx-module

    1.下载drizzle模块 wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz 2.安装drizzle模块 tar zxvf d ...