-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.java
92 lines (88 loc) · 2.77 KB
/
Trie.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
public class Trie {
static class TrieNode
{
TrieNode[] children = new TrieNode[26];
// isEndOfWord is true if the node represents
// end of a word
boolean isEndOfWord;
TrieNode(){
isEndOfWord = false;
for (int i = 0; i < 26; i++)
children[i] = null;
}
};
private static TrieNode root;
private static int charToIndex(char c){
return c >= 'A' && c <= 'Z'? c - 'A' : c - 'a';
}
private static void insert(TrieNode root, String word[]){
for(int i = 0; i < word.length; i++){
insert(root, word[i],0, word[i].length());
}
}
private static void insert(TrieNode root, String word, int start, int end){
if(start == end){
root.isEndOfWord = true;
return;
}
int cIndex = charToIndex(word.charAt(start));
if(root.children[cIndex] == null){
root.children[cIndex] = new TrieNode();
}
root = root.children[cIndex];
insert(root, word,start+1,end);
}
private static boolean isPresent(TrieNode root, String word){
return isPresent(root, word, 0, word.length());
}
private static boolean isPresent(TrieNode root, String word, int start, int end){
if(start == end){
return root != null && root.isEndOfWord;
}
int cIndex = charToIndex(word.charAt(start));
if(root.children[cIndex] == null){
return false;
}
root = root.children[cIndex];
return isPresent(root, word,start+1,end);
}
private static void deleteWord(TrieNode root, String word[]){
for(int i = 0; i < word.length; i++){
delete(root, word[i], 0, word[i].length());
}
}
private static boolean isFree(TrieNode root){
for (int i = 0; i < 26; i++)
{
if(root.children[i] != null){
return false;
}
}
return true;
}
private static boolean delete(TrieNode root, String word, int start, int len){
if(start == len){
if(root.isEndOfWord){
root.isEndOfWord = false;
if(isFree(root)){
return true;
}
}
return false;
}
int cIndex = charToIndex(word.charAt(start));
if(delete(root.children[cIndex],word,start+1,len)){
root.children[cIndex] = null;
return !root.isEndOfWord && isFree(root);
}
return false;
}
public static void main(String args[]) {
String word[] = {"apple","amazon","grapes"};
root = new TrieNode();
insert(root,word);
String wordsToBeDeleted[] = {"amazon","grapes"};
deleteWord(root, wordsToBeDeleted);
System.out.print(isPresent(root,"grapes")+" ");
}
}