- 题目连接:208. 实现 Trie (前缀树)
1- 思路
数组哈希表 + isEnd 字段
前缀树的当前结点,存储的是下一个子节点的元素值。当前结点中的内容是一个哈希表,和 isEnd 字段
① 前缀树节点
class TrieNode{
boolean isEnd = false;
TrieNode[] children;
public TriNode(){
children = new TrieNode[26];
}
}
② 插入操作
- 2.1 获取
root
结点赋值为cur
- 2.2 遍历 word 进行哈希,创造子节点的操作
- 2.3 插入完移动
cur
指针 - 2.4 最后赋值
isEnd
③ search操作
- 与插入代码类似,区别点在于 遍历的过程:如果遇到
null
直接返回false
- 最终返回的是
isEnd
④ startWith 操作
- 和 search 操作的区别在于,返回值
2- 实现
⭐208. 实现 Trie (前缀树)——题解思路
class Trie {
class TrieNode{
TrieNode[] children;
boolean isEnd;
public TrieNode(){
children = new TrieNode[26];
isEnd = false;
}
}
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
// 遍历word
TrieNode cur = root;
for(int i = 0 ; i < word.length();i++){
char c = word.charAt(i);
int index = c-'a';
if(cur.children[index] == null){
cur.children[index] = new TrieNode();
}
cur = cur.children[index];
}
cur.isEnd = true;
}
public boolean search(String word) {
// 遍历word
TrieNode cur = root;
for(int i = 0 ; i < word.length();i++){
char c = word.charAt(i);
int index = c-'a';
if(cur.children[index] == null){
return false;
}
cur = cur.children[index];
}
return cur.isEnd;
}
public boolean startsWith(String prefix) {
// 遍历word
TrieNode cur = root;
for(int i = 0 ; i < prefix.length();i++){
char c = prefix.charAt(i);
int index = c-'a';
if(cur.children[index] == null){
return false;
}
cur = cur.children[index];
}
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);
*/
3- ACM 实现
class Trie {
class TrieNode {
TrieNode[] children;
boolean isEnd;
public TrieNode() {
children = new TrieNode[26];
isEnd = false;
}
}
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
int index = c - 'a';
if (cur.children[index] == null) {
cur.children[index] = new TrieNode();
}
cur = cur.children[index];
}
cur.isEnd = true;
}
public boolean search(String word) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
int index = c - 'a';
if (cur.children[index] == null) {
return false;
}
cur = cur.children[index];
}
return cur.isEnd;
}
public boolean startsWith(String prefix) {
TrieNode cur = root;
for (int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
int index = c - 'a';
if (cur.children[index] == null) {
return false;
}
cur = cur.children[index];
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Trie trie = new Trie();
while (scanner.hasNext()) {
String command = scanner.next();
if (command.equals("insert")) {
String word = scanner.next();
trie.insert(word);
} else if (command.equals("search")) {
String word = scanner.next();
System.out.println(trie.search(word));
} else if (command.equals("startsWith")) {
String prefix = scanner.next();
System.out.println(trie.startsWith(prefix));
}
}
scanner.close();
}
}