208.实现Trie(前缀树)
问题: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
思路:List实现
class Trie {
private List<String> trie;
public Trie() {
this.trie = new ArrayList();
}
public void insert(String word) {
trie.add(word);
}
public boolean search(String word) {
return trie.contains(word);
}
public boolean startsWith(String prefix) {
if(trie.contains(prefix)){
return true;
}
for(String word: trie){
if(word.startsWith(prefix)){
return true;
}
}
return false;
}
}
- Map+List实现,时间上会比直接用List快一些
class Trie {
private Map<String, List<String>> trie;
private List<String> stringList;
public Trie() {
this.trie = new HashMap();
}
public void insert(String word) {
String firstChar = word.substring(0, 1);
stringList = trie.getOrDefault(firstChar, new ArrayList());
stringList.add(word);
trie.put(firstChar, stringList);
}
public boolean search(String word) {
String firstChar = word.substring(0, 1);
if(trie.containsKey(firstChar)){
stringList = trie.get(firstChar);
return stringList.contains(word);
}
return false;
}
public boolean startsWith(String prefix) {
String firstChar = prefix.substring(0, 1);
if(trie.containsKey(firstChar)){
stringList = trie.get(firstChar);
for(String w : stringList){
if(w.startsWith(prefix)){
return true;
}
}
}
return false;
}
}
- 自定义实现字典树(多叉树)
最终要实现的树是一颗多叉树,
每个节点包括指向子节点的指针数组children以及是否为字符串的结尾isEnd。
这道题只包含26个小写字母,所以可以用数组的下标来表示这些字母,数值则表示其子节点。
长这个样子:
这样每个节点就会含有大量的空链接
可以看作是这样的,方便理解
插入字符串
从根节点开始遍历,按字符依次插入,记住哈,我们用数组下标来表示字符,对应的值表示子节点
- 若子节点存在,即
children[index] != null
(index为根据当前字符计算出来的对应的数组下标),我们就将根据指针移动到子节点,继续下一个字符的处理 - 若不存在,就新建一个新节点,放在当前下标对应的位置上,然后根据指针移动到其对应的子节点(新建的节点),处理下一个字符。
重复以上步骤,直到将整个字符串全部插入,然后将当前节点的isEnd标志位置为true
查找前缀
从根开始查找,按照所给前缀的字符查找
- 若子节点存在,则根据指针移动到子节点,继续查找下一个字符
- 若子节点不存在,则返回空指针,说明所给前缀没找到
重复以上步骤,直到返回null或找到前缀的最后一个字符。
找到该前缀的最后一个字符,说明该前缀树中存在这个前缀
若这个字符对应的节点的标志位isEnd(表示是否有字符串以该字符结尾)为true,说明这个字符串在这颗前缀树里。代码如下
class Trie {
private Trie[] children;
private boolean isEnd;
public Trie() {
this.children = new Trie[26];
this.isEnd = false;
}
public void insert(String word) {
//从根节点开始
Trie node = this;
for(int i = 0; i < word.length(); i++){
char ch = word.charAt(i);
//根据ascii码计算其下标
int index = ch - 'a';
//子节点不存在
if(node.children[index] == null){
node.children[index] = new Trie();
}
//根据指针移动到子节点
node = node.children[index];
}
node.isEnd = true;
}
private Trie searchPrefix(String prefix){
//从根节点开始搜索
Trie node = this;
for(int i = 0; i < prefix.length(); i++){
char ch = prefix.charAt(i);
int index = ch - 'a';
//该前缀树中没有这个字符,该前缀不存在于该前缀树中
if(node.children[index] == null){
return null;
}
//根据指针移动到子节点
node = node.children[index];
}
//找到前缀的最后一个字符
return node;
}
public boolean search(String word) {
Trie node = searchPrefix(word);
return node != null && node.isEnd;
}
public boolean startsWith(String prefix) {
return searchPrefix(prefix) != null;
}
}
整理思路,记录博客,以便复习。若有误,望指正~