-
Notifications
You must be signed in to change notification settings - Fork 4
/
ImplementTriePrefixTree.java
48 lines (42 loc) · 1.39 KB
/
ImplementTriePrefixTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.dbc.code;
import java.util.HashMap;
import java.util.Map;
public class ImplementTriePrefixTree {
class TreeNode {
public char token;
public boolean end;
public final Map<Character, TreeNode> next = new HashMap<>();
public TreeNode() {}
public TreeNode(char ch, boolean end) {
this.token = ch;
this.end = end;
}
}
private final TreeNode root = new TreeNode();
public ImplementTriePrefixTree() {
}
public void insert(String word) {
TreeNode node = this.root;
for (int i = 0; i < word.length(); i++) {
if (!node.next.containsKey(word.charAt(i))) node.next.put(word.charAt(i), new TreeNode(word.charAt(i), false));
node = node.next.get(word.charAt(i));
}
node.end = true;
}
public boolean search(String word) {
TreeNode node = this.root;
for (int i = 0; i < word.length(); i++) {
if (!node.next.containsKey(word.charAt(i))) return false;
node = node.next.get(word.charAt(i));
}
return node.end;
}
public boolean startsWith(String prefix) {
TreeNode node = this.root;
for (int i = 0; i < prefix.length(); i++) {
if (!node.next.containsKey(prefix.charAt(i))) return false;
node = node.next.get(prefix.charAt(i));
}
return true;
}
}