字典树不言而喻就是类似字典的树形数据结构。假如要查找单词dog,那我们肯定是从字典的d部分查起;单词dog和doge自然也是相去不远,因为他们有公共的前缀dog;字典的编排也是按照字典序的…

字典树的常见应用

  1. 字符串检索、查询
  2. 词频统计
  3. 字符串排序
  4. 前缀匹配(搜索引擎、Linux指令自动补全)

字典树的实现

1. 正规字典树

定义字典树数据结构,用指针指向下一层子树,但是对空间要求较高,示意图如图:
字典树(Trie) 单词查找树 - 图1

此为字典树节点属性:

  1. public static class TrieNode{
  2. public TrieNode[] tn;
  3. public int num;
  4. public TrieNode(){
  5. tn = new TrieNode[26];
  6. }
  7. }

字典树的根节点,不存数据

  1. public static TrieNode root = new TrieNode();

插入指定字符串s到字典树,逐个将s中的字母放到对的位置 上

  1. public static void insert(String s){
  2. TrieNode temp = root;
  3. for(int i = 0;i < s.length();i++){
  4. if(temp.tn[s.charAt(i)-'a'] == null)
  5. temp.tn[s.charAt(i)-'a'] = new TrieNode();
  6. temp = temp.tn[s.charAt(i)-'a'];
  7. temp.num++;
  8. }
  9. }

检索单词s或前缀s出现次数

  1. public static int find(String s){
  2. TrieNode temp = root;
  3. for(int i = 0;i < s.length();i++){
  4. if(temp.tn[s.charAt(i)-'a'] == null)
  5. return 0;
  6. temp = temp.tn[s.charAt(i)-'a'];
  7. }
  8. return temp.num;
  9. }

字典序输出字典树

  1. public static void sort(TrieNode root,String s){
  2. if(sortCheck(root)){
  3. System.out.println(s);
  4. return;
  5. }
  6. for(int i = 0;i < 26;i++){
  7. if(root.tn[i] != null){
  8. String temp = s;
  9. temp += String.valueOf((char)('a'+i));
  10. sort(root.tn[i],temp);
  11. }
  12. }
  13. }
  14. public static boolean sortCheck(TrieNode root){
  15. for(int i = 0;i < 26;i++)
  16. if(root.tn[i] != null)
  17. return false;
  18. return true;
  19. }

完整源代码如下

  1. public class Trie {
  2. public static TrieNode root = new TrieNode();
  3. public static class TrieNode{
  4. public TrieNode[] tn;
  5. public int num;
  6. public TrieNode(){
  7. tn = new TrieNode[26];
  8. }
  9. }
  10. public static void insert(String s){
  11. TrieNode temp = root;
  12. for(int i = 0;i < s.length();i++){
  13. if(temp.tn[s.charAt(i)-'a'] == null)
  14. temp.tn[s.charAt(i)-'a'] = new TrieNode();
  15. temp = temp.tn[s.charAt(i)-'a'];
  16. temp.num++;
  17. }
  18. }
  19. public static int find(String s){
  20. TrieNode temp = root;
  21. for(int i = 0;i < s.length();i++){
  22. if(temp.tn[s.charAt(i)-'a'] == null)
  23. return 0;
  24. temp = temp.tn[s.charAt(i)-'a'];
  25. }
  26. return temp.num;
  27. }
  28. public static boolean sortCheck(TrieNode root){
  29. for(int i = 0;i < 26;i++)
  30. if(root.tn[i] != null)
  31. return false;
  32. return true;
  33. }
  34. public static void sort(TrieNode root,String s){
  35. if(sortCheck(root)){
  36. System.out.println(s);
  37. return;
  38. }
  39. for(int i = 0;i < 26;i++){
  40. if(root.tn[i] != null){
  41. String temp = s;
  42. temp += String.valueOf((char)('a'+i));
  43. sort(root.tn[i],temp);
  44. }
  45. }
  46. }
  47. public static void main(String[] args) {
  48. insert("gerge");
  49. insert("eroni");
  50. insert("cwrf");
  51. insert("oqpwinfw");
  52. insert("wnkca");
  53. insert("axcnasixu");
  54. insert("abf");
  55. insert("abcesf");
  56. sort(root,"");
  57. }
  58. }

2. 数组实现字典树

竞赛中常用,是一种更为紧凑的存储方式。

基本属性

  1. public static int[][] trie = new int[100000][26];
  2. public static int[] num = new int[100000];
  3. public static int pos=1;

数组实现与第一种实现大致相同,主要是注意理解pos是新分配的存储位置(行数),笔者建议代入几条数据模拟一下,完整源代码如下

  1. public class TrieArray {
  2. public static int[][] trie = new int[100000][26];
  3. public static int[] num = new int[100000];
  4. public static int pos=1;
  5. public static void main(String[] args) {
  6. insert("gerge");
  7. insert("eroni");
  8. insert("cwrf");
  9. insert("oqpwinfw");
  10. insert("wnkca");
  11. insert("axcnasixu");
  12. insert("abf");
  13. insert("abcesf");
  14. sort(0,"");
  15. }
  16. public static boolean sortCheck(int line){
  17. for(int i = 0;i < 26;i++)
  18. if(trie[line][i] != 0)
  19. return false;
  20. return true;
  21. }
  22. public static void sort(int line,String s){
  23. if(sortCheck(line)){
  24. System.out.println(s);
  25. return;
  26. }
  27. for(int i = 0;i < 26;i++){
  28. String temp = s;
  29. if(trie[line][i] != 0){
  30. temp += String.valueOf((char)('a'+i));
  31. sort(trie[line][i],temp);
  32. }
  33. }
  34. }
  35. public static void insert(String s){
  36. int p = 0;
  37. for(int i = 0;i < s.length();i++){
  38. int n = s.charAt(i) - 'a';
  39. if(trie[p][n] == 0)
  40. trie[p][n] = pos++;
  41. p = trie[p][n];
  42. num[p]++;
  43. }
  44. }
  45. public static int find(String s){
  46. int p = 0;
  47. for(int i = 0;i < s.length();i++){
  48. int n = s.charAt(i) - 'a';
  49. if(trie[p][n] == 0)
  50. return 0;
  51. p = trie[p][n];
  52. }
  53. return num[p];
  54. }
  55. }

复杂度

  • 时间复杂度:插入和查找单词的复杂度都是O(m),m是待插入/查询字符串的长度
  • 空间复杂度:有公共前缀的单词只需要存一次公共前缀,节省了空间