来源于英文“retrieval”.   Trie树就是字符树,其核心思想就是空间换时间。

举个简单的例子。
   给你100000个长度不超过10的单词。对于每一个单词,我们要判断他出没出现过,如果出现了,第一次出现第几个位置。
这题当然可以用hash来,但是我要介绍的是trie树。在某些方面它的用途更大。比如说对于某一个单词,我要询问它的前缀是否出现过。这样hash就不好搞了,而用trie还是很简单。

现在回到例子中,如果我们用最傻的方法,对于每一个单词,我们都要去查找它前面的单词中是否有它。那么这个算法的复杂度就是O(n^2)。显然对于100000的范围难以接受。现在我们换个思路想。假设我要查询的单词是abcd,那么在他前面的单词中,以b,c,d,f之类开头的我显然不必考虑。而只要找以a开头的中是否存在abcd就可以了。同样的,在以a开头中的单词中,我们只要考虑以b作为第二个字母的……这样一个树的模型就渐渐清晰了……

假设有b,abc,abd,bcd,abcd,efg,hii这6个单词,我们构建的树就是这样的。

对于每一个节点,从根遍历到他的过程就是一个单词,如果这个节点被标记为红色,就表示这个单词存在,否则不存在。 
   那么,对于一个单词,我只要顺着他从根走到对应的节点,再看这个节点是否被标记为红色就可以知道它是否出现过了。把这个节点标记为红色,就相当于插入了这个单词。

我们可以看到,trie树每一层的节点数是26^i级别的。所以为了节省空间。我们用动态链表,或者用数组来模拟动态。空间的花费,不会超过单词数×单词长度。(转自一大牛)

Trie树的java代码 实现如下:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; /** *//**
* A word trie which can only deal with 26 alphabeta letters.
* @author Leeclipse
* @since 2007-11-21
*/ public class Trie{ private Vertex root;//一个Trie树有一个根节点 //内部类
protected class Vertex{//节点类
protected int words;
protected int prefixes;
protected Vertex[] edges;//每个节点包含26个子节点(类型为自身)
Vertex() {
words = ;
prefixes = ;
edges = new Vertex[];
for (int i = ; i < edges.length; i++) {
edges[i] = null;
}
}
} public Trie () {
root = new Vertex();
} /** *//**
* List all words in the Trie.
*
* @return
*/ public List< String> listAllWords() { List< String> words = new ArrayList< String>();
Vertex[] edges = root.edges; for (int i = ; i < edges.length; i++) {
if (edges[i] != null) {
String word = "" + (char)('a' + i);
depthFirstSearchWords(words, edges[i], word);
}
}
return words;
} /** *//**
* Depth First Search words in the Trie and add them to the List.
*
* @param words
* @param vertex
* @param wordSegment
*/ private void depthFirstSearchWords(List words, Vertex vertex, String wordSegment) {
Vertex[] edges = vertex.edges;
boolean hasChildren = false;
for (int i = ; i < edges.length; i++) {
if (edges[i] != null) {
hasChildren = true;
String newWord = wordSegment + (char)('a' + i);
depthFirstSearchWords(words, edges[i], newWord);
}
}
if (!hasChildren) {
words.add(wordSegment);
}
} public int countPrefixes(String prefix) {
return countPrefixes(root, prefix);
} private int countPrefixes(Vertex vertex, String prefixSegment) {
if (prefixSegment.length() == ) { //reach the last character of the word
return vertex.prefixes;
} char c = prefixSegment.charAt();
int index = c - 'a';
if (vertex.edges[index] == null) { // the word does NOT exist
return ;
} else { return countPrefixes(vertex.edges[index], prefixSegment.substring()); } } public int countWords(String word) {
return countWords(root, word);
} private int countWords(Vertex vertex, String wordSegment) {
if (wordSegment.length() == ) { //reach the last character of the word
return vertex.words;
} char c = wordSegment.charAt();
int index = c - 'a';
if (vertex.edges[index] == null) { // the word does NOT exist
return ;
} else {
return countWords(vertex.edges[index], wordSegment.substring()); } } /** *//**
* Add a word to the Trie.
*
* @param word The word to be added.
*/ public void addWord(String word) {
addWord(root, word);
} /** *//**
* Add the word from the specified vertex.
* @param vertex The specified vertex.
* @param word The word to be added.
*/ private void addWord(Vertex vertex, String word) {
if (word.length() == ) { //if all characters of the word has been added
vertex.words ++;
} else {
vertex.prefixes ++;
char c = word.charAt();
c = Character.toLowerCase(c);
int index = c - 'a';
if (vertex.edges[index] == null) { //if the edge does NOT exist
vertex.edges[index] = new Vertex();
} addWord(vertex.edges[index], word.substring()); //go the the next character
}
} public static void main(String args[]) //Just used for test
{
Trie trie = new Trie();
trie.addWord("China");
trie.addWord("China");
trie.addWord("China"); trie.addWord("crawl");
trie.addWord("crime");
trie.addWord("ban");
trie.addWord("China"); trie.addWord("english");
trie.addWord("establish");
trie.addWord("eat");
System.out.println(trie.root.prefixes);
System.out.println(trie.root.words); List< String> list = trie.listAllWords();
Iterator listiterator = list.listIterator(); while(listiterator.hasNext())
{
String s = (String)listiterator.next();
System.out.println(s);
} int count = trie.countPrefixes("ch");
int count1=trie.countWords("china");
System.out.println("the count of c prefixes:"+count);
System.out.println("the count of china countWords:"+count1); }
}
运行:
C:\test>java Trie ban
china
crawl
crime
eat
english
establish
the count of c prefixes:
the count of china countWords:

Trie 树 及Java实现的更多相关文章

  1. 双数组Trie树(DoubleArrayTrie)Java实现

    http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...

  2. Trie树的java实现

    leetcode 地址: https://leetcode.com/problems/implement-trie-prefix-tree/description/ 难度:中等 描述:略 解题思路: ...

  3. leetcode网站中找到的关于trie树的JAVA版本介绍

    class TrieNode { // R links to node children private TrieNode[] links; private final int R = 26; pri ...

  4. Trie树的应用:查询IP地址的ISP

    1. 问题描述 给定一个IP地址,如何查询其所属的ISP,如:中国移动(ChinaMobile),中国电信(ChinaTelecom),中国铁通(ChinaTietong)?现有ISP的IP地址区段可 ...

  5. 从Trie树到双数组Trie树

    Trie树 原理 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,能在常数时间O(len)内实现插入和查 ...

  6. Trie树(转:http://blog.csdn.net/arhaiyun/article/details/11913501)

    Trie 树, 又称字典树,单词查找树.它来源于retrieval(检索)中取中间四个字符构成(读音同try).用于存储大量的字符串以便支持快速模式匹配.主要应用在信息检索领域. Trie 有三种结构 ...

  7. 字典树(Trie)的java实现

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

  8. java实现的Trie树数据结构

    近期在学习的时候,常常看到使用Trie树数据结构来解决这个问题.比方" 有一个1G大小的一个文件.里面每一行是一个词.词的大小不超过16字节,内存大小限制是1M. 返回频数最高的100个词. ...

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

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

随机推荐

  1. HTML5新增属性

    [sourcecode language="plain"] <!DOCTYPE html> <html manifest="cache.manifest ...

  2. 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)

    文章转载自http://www.csdn.net/article/2014-11-06/2822529/1 当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的 ...

  3. vmware下ubuntu14.04调整分辨率

    很多人在vmware中安装ubuntu时,为了调整屏幕分辨率,都去下载并安装vmware-tools.其实,这是没有必要的.如果你需要vmware和宿主机实现共享,或者为了使文件能拖进拖出,再或者是需 ...

  4. String - 兴趣解读

    个优点: . 以下代码的HashCode是否相同,它们是否是同个对象: . 以下代码的HashCode是否相同,他们是否是同个对象:        . 以下代码的HashCode是否相同,他们是否是同 ...

  5. 应用引擎BAE3.0(转)

    add by zhj: 其实我主要是想看看基于docker的PaaS的特性. 原文:http://developer.baidu.com/wiki/index.php?title=docs/cplat ...

  6. SQL2008 SQL Server 代理服务提供的凭据无效

    工作中遇到的问题记录: 安装到服务器配置时出的错误:为 SQL Server 代理服务提供的凭据无效.若要继续操作,请为 SQL Server 代理服务提供有效的帐户和密码. 解决方法:直接在所有的“ ...

  7. gdb之x命令

    [gdb之x命令] 可以使用examine命令(简写是x)来查看内存地址中的值.x命令的语法如下所示: x/<n/f/u> <addr> n.f.u是可选的参数. n是一个正整 ...

  8. 使用aspose.word两句代码将word转换为pdf

    //Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...

  9. sql with(lock) 与事务

    sql  select查询语句 表后面携带 with(nolock) 会获取到 在事务中已经执行 但还未完成提交的 记录   即使表被锁住也能查询到 当事务最终执行失败时  查询到的记录可能没有啦 不 ...

  10. cf754 A. Lesha and array splitting

    应该是做麻烦了,一开始还没A(幸好上一次比赛水惨了) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL lon ...