每日题解:LeetCode 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
解法
class Trie {
private Trie[] children;
private boolean isEnd;
public Trie() {
children = new Trie[26];
isEnd = false;
}
public void insert(String word) {
Trie node = this;
for (char ch : word.toCharArray()) {
int index = ch - 'a';
if (node.children[index] == null) {
node.children[index] = new Trie();
}
node = node.children[index];
}
node.isEnd = true;
}
public boolean search(String word) {
Trie node = startsWithPrefix(word);
return node != null && node.isEnd;
}
public boolean startsWith(String prefix) {
return startsWithPrefix(prefix) != null;
}
private Trie startsWithPrefix(String prefix) {
Trie node = this;
for (char ch : prefix.toCharArray()) {
int index = ch - 'a';
if (node.children[index] == null) {
return null;
}
node = node.children[index];
}
return node;
}
}
解题思路
字典树
Trie树,又称单词查找树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高
Trie
树当去掉所有的空链接s时,结构类似多叉树
但是,实际上,Trie
却和格子差不多,所以可以根据当前字符对应下标快速查找字符
由于本题只有小写英文字母组成,所以直接可以使用二维数组来实现 Trie 树
- 使用二维数组保存单词字符
- 使用
index
记录当前单词字符的在二维数组的下标,相当于格子的位置 - 使用
isEnd
标记当前的Trie
是否整个单词的结束
insert插入
实现:首先遍历 word字符串,与根节点的子节点匹配
- 如果子节点存在。沿着指针移动到子节点,继续处理下一个字符。
- 如果子节点不存在。创建一个新的子节点,记录在根节点数组的对应位置
index
上,指针指向当前节点,开辟新的节点,继续搜索下一个字符。
直到插入完 word 的最后一个字符,同时还要将最后一个结点isEnd = true
,表示单词的结束
public void insert(String word) {
Trie node=this;
for (char ch : word.toCharArray()) {
int index =ch - 'a';
if (node.children[index]==null){
node.children[index] = new Trie();
}
node=node.children[index];
}
node.isEnd=true;
}
search查找
描述:查找Trie
中是否存在单词 word
实现:从根结点的子结点开始,一直向下寻找数组上当前字符的index
是否为空,为空就直接返回null
,如果匹配结束,同时返回不是节点不是null
,则需要判断返回的节点isEnd
是否为true
,即最后一个字符
public boolean search(String word) {
Trie node=startsWithPrefix(word);
//判断当前节点是否为空或者是否到了最后一个字符
return node!=null && node.isEnd;
}
private Trie startsWithPrefix(String prefix){
Trie node=this;
for (char ch : prefix.toCharArray()) {
int index =ch - 'a';
if (node.children[index]==null){
return null;
}
node=node.children[index];
}
return node;
}
startsWith前缀匹配
描述:判断 Trie
中是或有以prefix
为前缀的单词
实现:和 search 类似,只是不需要判断当前返回节点的isEnd
,只要判断往下寻找返回的节点是否为null
public boolean startsWith(String prefix) {
return startsWithPrefix(prefix)!=null;
}
private Trie startsWithPrefix(String prefix){
Trie node=this;
for (char ch : prefix.toCharArray()) {
int index =ch - 'a';
//如果当前的节点对应下标是nul,返回null
if (node.children[index]==null){
return null;
}
//返回字符串对应的最后一个接入对象
node=node.children[index];
}
return node;
}