-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autocompleter.java
228 lines (199 loc) · 6.92 KB
/
Autocompleter.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
package autocompleter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
public class Autocompleter implements AutocompleterInterface {
// -----------------------------------------------------------
// Fields
// -----------------------------------------------------------
TTNode root;
// -----------------------------------------------------------
// Constructor
// -----------------------------------------------------------
Autocompleter () {
root = null;
}
// -----------------------------------------------------------
// Methods
// -----------------------------------------------------------
public boolean isEmpty () {
return root == null;
}
public void addTerm (String toAdd) {
root = addTerm(root, toAdd, 0);
}
public boolean hasTerm (String query) {
return hasTerm(root, query);
}
public String getSuggestedTerm (String query) {
return getSuggestedTerm(root, query);
}
public ArrayList<String> getSortedTerms () {
ArrayList<String> answer = new ArrayList<String>();
return getSortedTerms(root, answer, "");
}
// -----------------------------------------------------------
// Helper Methods
// -----------------------------------------------------------
private String normalizeTerm (String s) {
// Edge case handling: empty Strings illegal
if (s == null || s.equals("")) {
throw new IllegalArgumentException();
}
return s.trim().toLowerCase();
}
/*
* Returns:
* int less than 0 if c1 is alphabetically less than c2
* 0 if c1 is equal to c2
* int greater than 0 if c1 is alphabetically greater than c2
*/
private int compareChars (char c1, char c2) {
return Character.toLowerCase(c1) - Character.toLowerCase(c2);
}
// [!] Add your own helper methods here!
private boolean hasTerm(TTNode current, String query) {
query = normalizeTerm(query);
for (int i = 0; i < query.length(); i++) {
if (current == null) { return false; }
// If the last letter of our query does not correspond to a word end
// in our tree, returns false.
if (!current.wordEnd && (i == query.length() - 1) &&
current.letter == query.charAt(query.length() - 1)) {
return false;
}
if (compareChars(query.charAt(i), current.letter ) == 0) {
current = current.mid;
} else if (compareChars(query.charAt(i), current.letter ) < 0) {
current = current.left;
i--;
} else {
current = current.right;
i--;
}
}
return true;
}
private TTNode addTerm(TTNode current, String query, int counter) {
query = normalizeTerm(query);
if (counter < query.length()) {
if (current == null) {
if (counter < query.length() - 1) {
current = new TTNode(query.charAt(counter), false);
counter++;
current.mid = addTerm(current.mid, query, counter);
} else {
current = new TTNode(query.charAt(counter), true);
counter++;
}
}
else {
if (compareChars(query.charAt(counter), current.letter) < 0) {
current.left = addTerm(current.left, query, counter);
} else if (compareChars(query.charAt(counter), current.letter) > 0){
current.right = addTerm(current.right, query, counter);
} else {
if (!current.wordEnd && counter >= query.length() - 1 ) {
current.wordEnd = true;
counter++;
}
counter++;
current.mid = addTerm(current.mid, query, counter);
}
}
}
return current;
}
/*
* Returns whether or not the tree contains the given query;
* returns true if the query exists in the tree, whether or not
* the last letter is a word end.
*/
private boolean hasFragment(TTNode current, String query) {
query = normalizeTerm(query);
for (int i = 0; i < query.length(); i++) {
if (current == null) { return false; }
if (compareChars(query.charAt(i), current.letter ) == 0) {
current = current.mid;
} else if (compareChars(query.charAt(i), current.letter ) < 0) {
current = current.left;
i--;
} else {
current = current.right;
i--;
}
}
return true;
}
/*
* Returns the node that corresponds to the last letter of the given query.
*/
private TTNode getLastLetterNode(TTNode current, String query) {
query = normalizeTerm(query);
for (int i = 0; i < query.length(); i++) {
if (i >= query.length() - 1 && current.letter == query.charAt(query.length() - 1)) {
return current;
}
if (compareChars(query.charAt(i), current.letter ) == 0) {
current = current.mid;
} else if (compareChars(query.charAt(i), current.letter ) < 0) {
current = current.left;
i--;
} else {
current = current.right;
i--;
}
}
return current;
}
private String getSuggestedTerm(TTNode current, String query) {
query = normalizeTerm(query);
if (hasTerm(query)) {
return query;
}
if (!hasFragment(current, query)) {
return null;
}
TTNode lastNode = getLastLetterNode(current, query);
TTNode nextNode = lastNode.mid;
if (!hasFragment(current, query)) {
return null;
} else {
query = query + String.valueOf(nextNode.letter);
return getSuggestedTerm(current, query);
}
}
private ArrayList<String> getSortedTerms (TTNode current, ArrayList<String> list, String word) {
if (current != null) {
getSortedTerms(current.left, list, word);
word += current.letter;
if (current.wordEnd) {
list.add(word);
}
getSortedTerms(current.mid, list, word);
word = word.substring(0, word.length() - 1);
getSortedTerms(current.right, list, word);
}
return list;
}
// -----------------------------------------------------------
// TTNode Internal Storage
// -----------------------------------------------------------
/*
* Internal storage of autocompleter search terms
* as represented using a Ternary Tree with TTNodes
*/
private class TTNode {
boolean wordEnd;
char letter;
TTNode left, mid, right;
TTNode (char c, boolean w) {
letter = c;
wordEnd = w;
left = null;
mid = null;
right = null;
}
}
}