概述:

Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串。和普通树不同的地方是,相同的字符串前缀共享同一条分支。
例如:pool,prize,preview,prepare,produce,progress这些关键词的Tire树
字典树Tire - 图1

典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。
它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

基本性质:

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

应用场景:

字典数查找效率很高,时间复杂度是O(m),m是要查找的单词中包含的字母的个数,但是会浪费大量存放空指针的存储空间,属于以空间换时间的算法。

1、串快速检索

给出N个单词组成的熟词表,以及一篇全用小写英文书写的文章,请你按最早出现的顺序写出所有不在熟词表中的生词。

2、单词自动完成

编辑代码时,输入字符,自动提示可能的关键字、变量或函数等信息。

3、最长公共前缀

对所有串建立字典树,对于两个串的最长公共前缀的长度即他们所在的结点的公共祖先个数,于是,问题就转化为最近公共祖先问题。

4、串排序方面的应用

给定N个互不相同的仅由一个单词构成的英文名,让你将他们按字典序从小到大输出用字典树进行排序,采用数组的方式创建字典树,这棵树的每个结点的所有儿子很显然地按照其字母大小排序。对这棵树进行先序遍历即可。

算法题目

在力扣上也有关于Tire树的题目

208. 实现 Trie (前缀树)

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert(“apple”);

trie.search(“apple”); // 返回 true

trie.search(“app”); // 返回 false

trie.startsWith(“app”); // 返回 true

trie.insert(“app”);

trie.search(“app”); // 返回 true

说明:

你可以假设所有的输入都是由小写字母 a-z 构成的。

保证所有输入均为非空字符串。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

实现一:

  1. class Trie {
  2. public:
  3. /** Initialize your data structure here. */
  4. Trie() {
  5. }
  6. /** Inserts a word into the trie. */
  7. void insert(string word) {
  8. Trie* node = this;
  9. for(auto ch : word)
  10. {
  11. if(!node->next.count(ch))
  12. node->next[ch] = new Trie();
  13. node = node->next[ch];
  14. }
  15. node->is_word = true;
  16. }
  17. /** Returns if the word is in the trie. */
  18. bool search(string word) {
  19. Trie* node = this;
  20. for(auto &ch : word)
  21. {
  22. if(!node->next.count(ch))
  23. return false;
  24. node = node->next[ch];
  25. }
  26. return node->is_word;
  27. }
  28. /** Returns if there is any word in the trie that starts with the given prefix. */
  29. bool startsWith(string prefix) {
  30. Trie* node = this;
  31. for(auto &ch : prefix)
  32. {
  33. if(!node->next.count(ch))
  34. return false;
  35. node = node->next[ch];
  36. }
  37. return true;
  38. }
  39. private:
  40. bool is_word = false;
  41. map<char, Trie*> next = {};
  42. };
  43. /**
  44. * Your Trie object will be instantiated and called as such:
  45. * Trie* obj = new Trie();
  46. * obj->insert(word);
  47. * bool param_2 = obj->search(word);
  48. * bool param_3 = obj->startsWith(prefix);
  49. */

实现二:该实现方法只是单纯为了解题,并不是实现出Tire这个数据结构

  1. class Trie {
  2. public:
  3. struct tireNode {
  4. char c;
  5. map<char, tireNode*>next;
  6. bool endword;
  7. tireNode(char chr, bool happyend) : c(chr), endword(happyend){}
  8. };
  9. map<char, tireNode*>head;
  10. /** Initialize your data structure here. */
  11. Trie() {
  12. }
  13. /** Inserts a word into the trie. */
  14. void insert(string word) {
  15. if(word.size() == 0)
  16. return;
  17. char headchar = word[0];
  18. tireNode* tmphead = NULL;
  19. if(head.find(headchar) == head.end())
  20. {
  21. head[headchar] = new tireNode(headchar, false);
  22. }
  23. tmphead = head[headchar];
  24. for(int i = 1; i < word.size(); i++)
  25. {
  26. if(tmphead->next.find(word[i]) == tmphead->next.end())
  27. {
  28. tmphead->next[word[i]] = new tireNode(word[i], false);
  29. }
  30. tmphead = tmphead->next[word[i]];
  31. }
  32. tmphead->endword = true;
  33. return;
  34. }
  35. /** Returns if the word is in the trie. */
  36. bool search(string word) {
  37. if(word.size() == 0)
  38. return true;
  39. char headchar = word[0];
  40. if(head.find(headchar) == head.end())
  41. return false;
  42. tireNode *tmphead = head[headchar];
  43. for(int i = 1; i < word.size(); i++)
  44. {
  45. if(tmphead->next.find(word[i]) == tmphead->next.end())
  46. return false;
  47. tmphead = tmphead->next[word[i]];
  48. }
  49. if(tmphead->endword)
  50. return true;
  51. else
  52. return false;
  53. }
  54. /** Returns if there is any word in the trie that starts with the given prefix. */
  55. bool startsWith(string prefix) {
  56. if(prefix.size() == 0)
  57. return true;
  58. char headchar = prefix[0];
  59. if(head.find(headchar) == head.end())
  60. return false;
  61. tireNode *tmphead = head[headchar];
  62. for(int i = 1; i < prefix.size(); i++)
  63. {
  64. if(tmphead->next.find(prefix[i]) == tmphead->next.end())
  65. return false;
  66. tmphead = tmphead->next[prefix[i]];
  67. }
  68. return true;
  69. }
  70. };
  71. /**
  72. * Your Trie object will be instantiated and called as such:
  73. * Trie* obj = new Trie();
  74. * obj->insert(word);
  75. * bool param_2 = obj->search(word);
  76. * bool param_3 = obj->startsWith(prefix);
  77. */

参考资料:

https://www.cnblogs.com/Quincy/p/4898348.html