-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementTriePrefixTree.java
More file actions
338 lines (257 loc) · 10.1 KB
/
ImplementTriePrefixTree.java
File metadata and controls
338 lines (257 loc) · 10.1 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package Algorithms.Tries;
import java.util.ArrayList;
import java.util.List;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 23 Feb 2025
* @link 208. Implement Trie (Prefix Tree) <a href="https://leetcode.com/problems/implement-trie-prefix-tree/">LeetCode Link</a>
* @topics HashTable, String, Design, Trie
* @companies Oracle(4), MongoDB(4), Amazon(3), Bloomberg(2), Microsoft(5), Google(4), Meta(3), General Motors(3), Apple(2), DoorDash(10), Roblox(7), DocuSign(5), Citadel(3), Grammarly(3), Adobe(2), Walmart Labs(2), TikTok(2), Goldman Sachs(2), Uber(2)
* <pre>
*
THOUGHTS:
---------
1) Max num of children for root node is 26 alphabets.
2) when we inserted "apple", "app", "ape", "boy" in Trie, it looks like:
""
________|___________________
| |
a b
| |
p o
__________|________ |
| | |
p isEnd=true e isEnd=true y isEnd=true
|
l
|
e isEnd=true
*
* </pre>
*/
@SuppressWarnings("unused")
public class ImplementTriePrefixTree {
public static void main(String[] args) {
System.out.println("Implement Trie Prefix Tree --- Original");
Trie trie = new Trie();
trie.insert("apple");
System.out.println("trie.search(\"apple\") => " + trie.search("apple"));
System.out.println("trie.search(\"app\") => " + trie.search("app"));
System.out.println("trie.startsWith(\"app\") => " + trie.startsWith("app"));
trie.insert("app");
System.out.println("trie.search(\"app\") => " + trie.search("app"));
System.out.println("\nImplement Trie Prefix Tree --- MyApproach");
TrieMyApproach trieMyApproach = new TrieMyApproach();
trieMyApproach.insert("apple");
System.out.println("trieMyApproach.search(\"apple\") => " + trieMyApproach.search("apple"));
System.out.println("trieMyApproach.search(\"app\") => " + trieMyApproach.search("app"));
System.out.println("trieMyApproach.startsWith(\"app\") => " + trieMyApproach.startsWith("app"));
trieMyApproach.insert("app");
System.out.println("trieMyApproach.search(\"app\") => " + trieMyApproach.search("app"));
}
static class Trie {
static class TrieNode {
TrieNode[] children = new TrieNode[26]; // or HashMap
boolean isEnd = false;
}
TrieNode root = new TrieNode();
public void insert(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.children[c - 'a'] == null) {
curr.children[c - 'a'] = new TrieNode();
}
curr = curr.children[c - 'a'];
}
curr.isEnd = true;
}
public boolean search(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.children[c - 'a'] == null) {
return false;
}
curr = curr.children[c - 'a'];
}
return curr.isEnd;
}
public boolean startsWith(String prefix) {
TrieNode curr = root;
for (char c : prefix.toCharArray()) {
if (curr.children[c - 'a'] == null) {
return false;
}
curr = curr.children[c - 'a'];
}
return true;
}
}
static class Trie2 {
static class TrieNode {
private final int R = 26;
private final TrieNode[] links = new TrieNode[R];
private boolean isEnd;
public boolean containsKey(char ch) {return links[ch -'a'] != null;}
public TrieNode get(char ch) {return links[ch -'a'];}
public void put(char ch, TrieNode node) {links[ch -'a'] = node;}
public void setEnd() {isEnd = true;}
public boolean isEnd() {return isEnd;}
}
private final TrieNode root = new TrieNode();
public void insert(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
if (!node.containsKey(currentChar)) {
node.put(currentChar, new TrieNode());
}
node = node.get(currentChar);
}
node.setEnd();
}
public boolean search(String word) {
TrieNode node = searchPrefix(word);
return node != null && node.isEnd();
}
public boolean startsWith(String prefix) {
TrieNode node = searchPrefix(prefix);
return node != null;
}
private TrieNode searchPrefix(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (node.containsKey(c)) {
node = node.get(c);
} else {
return null;
}
}
return node;
}
}
static class TrieMyApproach {
TrieMyApproach[] arr = new TrieMyApproach[26];
char c ='\0'; // optional
boolean isEnd = false;
public void insert(String word) {
TrieMyApproach trav = this;
for(char ch: word.toCharArray()) {
if(trav.arr[ch-'a'] == null) trav.arr[ch-'a'] = new TrieMyApproach();
trav = trav.arr[ch-'a'];
trav.c=ch; // optional
}
trav.isEnd=true;
}
public boolean search(String word) {
TrieMyApproach trav = this;
for(char ch: word.toCharArray()) {
trav = trav.arr[ch-'a'];
if(trav==null) return false;
trav.c=ch; // optional
}
return trav.isEnd;
}
public boolean startsWith(String prefix) {
TrieMyApproach trav = this;
for(char ch: prefix.toCharArray()) {
trav = trav.arr[ch-'a'];
if(trav==null) return false;
trav.c=ch; // optional
}
return true;
}
}
static class TrieMyApproach2 {
class Tree {
char c;
boolean isEnd;
List<Tree> lst = new ArrayList<>(); // or HashMap
Tree() {}
Tree(char c) {this.c = c;}
}
private Tree root = new Tree();
public void insert(String word) {
insert(word, 0, root);
}
// complete word find i.e word 1st char is child of root node && word last char contains isEnd=true;
public boolean search(String word) {
if (root.lst.size()==0) return false;
return search(word, 0, root);
}
// prefix 1st char is child of root node and prefix last char node not
public boolean startsWith(String prefix) {
if (root.lst.size()==0) return false;
return startsWith(prefix, 0, root);
}
private void insert(String word, int i, Tree node) {
Tree child = node.lst.stream().filter(x->x.c==word.charAt(i)).findFirst().orElse(null);
if (child==null) { // child not found
child = new Tree(word.charAt(i));
node.lst.add(child);
}
if (i==word.length()-1) {
child.isEnd = true;
return;
}
insert(word, i+1, child);
}
private boolean search(String word, int i, Tree node) {
Tree child = node.lst.stream().filter(x->x.c==word.charAt(i)).findFirst().orElse(null);
if (child==null) return false;
if (i==word.length()-1) return child.isEnd;
return search(word, i+1, child);
}
private boolean startsWith(String word, int i, Tree node) {
if (i==word.length()) return true;
Tree child = node.lst.stream().filter(x->x.c==word.charAt(i)).findFirst().orElse(null);
if (child==null) return false;
return startsWith(word, i+1, child);
}
}
static class Trie3 {
Node root;
public Trie3() {
root = new Node();
}
public void insert(String word) {
root.insert(word, 0);
}
public boolean search(String word) {
return root.search(word, 0, false);
}
public boolean startsWith(String prefix) {
return root.search(prefix, 0, true);
}
static class Node {
Node[] nodes;
boolean isEnd;
Node() {
nodes = new Node[26];
}
private void insert(String word, int idx) {
if (idx >= word.length()) return;
int i = word.charAt(idx) - 'a';
if (nodes[i] == null) {
nodes[i] = new Node();
}
if (idx == word.length()-1) nodes[i].isEnd = true;
nodes[i].insert(word, idx+1);
}
private boolean search(String word, int idx, boolean checkPrefix) {
if (idx >= word.length()) return false;
Node node = nodes[word.charAt(idx) - 'a'];
if (node == null) return false;
if (idx == word.length() - 1 && (checkPrefix || node.isEnd)) return true;
return node.search(word, idx+1, checkPrefix);
}
private boolean startsWith(String prefix, int idx) {
if (idx >= prefix.length()) return false;
Node node = nodes[prefix.charAt(idx) - 'a'];
if (node == null) return false;
if (idx == prefix.length() - 1) return true;
return node.startsWith(prefix, idx+1);
}
}
}
}