题目
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
- Trie() 初始化前缀树对象。
- void insert(String word) 向前缀树中插入字符串 word 。
- boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
- boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
示例:
输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]
解释
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
提示:
1 <= word.length, prefix.length <= 2000
word 和 prefix 仅由小写英文字母组成
insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
方法:字典树
Trie,又称前缀树或字典树,是一棵有根树,其每个节点包含以下字段:
- 指向子节点的指针数组children。对于本题而言,数组长度为 26,即小写英文字母的数量。此时children[0] 对应小写字母 a,children[1]对应小写字母 b,…,children[25]对应小写字母 z。
- 布尔字段 isEnd,表示该节点是否为字符串的结尾。
例如:
三个单词 “sea”,“sells”,“she” 的 Trie树
简化成
插入字符串
描述:向 Trie 中插入一个单词 word
实现:我们从字典树的根开始,插入字符串。对于当前字符对应的子节点,有两种情况:
- 子节点存在。沿着指针移动到子节点,继续处理下一个字符。
- 子节点不存在。创建一个新的子节点,记录在 children数组的对应位置上,然后沿着指针移动到子节点,继续搜索下一个字符。
重复以上步骤,直到处理字符串的最后一个字符,然后将当前节点标记为字符串的结尾。
查找
描述:查找 Trie 中是否存在单词 word
实现:从根结点的子结点开始,一直向下匹配即可,如果出现结点值为空就返回 false,如果匹配到了最后一个字符,那我们只需判断 next->isEnd即可。
查找前缀
描述:判断 Trie 中是或有以 prefix 为前缀的单词
实现:我们从字典树的根开始,查找前缀。对于当前字符对应的子节点,有两种情况:
- 子节点存在。沿着指针移动到子节点,继续搜索下一个字符。
- 子节点不存在。说明字典树中不包含该前缀,返回空指针。
- 重复以上步骤,直到返回空指针或搜索完前缀的最后一个字符。
若搜索到了前缀的末尾,就说明字典树中存在该前缀。此外,若前缀末尾对应节点的 isEnd为真,则说明字典树中存在该字符串。
和 search 操作类似,只是不需要判断最后一个字符结点的isEnd,因为既然能匹配到最后一个字符,那后面一定有单词是以它为前缀的。
代码
class Trie {
class TireNode {
private boolean isEnd;
TireNode[] next;
public TireNode() {
isEnd = false;
next = new TireNode[26];
}
}
private TireNode root;
public Trie() {
root = new TireNode();
}
public void insert(String word) {
TireNode node = root;
for (char c : word.toCharArray()) {
if (node.next[c - 'a'] == null) {
node.next[c - 'a'] = new TireNode();
}
node = node.next[c - 'a'];
}
node.isEnd = true;
}
public boolean search(String word) {
TireNode node = root;
for (char c : word.toCharArray()) {
node = node.next[c - 'a'];
if (node == null) {
return false;
}
}
return node.isEnd;
}
public boolean startsWith(String prefix) {
TireNode node = root;
for (char c : prefix.toCharArray()) {
node = node.next[c - 'a'];
if (node == null) {
return false;
}
}
return true;
}
}
或者
class Trie {
//每个节点最多有26个不同的小写字母
private class TrieNode{
private boolean isEnd;//是否是单词(标记)
private TrieNode[] next;//26个小写字母
public TrieNode(){
isEnd=false;
next=new TrieNode[26];
}
}
//根节点,根节点是不存储任何字母的,从根节点的子节点开始存储
private TrieNode root;
/** Initialize your data structure here. */
public Trie() {
root=new TrieNode();
}
/** Inserts a word into the trie. */
public void insert(String word) {
TrieNode curr=root;
for(int i=0;i<word.length();i++){
int index=word.charAt(i)-'a';
//判断字符有没有创建,如果没有创建就创建
if(curr.next[index]==null){
curr.next[index]=new TrieNode();
//中间的字符不是完整的单词
curr.next[index].isEnd = false;
}
//这个把当前字符的前缀树作为下个搜索的结点,Trie中的next数组其实就是插入单词的
curr=curr.next[index];
}
//最后一个字符才能构成一个完整的单词
curr.isEnd=true;
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode curr=root;
for(int i=0;i<word.length();i++){
int index=word.charAt(i)-'a';
if(curr.next[index]==null){
return false;
}
curr=curr.next[index];
}
return curr.isEnd;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode curr=root;
for(int i=0;i<prefix.length();i++){
int index=prefix.charAt(i)-'a';
if(curr.next[index]==null){
//若没有遍历完给定的前缀子串,直接返回false
return false;
}
curr=curr.next[index];
}
return true;// 直接返回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);
*/
总结
通过以上介绍和代码实现我们可以总结出 Trie 的几点性质:
-
Trie 的形状和单词的插入或删除顺序无关,也就是说对于任意给定的一组单词,Trie 的形状都是唯一的。
-
查找或插入一个长度为 L 的单词,访问 next 数组的次数最多为 L+1,和 Trie 中包含多少个单词无关。
-
Trie 的每个结点中都保留着一个字母表,这是很耗费空间的。如果 Trie 的高度为 n,字母表的大小为 m,最坏的情况是 Trie 中还不存在前缀相同的单词,那空间复杂度就为 O(mn)。
-
Trie 应用场景:一次建树,多次查询。
复杂度分析
-
时间复杂度:初始化为 O(1),其余操作为 O(∣S∣),其中 ∣S∣是每次插入或查询的字符串的长度。
-
空间复杂度:O(∣T∣⋅Σ),其中 ∣T∣ 为所有插入字符串的长度之和,Σ 为字符集的大小,本题Σ=26。
参考资料:https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/trie-tree-de-shi-xian-gua-he-chu-xue-zhe-by-huwt/