Java基础 - 字典树(Trie树)

来源:互联网 发布:网络派出所电话 编辑:程序博客网 时间:2024/05/19 19:31

本文转载由http://blog.csdn.net/jiutianhe/article/details/8076835提供


1. Trie树

Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。

Trie的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。

它有3个基本性质:

  1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
  2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
  3. 每个节点的所有子节点包含的字符都不相同。

2. 字典树的构建

       题目:给你100000个长度不超过10的单词。对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位置。
分析:这题当然可以用hash来解决,但是本文重点介绍的是trie树,因为在某些方面它的用途更大。比如说对于某一个单词,我们要询问它的前缀是否出现过。这样hash就不好搞了,而用trie还是很简单。
假设我要查询的单词是abcd,那么在他前面的单词中,以b,c,d,f之类开头的我显然不必考虑。而只要找以a开头的中是否存在abcd就可以了。同样的,在以a开头中的单词中,我们只要考虑以b作为第二个字母的,一次次缩小范围和提高针对性,这样一个树的模型就渐渐清晰了。
好比假设有b,abc,abd,bcd,abcd,efg,hii 这6个单词,我们构建的树就是如下图这样的:
ok,如上图所示,对于每一个节点,从根遍历到他的过程就是一个单词,如果这个节点被标记为红色,就表示这个单词存在,否则不存在。
那么,对于一个单词,我只要顺着他从根走到对应的节点,再看这个节点是否被标记为红色就可以知道它是否出现过了。把这个节点标记为红色,就相当于插入了这个单词。
       这样一来我们查询和插入可以一起完成(重点体会这个查询和插入是如何一起完成的,稍后,下文具体解释),所用时间仅仅为单词长度,在这一个样例,便是10。
我们可以看到,trie树每一层的节点数是26^i级别的。所以为了节省空间。我们用动态链表,或者用数组来模拟动态。空间的花费,不会超过单词数×单词长度。
      已知n个由小写字母构成的平均长度为10的单词,判断其中是否存在某个串为另一个串的前缀子串。下面对比3种方法:
  1. 最容易想到的:即从字符串集中从头往后搜,看每个字符串是否为字符串集中某个字符串的前缀,复杂度为O(n^2)。
  2. 使用hash:我们用hash存下所有字符串的所有的前缀子串,建立存有子串hash的复杂度为O(n*len),而查询的复杂度为O(n)* O(1)= O(n)。
  3. 使用trie:因为当查询如字符串abc是否为某个字符串的前缀时,显然以b,c,d....等不是以a开头的字符串就不用查找了。所以建立trie的复杂度为O(n*len),而建立+查询在trie中是可以同时执行的,建立的过程也就可以成为查询的过程,hash就不能实现这个功能。所以总的复杂度为O(n*len),实际查询的复杂度也只是O(len)。(说白了,就是Trie树的平均高度h为len,所以Trie树的查询复杂度为O(h)=O(len)。好比一棵二叉平衡树的高度为logN,则其查询,插入的平均时间复杂度亦为O(logN))。

3. 查询

Trie树是简单但实用的数据结构,通常用于实现字典查询。我们做即时响应用户输入的AJAX搜索框时,就是Trie开始。本质上,Trie是一颗存储多个字符串的树。相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串。和普通树不同的地方是,相同的字符串前缀共享同一条分支。下面,再举一个例子。给出一组单词,inn, int, at, age, adv, ant, 我们可以得到下面的Trie:
可以看出:
  • 每条边对应一个字母。
  • 每个节点对应一项前缀。叶节点对应最长前缀,即单词本身。
  • 单词inn与单词int有共同的前缀“in”, 因此他们共享左边的一条分支,root->i->in。同理,ate, age, adv, 和ant共享前缀"a",所以他们共享从根节点到节点"a"的边。
查询操纵非常简单。比如要查找int,顺着路径i -> in -> int就找到了。

搭建Trie的基本算法也很简单,无非是逐一把每则单词的每个字母插入Trie。插入前先看前缀是否存在。如果存在,就共享,否则创建对应的节点和边。比如要插入单词add,就有下面几步:
  1. 考察前缀"a",发现边a已经存在。于是顺着边a走到节点a。
  2. 考察剩下的字符串"dd"的前缀"d",发现从节点a出发,已经有边d存在。于是顺着边d走到节点ad
  3. 考察最后一个字符"d",这下从节点ad出发没有边d了,于是创建节点ad的子节点add,并把边ad->add标记为d。

4. 应用

  • 1、有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M。返回频数最高的100个词。
  • 2、1000万字符串,其中有些是重复的,需要把重复的全部去掉,保留没有重复的字符串。请怎么设计和实现?
  • 3、 一个文本文件,大约有一万行,每行一个词,要求统计出其中最频繁出现的前10个词,请给出思想,给出时间复杂度分析。
  • 4、寻找热门查询:搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节。假设目前有一千万个记录,这些查询串的重复读比较高,虽然总数是1千万,但是如果去除重复和,不超过3百万个。一个查询串的重复度越高,说明查询它的用户越多,也就越热门。请你统计最热门的10个查询串,要求使用的内存不能超过1G。
    (1) 请描述你解决这个问题的思路;
    (2) 请给出主要的处理流程,算法,以及算法的复杂度。

5. 代码实现(Java)

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package binarytree;  
  2.   
  3. /** 
  4.  * 字典树的Java实现。实现了插入、查询以及深度优先遍历. 
  5.  * Trie tree's java implementation.(Insert,Search,DFS) 
  6.  * @author jiutianhe 
  7.  * @time 2012.10.16 
  8.  */  
  9. public class TrieTree {  
  10.     final int MAX_SIZE=26;  
  11.     public class TrieTreeNode {       
  12.         int nCount;//记录该字符出现次数  
  13.         char ch; //记录该字符  
  14.         TrieTreeNode[] child;  
  15.           
  16.         public TrieTreeNode() {  
  17.             nCount=1;  
  18.             child=new TrieTreeNode[MAX_SIZE];  
  19.         }  
  20.           
  21.           
  22.     }  
  23.     //字典树的插入和构建  
  24.     public void createTrie(TrieTreeNode node,String str){  
  25.         if (str==null||str.length()==0) {  
  26.             return;  
  27.         }  
  28.         char[] letters=str.toCharArray();  
  29.         for (int i = 0; i < letters.length; i++) {  
  30.             int pos = letters[i] - 'a';    
  31.             if (node.child[pos] == null) {    
  32.                 node.child[pos] = new TrieTreeNode();     
  33.             }else {  
  34.                 node.child[pos].nCount++;  
  35.             }  
  36.             node.ch=letters[i];              
  37.             node = node.child[pos];              
  38.         }  
  39.     }  
  40.     //字典树的查找  
  41.     public int findCount(TrieTreeNode node,String str){  
  42.         if (str==null||str.length()==0) {  
  43.             return -1;  
  44.         }  
  45.         char[] letters=str.toCharArray();  
  46.         for (int i = 0; i < letters.length; i++) {  
  47.             int pos = letters[i] - 'a';    
  48.             if (node.child[pos] == null) {    
  49.                 return 0;     
  50.             }else {  
  51.                 node=node.child[pos];  
  52.             }             
  53.         }  
  54.         return node.nCount;  
  55.     }  
  56.           
  57. }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2.     public void trieTreeTest2(){  
  3.     /** 
  4.      * Problem Description   
  5.      * 老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计 
  6.      * 出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).   
  7.      */  
  8.         String[] strs={     
  9.                 "banana",     
  10.                 "band",     
  11.                 "bee",     
  12.                 "absolute",     
  13.                 "acm",     
  14.         };     
  15.         String[] prefix={     
  16.             "ba",     
  17.             "b",     
  18.             "band",     
  19.             "abc",     
  20.         };    
  21.         TrieTree tree = new TrieTree();  
  22.         TrieTreeNode root=tree.new TrieTreeNode();  
  23.           
  24.         for (String s : strs) {    
  25.             tree.createTrie(root, s);    
  26.         }     
  27. //        tree.printAllWords();     
  28.         for(String pre:prefix){     
  29.             int num=tree.findCount(root,pre);     
  30.             System.out.println(pre+" "+num);     
  31.         }  
  32.            
  33.     }  

代码2:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.algorithm;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. /** 
  9.  *  
  10.  * <p> 
  11.  * Title: 
  12.  * </p> 
  13.  * <p> 
  14.  * Description: 单词Trie树 
  15.  * </p> 
  16.  *  
  17.  * @createDate:2013-10-17 
  18.  * @author 
  19.  * @version 1.0 
  20.  */  
  21. public class WordTrie {  
  22.     class TrieNode {  
  23.         /** 
  24.          * trie tree word count 
  25.          */  
  26.         int count = 0;  
  27.   
  28.         /** 
  29.          * trie tree prefix count 
  30.          */  
  31.         int prefixCount = 0;  
  32.   
  33.         /** 
  34.          * 指向各个子树的指针,存储26个字母[a-z] 
  35.          */  
  36.         TrieNode[] next = new TrieNode[26];  
  37.   
  38.         /** 
  39.          * 当前TrieNode状态 ,默认 0 , 1表示从根节点到当前节点的路径表示一个词,即叶子节点 
  40.          */  
  41.         int nodeState = 0;  
  42.   
  43.         TrieNode() {  
  44.             count = 0;  
  45.             prefixCount = 0;  
  46.             next = new TrieNode[26];  
  47.             nodeState = 0;  
  48.         }  
  49.     }  
  50.   
  51.     /** trie树根 */  
  52.     private TrieNode root = new TrieNode();  
  53.   
  54.     /** 英文字符串正则匹配 */  
  55.     static String englishPattern = "^[A-Za-z]+$";  
  56.     /** 中文正则匹配 */  
  57.     static String chinesePattern = "[\u4e00-\u9fa5]";  
  58.   
  59.     static int ARRAY_LENGTH = 26;  
  60.   
  61.     static String zeroString = "";  
  62.   
  63.     /** 
  64.      * 插入字串,用循环代替迭代实现 
  65.      *  
  66.      * @param words 
  67.      */  
  68.     public void insert(String word) {  
  69.         insert(this.root, word);  
  70.     }  
  71.   
  72.     /** 
  73.      * 插入字串,用循环代替迭代实现  
  74.      *  
  75.      * @param root 
  76.      * @param words 
  77.      */  
  78.     private void insert(TrieNode root, String word) {  
  79.         word = word.toLowerCase();// //转化为小写  
  80.         char[] chrs = word.toCharArray();  
  81.   
  82.         for (int i = 0, length = chrs.length; i < length; i++) {  
  83.             // /用相对于a字母的值作为下标索引,也隐式地记录了该字母的值  
  84.             int index = chrs[i] - 'a';  
  85.             if (index >= 0 && index < ARRAY_LENGTH) {//过滤特殊字符,例如`等  
  86.                 if (root.next[index] != null) {  
  87.                     // //已经存在了,该子节点prefixCount++  
  88.                     root.next[index].prefixCount++;  
  89.                 } else {  
  90.                     // /如果不存在  
  91.                     root.next[index] = new TrieNode();  
  92.                     root.next[index].prefixCount++;  
  93.                 }  
  94.   
  95.                 // /如果到了字串结尾,则做标记  
  96.                 if (i == length - 1) {  
  97.                     root.next[index].nodeState = 1;  
  98.                     root.next[index].count++;  
  99.                 }  
  100.                 // /root指向子节点,继续处理  
  101.                 root = root.next[index];  
  102.             }  
  103.         }  
  104.   
  105.     }  
  106.   
  107.     /** 
  108.      *  
  109.      * @Title: addWord 
  110.      * @Description: add word 
  111.      * @param @param word 
  112.      * @return void 
  113.      * @throws 
  114.      */  
  115.     public void addWord(String word) {  
  116.         if (word == null || "".equals(word.trim())) {  
  117.             throw new IllegalArgumentException("word can not be null!");  
  118.         }  
  119.         // if(!word.matches(englishPattern)){  
  120.         // System.out.println(word);  
  121.         // throw new IllegalArgumentException("word must be english!");  
  122.         // }  
  123.         addWord(root, word);  
  124.     }  
  125.   
  126.     /** 
  127.      *  
  128.      * @Title: addWord 
  129.      * @Description:add word to node 
  130.      * @param @param node 
  131.      * @param @param word 
  132.      * @return void 
  133.      * @throws 
  134.      */  
  135.     private void addWord(TrieNode node, String word) {  
  136.         if (word.length() == 0) { // if all characters of the word has been  
  137.             // added  
  138.             node.count++;  
  139.             node.nodeState = 1;  
  140.         } else {  
  141.             node.prefixCount++;  
  142.             char c = word.charAt(0);  
  143.             c = Character.toLowerCase(c);  
  144.             int index = c - 'a';  
  145.             if (index >= 0 && index < ARRAY_LENGTH) {  
  146.                 if (node.next[index] == null) {  
  147.                     node.next[index] = new TrieNode();  
  148.                 }  
  149.                 // go the the next character  
  150.                 addWord(node.next[index], word.substring(1));  
  151.             }  
  152.   
  153.         }  
  154.     }  
  155.   
  156.     /** 
  157.      *  
  158.      * @Title: prefixSearchWord 
  159.      * @Description: 前缀搜索 
  160.      * @param @param word 
  161.      * @param @return 
  162.      * @return List<String> 
  163.      * @throws 
  164.      */  
  165.     public List<String> prefixSearchWord(String word) {  
  166.         if (word == null || "".equals(word.trim())) {  
  167.             return new ArrayList<String>();  
  168.         }  
  169.         if (!word.matches(englishPattern)) {  
  170.             return new ArrayList<String>();  
  171.         }  
  172.         char c = word.charAt(0);  
  173.         c = Character.toLowerCase(c);  
  174.         int index = c - 'a';  
  175.         if (root.next != null && root.next[index] != null) {  
  176.             return depthSearch(root.next[index], new ArrayList<String>(),  
  177.                     word.substring(1), "" + c, word);  
  178.         } else {  
  179.             return new ArrayList<String>();  
  180.         }  
  181.     }  
  182.   
  183.     /** 
  184.      *  
  185.      * @Title: searchWord 
  186.      * @Description: 搜索单词,以a-z为根,分别向下递归搜索 
  187.      * @param @param word 
  188.      * @param @return 
  189.      * @return List<String> 
  190.      * @throws 
  191.      */  
  192.     public List<String> searchWord(String word) {  
  193.         if (word == null || "".equals(word.trim())) {  
  194.             return new ArrayList<String>();  
  195.         }  
  196.         if (!word.matches(englishPattern)) {  
  197.             return new ArrayList<String>();  
  198.         }  
  199.         char c = word.charAt(0);  
  200.         c = Character.toLowerCase(c);  
  201.         int index = c - 'a';  
  202.         List<String> list = new ArrayList<String>();  
  203.         if (root.next == null) {  
  204.             return list;  
  205.         }  
  206.         for (int i = 0; i < ARRAY_LENGTH; i++) {  
  207.             int j = 'a' + i;  
  208.             char temp = (char) j;  
  209.             if (root.next[i] != null) {  
  210.                 if (index == i) {  
  211.                     fullSearch(root.next[i], list, word.substring(1),  
  212.                             "" + temp, word);  
  213.                 } else {  
  214.                     fullSearch(root.next[i], list, word, "" + temp, word);  
  215.                 }  
  216.             }  
  217.         }  
  218.         return list;  
  219.     }  
  220.   
  221.     /** 
  222.      *  
  223.      * @Title: fullSearch 
  224.      * @Description: 匹配到对应的字母,则以该字母为字根,继续匹配完所有的单词。 
  225.      * @param @param node 
  226.      * @param @param list 保存搜索到的字符串 
  227.      * @param @param word 搜索的单词.匹配到第一个则减去一个第一个,连续匹配,直到word为空串.若没有连续匹配,则恢复到原串。 
  228.      * @param @param matchedWord 匹配到的单词 
  229.      * @param @return 
  230.      * @return List<String> 
  231.      * @throws 
  232.      */  
  233.     private List<String> fullSearch(TrieNode node, List<String> list,  
  234.             String word, String matchedWord, String inputWord) {  
  235.         if (node.nodeState == 1 && word.length() == 0) {  
  236.             list.add(matchedWord);  
  237.         }  
  238.         if (word.length() != 0) {  
  239.             char c = word.charAt(0);  
  240.             c = Character.toLowerCase(c);  
  241.             int index = c - 'a';  
  242.             for (int i = 0; i < ARRAY_LENGTH; i++) {  
  243.                 if (node.next[i] != null) {  
  244.                     int j = 'a' + i;  
  245.                     char temp = (char) j;  
  246.                     if (index == i) {  
  247.                         // 连续匹配  
  248.                         fullSearch(node.next[i], list, word.substring(1),  
  249.                                 matchedWord + temp, inputWord);  
  250.                     } else {  
  251.                         // 未连续匹配,则重新匹配  
  252.                         fullSearch(node.next[i], list, inputWord, matchedWord  
  253.                                 + temp, inputWord);  
  254.                     }  
  255.                 }  
  256.             }  
  257.         } else {  
  258.             if (node.prefixCount > 0) {  
  259.                 for (int i = 0; i < ARRAY_LENGTH; i++) {  
  260.                     if (node.next[i] != null) {  
  261.                         int j = 'a' + i;  
  262.                         char temp = (char) j;  
  263.                         fullSearch(node.next[i], list, zeroString, matchedWord  
  264.                                 + temp, inputWord);  
  265.                     }  
  266.                 }  
  267.             }  
  268.         }  
  269.         return list;  
  270.     }  
  271.   
  272.     /** 
  273.      *  
  274.      * @Title: depthSearch 
  275.      * @Description: 深度遍历子树 
  276.      * @param @param node 
  277.      * @param @param list 保存搜索到的字符串 
  278.      * @param @param word 搜索的单词.匹配到第一个则减去一个第一个,连续匹配,直到word为空串.若没有连续匹配,则恢复到原串。 
  279.      * @param @param matchedWord 匹配到的单词 
  280.      * @param @return 
  281.      * @return List<String> 
  282.      * @throws 
  283.      */  
  284.     private List<String> depthSearch(TrieNode node, List<String> list,  
  285.             String word, String matchedWord, String inputWord) {  
  286.         if (node.nodeState == 1 && word.length() == 0) {  
  287.             list.add(matchedWord);  
  288.         }  
  289.         if (word.length() != 0) {  
  290.             char c = word.charAt(0);  
  291.             c = Character.toLowerCase(c);  
  292.             int index = c - 'a';  
  293.             // 继续完全匹配,直到word为空串,否则未找到  
  294.             if (node.next[index] != null) {  
  295.                 depthSearch(node.next[index], list, word.substring(1),  
  296.                         matchedWord + c, inputWord);  
  297.             }  
  298.         } else {  
  299.             if (node.prefixCount > 0) {// 若匹配单词结束,但是trie中的单词并没有完全找到,需继续找到trie中的单词结束.  
  300.                 // node.prefixCount>0表示trie中的单词还未结束  
  301.                 for (int i = 0; i < ARRAY_LENGTH; i++) {  
  302.                     if (node.next[i] != null) {  
  303.                         int j = 'a' + i;  
  304.                         char temp = (char) j;  
  305.                         depthSearch(node.next[i], list, zeroString, matchedWord  
  306.                                 + temp, inputWord);  
  307.                     }  
  308.                 }  
  309.             }  
  310.         }  
  311.         return list;  
  312.     }  
  313.   
  314.     /** 
  315.      * 遍历Trie树,查找所有的words以及出现次数 
  316.      *  
  317.      * @return HashMap<String, Integer> map 
  318.      */  
  319.     public Map<String, Integer> getAllWords() {  
  320.         return preTraversal(this.root, "");  
  321.     }  
  322.   
  323.     /** 
  324.      * 前序遍历。。。 
  325.      *  
  326.      * @param root 
  327.      *            子树根节点 
  328.      * @param prefixs 
  329.      *            查询到该节点前所遍历过的前缀 
  330.      * @return 
  331.      */  
  332.     private Map<String, Integer> preTraversal(TrieNode root, String prefixs) {  
  333.         Map<String, Integer> map = new HashMap<String, Integer>();  
  334.   
  335.         if (root != null) {  
  336.   
  337.             if (root.nodeState == 1) {  
  338.                 // //当前即为一个单词  
  339.                 map.put(prefixs, root.count);  
  340.             }  
  341.   
  342.             for (int i = 0, length = root.next.length; i < length; i++) {  
  343.                 if (root.next[i] != null) {  
  344.                     char ch = (char) (i + 'a');  
  345.                     // //递归调用前序遍历  
  346.                     String tempStr = prefixs + ch;  
  347.                     map.putAll(preTraversal(root.next[i], tempStr));  
  348.                 }  
  349.             }  
  350.         }  
  351.   
  352.         return map;  
  353.     }  
  354.   
  355.     /** 
  356.      * 判断某字串是否在字典树中 
  357.      *  
  358.      * @param word 
  359.      * @return true if exists ,otherwise false 
  360.      */  
  361.     public boolean isExist(String word) {  
  362.         return search(this.root, word);  
  363.     }  
  364.   
  365.     /** 
  366.      * 查询某字串是否在字典树中 
  367.      *  
  368.      * @param word 
  369.      * @return true if exists ,otherwise false 
  370.      */  
  371.     private boolean search(TrieNode root, String word) {  
  372.         char[] chs = word.toLowerCase().toCharArray();  
  373.         for (int i = 0, length = chs.length; i < length; i++) {  
  374.             int index = chs[i] - 'a';  
  375.             if (root.next[index] == null) {  
  376.                 // /如果不存在,则查找失败  
  377.                 return false;  
  378.             }  
  379.             root = root.next[index];  
  380.         }  
  381.   
  382.         return true;  
  383.     }  
  384. }  

测试和计算词频中最大n个

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.algorithm;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.FileReader;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10. import java.util.Comparator;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13. import java.util.Map.Entry;  
  14. import java.util.PriorityQueue;  
  15.   
  16. public class WordTrieMain {  
  17.   
  18.     public static void main(String[] args){  
  19.         wordMaxNFrequency(10);  
  20.     }  
  21.       
  22.     public static void test1(){  
  23.         WordTrie trie=new WordTrie();  
  24.           
  25.         trie.addWord("ibiyzbi");  
  26.   
  27.         System.out.println("----------------------------------------");  
  28.         List<String> words=trie.searchWord("bi");  
  29.         for(String s: words){  
  30.             System.out.println(s);  
  31.         }  
  32.     }  
  33.       
  34.     public static void test(){  
  35.         WordTrie trie=new WordTrie();  
  36.         trie.addWord("abi");  
  37.         trie.addWord("ai");  
  38.         trie.addWord("aqi");  
  39.         trie.addWord("biiiyou");  
  40.         trie.addWord("dqdi");  
  41.         trie.addWord("ji");  
  42.         trie.addWord("li");  
  43.         trie.addWord("li");  
  44.         trie.addWord("li");  
  45.         trie.addWord("lipi");  
  46.         trie.addWord("qi");  
  47.         trie.addWord("qibi");  
  48.         trie.addWord("i");  
  49.         trie.addWord("ibiyzbi");  
  50.         List<String> list=trie.prefixSearchWord("li");  
  51.         for(String s: list){  
  52.             System.out.println(s);  
  53.         }  
  54.         System.out.println("----------------------------------------");  
  55.         System.out.println(trie.getAllWords());  
  56.         System.out.println("----------------------------------------");  
  57.         List<String> li=trie.searchWord("i");  
  58.         for(String s: li){  
  59.             System.out.println(s);  
  60.         }  
  61.         System.out.println("----------------------------------------");  
  62.         List<String> words=trie.searchWord("bi");  
  63.         for(String s: words){  
  64.             System.out.println(s);  
  65.         }  
  66.           
  67.         System.out.println("----------------------------------------");  
  68.         List<String> lst=trie.searchWord("q");  
  69.         for(String s: lst){  
  70.             System.out.println(s);  
  71.         }  
  72.     }  
  73.       
  74.     /** 
  75.      * @Title: wordMaxNFrequency  
  76.      * @Description: 计算文章词频中最大的前N个 
  77.      * @param     设定文件  
  78.      * @return void    返回类型  
  79.      * @throws 
  80.      */  
  81.     public static void wordMaxNFrequency(int n){  
  82. //      InputStream is = new WordTrieMain().getClass().getClassLoader().getResourceAsStream("words.txt");  
  83.         BufferedReader br = null;  
  84.         try {  
  85.             File file= new File("src/com/algorithm/words.txt");  
  86.             //读取语料库words.txt  
  87.             br = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsolutePath()),"GBK"));  
  88.             String word="";  
  89.             WordTrie trie=new WordTrie();  
  90.             while ((word = br.readLine()) != null) {  
  91.                 trie.insert(word);;  
  92.             }  
  93.             Map<String,Integer> map = trie.getAllWords();  
  94.             System.out.println(map.get("the"));  
  95.             PriorityQueue<Map.Entry<String, Integer>> pq=new PriorityQueue<Map.Entry<String, Integer>>(10,new Comparator<Map.Entry<String, Integer>>() {  
  96.                 @Override  
  97.                 public int compare(Map.Entry<String, Integer> o1,  
  98.                         Map.Entry<String, Integer> o2) {  
  99.                     return o1.getValue().compareTo(o2.getValue());  
  100.                 }  
  101.             });  
  102.             int i =0;  
  103.             for(Entry<String,Integer> entry : map.entrySet()){              
  104.                 if(i<n){  
  105.                     pq.offer(entry);                      
  106.                 }else{  
  107.                     Entry<String,Integer> entryTemp =  (Entry<String, Integer>) pq.peek();  
  108.                     if(entryTemp.getValue().compareTo(entry.getValue())<0){  
  109.                         pq.poll();  
  110.                         pq.offer(entry);  
  111.                           
  112.                     }  
  113.                 }  
  114.                 i++;  
  115.             }  
  116.             System.out.println(pq.toString());  
  117.         }catch (FileNotFoundException e) {  
  118.             e.printStackTrace();  
  119.         }catch(IOException e){  
  120.             e.printStackTrace();  
  121.         }  
  122.         finally{  
  123.             try{  
  124.                 br.close();  
  125.             }catch(Exception e){  
  126.                 e.printStackTrace();  
  127.             }  
  128.         }  
  129.     }  
  130. }  

Trie树占用内存较大,例如:处理最大长度为20、全部为小写字母的一组字符串,则可能需要 2620 个节点来保存数据。而这样的树实际上稀疏的十分厉害,可以采用左儿子右兄弟的方式来改善,也可以采用需要多少子节点则添加多少子节点来解决(不要类似网上的示例,每个节点初始化时就申请一个长度为26的数组)。

Wiki上提到了采用三数组Trie(Tripple-Array Trie)和二数组Trie(Double-Array Trie)来解决该问题,此外还有压缩等方式来缓解该问题。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.recommend.base.algorithm;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collection;  
  5. import java.util.Map;  
  6. import java.util.TreeMap;  
  7.   
  8. import org.apache.commons.lang3.StringUtils;  
  9.   
  10. public class Trie {  
  11.    
  12.     private TrieNode root; //根节点  
  13.    
  14.     public Trie() {  
  15.         this.root = new TrieNode();  
  16.     }  
  17.    
  18.     private class TrieNode { //节点类  
  19.         private int num; //通过的字符串数(包含在此结束的字符串)  
  20.         private int count; //刚好在这里结束的单词数  
  21.         private Map<Character,TrieNode> son; //记录子节点  
  22.    
  23.         TrieNode() {  
  24.             num = 1;  
  25.             count = 0;  
  26.             son = new TreeMap<>(); //TreeMap用于排序  
  27.         }  
  28.     }  
  29.    
  30.     public void add(String word) {  //在字典树中插入一个字符串  
  31.         if(StringUtils.isBlank(word)) {  
  32.             return;  
  33.         }  
  34.         TrieNode node = root;  
  35.         char[] letters = word.toCharArray();  
  36.         for(char c : letters) {  
  37.             if(node.son.containsKey(c)) {  
  38.                 node.son.get(c).num++;  
  39.             } else {  
  40.                 node.son.put(c, new TrieNode());  
  41.             }  
  42.             node = node.son.get(c);  
  43.         }  
  44.         node.count++;  
  45.     }  
  46.    
  47.     public int countWord(String word) { //计算字符串出现的次数  
  48.         return count(word, false);  
  49.     }  
  50.    
  51.     public int countPrefix(String prefix) { //计算前缀出现的次数  
  52.         return count(prefix, true);  
  53.     }  
  54.    
  55.     public boolean contain(String word) { //是否含有字符串  
  56.         return count(word, false) > 0;  
  57.     }  
  58.    
  59.     public int count(String word, boolean isPrefix) { //计算字符串/前缀出现的次数  
  60.         if(StringUtils.isBlank(word))  
  61.             return 0;  
  62.         TrieNode node = root;  
  63.         char[] letters = word.toCharArray();  
  64.         for(char c : letters) {  
  65.             if(node.son.containsKey(c))  
  66.                 node = node.son.get(c);  
  67.             else  
  68.                 return 0;  
  69.         }  
  70.         return isPrefix? node.num: node.count;  
  71.     }  
  72.    
  73.     public Map<String, Integer> getSortedWordsAndCounts() { //获取排序号的字符串和其出现次数  
  74.         Map<String, Integer> map = new TreeMap<>();  
  75.         getSortedWordsAndCounts(root, map, StringUtils.EMPTY);  
  76.         return map;  
  77.     }  
  78.     private void getSortedWordsAndCounts(TrieNode node, Map<String, Integer> map, String pre) {  
  79.         for(Map.Entry<Character,TrieNode> e: node.son.entrySet()) {  
  80.             String prefix = pre + e.getKey();  
  81.             if(e.getValue().count > 0) {  
  82.                 map.put(prefix, e.getValue().count);  
  83.             }  
  84.             getSortedWordsAndCounts(e.getValue(), map, prefix);  
  85.         }  
  86.     }  
  87.    
  88.     public Collection<String> getSortedWords() { //获取排好序的字符串  
  89.         Collection<String> list = new ArrayList<>();  
  90.         getSortedWords(root, list, StringUtils.EMPTY);  
  91.         return list;  
  92.     }  
  93.     private void getSortedWords(TrieNode node, Collection<String> list, String pre) {  
  94.         for(Map.Entry<Character,TrieNode> e: node.son.entrySet()) {  
  95.             String prefix = pre + e.getKey();  
  96.             if(e.getValue().count > 0) {  
  97.                 list.add(prefix);  
  98.             }  
  99.             getSortedWords(e.getValue(), list, prefix);  
  100.         }  
  101.     }  
  102.    
  103.     public String getMaxCommonPrefix() { //获取最大公共前缀  
  104.         TrieNode node = root;  
  105.         String maxPrefix = StringUtils.EMPTY;  
  106.         while(node.son.size() == 1 && node.count == 0) {  
  107.             for(Map.Entry<Character,TrieNode> e: node.son.entrySet()) {  
  108.                 node = e.getValue();  
  109.                 maxPrefix += e.getKey();  
  110.             }  
  111.         }  
  112.         return maxPrefix;  
  113.     }  
  114.    
  115.     public static void main(String[] args) { //测试  
  116.         Trie trie = new Trie();  
  117. //        trie.add("he");  
  118.         trie.add("hf");  
  119.         trie.add("hfz");  
  120.         trie.add("hfz");  
  121.         trie.add("hfz");  
  122.         trie.add("hfzy");  
  123. //        trie.add("hg");  
  124. //        trie.add("eh");  
  125. //        trie.add("eh");  
  126. //        trie.add("ek");  
  127.    
  128.         System.out.println(trie.countWord("hfz"));  
  129.         System.out.println(trie.countPrefix("hfz"));  
  130.         System.out.println(trie.contain("eh"));  
  131.         System.out.println(trie.getSortedWords());  
  132.         System.out.println(trie.getSortedWordsAndCounts());  
  133.         System.out.println(trie.getMaxCommonPrefix());  
  134.     }  
  135.    
  136. }

0 0
原创粉丝点击