-
Notifications
You must be signed in to change notification settings - Fork 43
/
ImplementTrie.java
310 lines (269 loc) · 8.71 KB
/
ImplementTrie.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package LeetCodeJava.Trie;
// https://leetcode.com/problems/implement-trie-prefix-tree/
/**
* 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);
*/
import java.util.HashMap;
import java.util.Map;
public class ImplementTrie {
// V0
// IDEA : https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Tree/implement-trie-prefix-tree.py#L49
// modified by GPT
class TrieNode {
/** NOTE !!!!
*
* Define children as map structure:
*
* Map<String, TrieNode>
*
* -> string : current "element"
* -> TrieNode : the child object
*
*/
Map<String, TrieNode> children;
boolean isWord;
public TrieNode() {
children = new HashMap<>();
isWord = false;
}
}
/**
* NOTE !!!
*
* Define 2 classes
*
* 1) TrieNode
* 2) Trie
*
*/
class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
/**
* NOTE !!! get current node first
*/
TrieNode cur = root;
for (String c : word.split("")) {
cur.children.putIfAbsent(c, new TrieNode());
// move node to its child
cur = cur.children.get(c);
}
cur.isWord = true;
}
public boolean search(String word) {
/**
* NOTE !!! get current node first
*/
TrieNode cur = root;
for (String c : word.split("")) {
cur = cur.children.get(c);
if (cur == null) {
return false;
}
}
return cur.isWord;
}
public boolean startsWith(String prefix) {
/**
* NOTE !!! get current node first
*/
TrieNode cur = root;
for (String c : prefix.split("")) {
cur = cur.children.get(c);
if (cur == null) {
return false;
}
}
return true;
}
}
// V1
// https://leetcode.com/problems/implement-trie-prefix-tree/editorial/
class TrieNode2 {
// R links to node children
private TrieNode2[] links;
private final int R = 26;
private boolean isEnd;
public TrieNode2() {
links = new TrieNode2[R];
}
public boolean containsKey(char ch) {
return links[ch -'a'] != null;
}
public TrieNode2 get(char ch) {
return links[ch -'a'];
}
public void put(char ch, TrieNode2 node) {
links[ch -'a'] = node;
}
public void setEnd() {
isEnd = true;
}
public boolean isEnd() {
return isEnd;
}
}
class Trie2 {
private TrieNode2 root;
public Trie2() {
root = new TrieNode2();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode2 node = root;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
if (!node.containsKey(currentChar)) {
node.put(currentChar, new TrieNode2());
}
node = node.get(currentChar);
}
node.setEnd();
}
// search a prefix or whole key in trie and
// returns the node where search ends
private TrieNode2 searchPrefix(String word) {
TrieNode2 node = root;
for (int i = 0; i < word.length(); i++) {
char curLetter = word.charAt(i);
if (node.containsKey(curLetter)) {
node = node.get(curLetter);
} else {
return null;
}
}
return node;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode2 node = searchPrefix(word);
return node != null && node.isEnd();
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode2 node = searchPrefix(prefix);
return node != null;
}
}
// V2
// https://leetcode.com/problems/implement-trie-prefix-tree/solutions/3309950/java-easiest-solution/
class TrieNode3 {
boolean isWord;
TrieNode3[] children;
public TrieNode3() {
isWord = false;
children = new TrieNode3[26]; // 26 English lowercase letters
}
}
class Trie3 {
TrieNode3 root;
public Trie3() {
root = new TrieNode3();
}
public void insert(String word) {
TrieNode3 node = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode3();
}
node = node.children[index];
}
node.isWord = true;
}
public boolean search(String word) {
TrieNode3 node = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return node.isWord;
}
public boolean startsWith(String prefix) {
TrieNode3 node = root;
for (char c : prefix.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return true;
}
}
// V3
// https://leetcode.com/problems/implement-trie-prefix-tree/solutions/3308019/simple-implementation-in-java/
class Trie4 {
// Trie Node class
class TrieNode {
// to mark the last letter of a string as true
// that yes, a word ends at this letter
boolean isComplete;
// a node array of size 26
// to store nodes for 26 lowercase alphabets
TrieNode[] children;
public TrieNode() {
isComplete = false;
children = new TrieNode[26];
}
}
TrieNode root; // Declare the root node
public Trie4() {
root = new TrieNode(); // initialize the root node
}
public void insert(String word) {
TrieNode node = root; // start traversing from the root
for (char c : word.toCharArray()) {
// if the node index for the character is empty
if (node.children[c - 'a'] == null) {
// create a new node at that index
node.children[c - 'a'] = new TrieNode();
}
// jump to that node
node = node.children[c - 'a'];
}
// mark the last letter as true
// means a word ends at this letter
node.isComplete = true;
}
public boolean search(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
// if the node index for the character is empty
if (node.children[c - 'a'] == null) {
return false; // means word doesn't exist
}
// jump to the character node
node = node.children[c - 'a'];
}
// return if the last letter is marked as true/false
// if true -> a word ends at this character otherwise not
// if this exact word were inserted, we would have marked
// the last character as true for sure
return node.isComplete;
}
public boolean startsWith(String prefix) {
TrieNode node = root;
for (char c : prefix.toCharArray()) {
// if the node index for the character is empty
if (node.children[c - 'a'] == null) {
return false;
}
node = node.children[c - 'a'];
}
// all the character nodes were found in the tree
return true; // means prefix exists
}
}
}