定义
Trie,又称前缀树或字典树,是一棵有根树,其每个节点包含以下字段:
- 指向子节点的指针数组 children。数组长度为 26,即小写英文字母的数量。此时 children[0] 对应小写字母 a,children[1] 对应小写字母 b,…,children[25] 对应小写字母 z。
- 布尔字段 isEnd,表示该节点是否为字符串的结尾
典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
前缀树的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。
给出一组单词,inn, int, at, age, adv,ant, adv 我们可以得到下面的Trie:
题目:208. 实现 Trie (前缀树) - 力扣(LeetCode)
A trie (pronounced as “try”) or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Implement the Trie class:
- Trie() Initializes the trie object.
- void insert(String word) Inserts the string word into the trie.
- boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
- boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
HashMap实现
class Trie {public String value;public Map<Character, Trie> children;public boolean isEnd;public Trie() {this.value = null;this.isEnd = false;this.children = new HashMap<>();}public void insert(String word) {if (search(word)) return;char []str = word.toCharArray();int len = str.length;int i;Trie currentTrie = new Trie();Map<Character, Trie> ths = this.children;for (i = 0; i < len; i++){if (ths.containsKey(str[i])) {currentTrie = ths.get(str[i]);ths = ths.get(str[i]).children;}else {Trie newTire = new Trie();ths.put(str[i], newTire);ths = newTire.children;if (i == len - 1){newTire.value = word;newTire.isEnd = true;return;}}}if (!currentTrie.isEnd) currentTrie.value = word;}public boolean search(String word) {if (find(word) != null) return true;return false;}private String find (String word){char []str = word.toCharArray();int len = str.length;int i;String res = null;Map<Character, Trie> ths = this.children;for (i = 0; i < len; i++){if (ths.containsKey(str[i])) {res = ths.get(str[i]).value;ths = ths.get(str[i]).children;}else break;}if (i != len) res = null;return res;}public boolean startsWith(String prefix) {char []str = prefix.toCharArray();int len = str.length;int i;Map<Character, Trie> ths = this.children;for (i = 0; i < len; i++){if (ths.containsKey(str[i])) {ths = ths.get(str[i]).children;}else break;}if (i == len) return true;return false;}}/*** Your Trie object will be instantiated and called as such:* Trie obj = new Trie();* obj.insert(word);* boolean param_2 = obj.search(word);* boolean param_3 = obj.startsWith(prefix);*/
数组实现
class Trie {public String value;public Trie[] nodes;public boolean isEnd;public Trie() {this.nodes = new Trie[26];this.value = null;this.isEnd = false;}public void insert(String word) {char[] str = word.toCharArray();Trie currentTrie = this;for (char c : str){if (currentTrie.nodes[c-'a'] == null) currentTrie.nodes[c-'a'] = new Trie();currentTrie = currentTrie.nodes[c-'a'];}currentTrie.isEnd = true;currentTrie.value = word;}public boolean search(String word) {char[] str = word.toCharArray();Trie currentTrie = this;for (char c : str){if (currentTrie.nodes[c-'a'] == null) return false;currentTrie = currentTrie.nodes[c-'a'];}return currentTrie.isEnd;}public boolean startsWith(String prefix) {char[] str = prefix.toCharArray();Trie currentTrie = this;for (char c : str){if (currentTrie.nodes[c-'a'] == null) return false;currentTrie = currentTrie.nodes[c-'a'];}return true;}}/*** Your Trie object will be instantiated and called as such:* Trie obj = new Trie();* obj.insert(word);* boolean param_2 = obj.search(word);* boolean param_3 = obj.startsWith(prefix);*/
