-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTrie.java
165 lines (149 loc) · 3.65 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
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
package ds;
import java.util.HashMap;
class Trie
{
//total number of words inserted
private int numberOfWords;
private class TrieNode
{
private HashMap<Character, TrieNode> character;
boolean isWordComplete;
private int numberOfWords;
TrieNode()
{
character = new HashMap<Character, TrieNode>();
isWordComplete = false;
numberOfWords=0;
}
}
private final TrieNode root;
public Trie()
{
root = new TrieNode();
numberOfWords = 0;
}
// total number of words inserted
public int getTotalNumberOfWords()
{
return numberOfWords;
}
// insert a word in TRie
public void insertWord(String word)
{
TrieNode current = root;
current.numberOfWords++;
int index, size = word.length();
Character letter;
for (index = 0; index < size; index++)
{
letter = word.charAt(index);
if (!current.character.containsKey(letter))
{
current.character.put(letter, new TrieNode());
}
current = current.character.get(letter);
current.numberOfWords++;
}
current.isWordComplete = true;
numberOfWords++;
}
private TrieNode isWordPresent(String word)
{
TrieNode current = root;
int index, size;
size = word.length();
Character letter;
for (index = 0; index < size; index++)
{
letter = word.charAt(index);
if (current.character.containsKey(letter))
{
current = current.character.get(letter);
} else
return null;// word not found
}
return current;// word found and last character node returned
}
//check whetehr the compelete word is present or not
public boolean isCompleteWordPresent(String word)
{
TrieNode lastNode = isWordPresent(word);
if (lastNode == null)// word not present
return false;
else
return lastNode.isWordComplete;// returns whether it is a
// complete word or not
}
//returns the number of word with same prefix as the word
public int numberOfWordWithPrefix(String word)
{
TrieNode last=isWordPresent(word);
if(last==null)
return 0;
else
return last.numberOfWords;
}
//checks whether a word with prefix as the word is present or not
public boolean isPrefixWordPresent(String word)
{
TrieNode lastNode = isWordPresent(word);
if (lastNode == null)
return false;
else
return true;
}
//delete a word from the trie structure
public void delete(String word)
{
if(!isCompleteWordPresent(word))
throw new IllegalStateException("Word Not Present");
deleteWord(word, word.length(), 0, root);
numberOfWords--;
}
private boolean deleteWord(String word, int size, int index, TrieNode current)
{
if (index == size)
{
current.isWordComplete=false;
return current.character.size() == 0;
}
else
{
boolean deletable = deleteWord(word, size, index + 1, current.character.get(word.charAt(index)));
if (deletable && !current.isWordComplete)
{
current.character.remove(word.charAt(index));
return current.character.size() == 0;
}
else
return false;
}
}
public String getlongestPrefix(String word)
{
/*
* returns the longest prefix of the string which is also a word in dictionary.
* if no prefix then returns null
*/
TrieNode current=root;
int endIndex=0;
int size=word.length();
char letter;
for(int index=0;index<size;index++)
{
letter=word.charAt(index);
if(current.character.containsKey(letter))
{
if(current.isWordComplete)
endIndex=index;
current=current.character.get(letter);
}
else
break;
}
if(endIndex==0)//no prefix found
return null;
else
return word.substring(0, endIndex);
}
}