-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoImplementation.java
46 lines (41 loc) · 1.24 KB
/
DemoImplementation.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
class Main {
public static class TrieNode{
char data;
TrieNode[] children = new TrieNode[26];
boolean isTerminal;
TrieNode(char ch){
data = ch;
for(int i = 0; i < 26; i++){
children[i] = null;
}
isTerminal = false;
}
}
public static class Trie{
TrieNode root = new TrieNode(' ');
void insertUtil(TrieNode root, String word){
if(word.length() == 0){
root.isTerminal = true;
return;
}
// assuming words is in caps
int index = word.charAt(0) - 'A';
TrieNode child;
// present case
if(root.children[index] != null){
child = root.children[index];
}else{ // absent case
child = new TrieNode(word.charAt(0));
root.children[index] = child;
}
insertUtil(child, word.substring(1));
}
void insertWord(String word){
insertUtil(root, word.toUpperCase());
}
}
public static void main(String[] args) {
Trie t = new Trie();
t.insertWord("Trilochna");
}
}