Trie树,即字典树、前缀树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较。
总的来说,Trie的思想是以空间换空间,利用字符串的公共前缀来降低查找操作带来的时间开销以达到提高效率的目的。
主要性质

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

一.字典树的定义

  1. public class Trie{
  2. Trie[] root; //孩子节点,代表下一个字符
  3. boolean isPause;//是否终止
  4. public Trie(){
  5. root = new Trie[26];
  6. isPause = false;
  7. }
  8. }

二.字典树的创建

void insert(String word)向前缀树中插入字符串 word 。
栗子:好比假设有b,abc,abd,bcd,abcd,efg,hii 这6个单词,那我们创建trie树就得到:
数据结构之字典树 - 图1
所以我们可以得到以下代码:

  1. /** Inserts a word into the trie. */
  2. public void insert(String word) {
  3. int length = word.length();
  4. Trie cur = this;
  5. for(int i = 0; i < length; i++){
  6. int index =word.charAt(i) - 'a'; //这里获得第i个字符的所在位置
  7. if(cur.children[index] == null){//如果该分支不存在,那么创建
  8. cur.children[index] = new Trie();
  9. }
  10. cur = cur.children[index]; //令cur指向当前单词,准备执行下一次操作
  11. }
  12. //单词插入完成后,需要将其此单词末尾字符的标记位 置true,代表此字符是单词的结尾
  13. cur.isPause = true;
  14. }

三.字符串的查找

  1. /** Returns if the word is in the trie. */
  2. public boolean search(String word) {
  3. int length = word.length();
  4. Trie cur = this;
  5. for(int i = 0; i < length; i++){
  6. int index = word.charAt(i) - 'a';
  7. //如果下一个字符匹配不上,直接返回。
  8. if(cur.children[index] == null){
  9. return false;
  10. }else{
  11. cur = cur.children[index];
  12. }
  13. }
  14. //遍历所有待查询单词的字符,返回待查询末尾字符是否为一个单词的结尾字符。
  15. return cur.isPause;
  16. }

四.字符串前缀的查找

boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

  1. /** Returns if there is any word in the trie that starts with the given prefix. */
  2. public boolean startsWith(String prefix) {
  3. int length = prefix.length();
  4. Trie cur = this;
  5. for(int i = 0; i < length; i++){
  6. int index = prefix.charAt(i) - 'a';
  7. //如果下一个字符匹配不上,直接返回。
  8. if(cur.children[index] == null){
  9. return false;
  10. }else{
  11. cur = cur.children[index];
  12. }
  13. }
  14. //这里和字符串查询的区别在于, 只要满足一个单词的前缀就返回true
  15. return true;
  16. }

五.代码汇总

  1. class Trie {
  2. Trie[] children;
  3. boolean isPause;
  4. /** Initialize your data structure here. */
  5. public Trie() {
  6. children = new Trie[26];
  7. isPause = false;
  8. }
  9. /** Inserts a word into the trie. */
  10. public void insert(String word) {
  11. int length = word.length();
  12. Trie cur = this;
  13. for(int i = 0; i < length; i++){
  14. int index =word.charAt(i) - 'a';
  15. if(cur.children[index] == null){
  16. cur.children[index] = new Trie();
  17. }
  18. cur = cur.children[index];
  19. }
  20. cur.isPause = true;
  21. }
  22. /** Returns if the word is in the trie. */
  23. public boolean search(String word) {
  24. int length = word.length();
  25. Trie cur = this;
  26. for(int i = 0; i < length; i++){
  27. int index = word.charAt(i) - 'a';
  28. if(cur.children[index] == null){
  29. return false;
  30. }else{
  31. cur = cur.children[index];
  32. }
  33. }
  34. return cur.isPause;
  35. }
  36. /** Returns if there is any word in the trie that starts with the given prefix. */
  37. public boolean startsWith(String prefix) {
  38. int length = prefix.length();
  39. Trie cur = this;
  40. for(int i = 0; i < length; i++){
  41. int index = prefix.charAt(i) - 'a';
  42. if(cur.children[index] == null){
  43. return false;
  44. }else{
  45. cur = cur.children[index];
  46. }
  47. }
  48. return true;
  49. }
  50. }
  51. /**
  52. * Your Trie object will be instantiated and called as such:
  53. * Trie obj = new Trie();
  54. * obj.insert(word);
  55. * boolean param_2 = obj.search(word);
  56. * boolean param_3 = obj.startsWith(prefix);
  57. */

总结

时间复杂度:初始化为 O(1),其余操作为 O(|S|),其中 |S| 是每次插入或查询的字符串的长度。