Skip to content

Commit c41d117

Browse files
committed
Trie code improved
1 parent a30ac9e commit c41d117

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/main/java/com/rampatra/base/Trie.java

+25-27
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,55 @@
1414
* Some resources:
1515
* <a href="https://en.wikipedia.org/wiki/Trie">Trie Data Structure</a>
1616
* <a href="https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries">More about Tries</a>
17+
* <a href="https://www.youtube.com/watch?v=zIjfhVPRZCg">Video explanation from Gayle McDowell</a>
1718
*
1819
* @author rampatra
1920
* @since 9/22/15
2021
*/
2122
public class Trie<E> {
2223

23-
TrieNode<E> root;
24+
private class TrieNode<T> {
25+
HashMap<T, TrieNode<T>> children;
26+
boolean isCompleteWord; // to mark a complete word in the tri data structure
27+
28+
TrieNode(HashMap<T, TrieNode<T>> children) {
29+
this.children = children;
30+
}
31+
}
32+
33+
private TrieNode<Character> root;
2434

2535
Trie() {
26-
root = new TrieNode<>(null, new HashMap<>());
36+
root = new TrieNode<>(new HashMap<>());
2737
}
2838

2939
/**
30-
* Inserts {@param data} in trie.
40+
* Inserts {@code data} in trie.
3141
*
3242
* @param data
3343
*/
3444
public void insert(E data) {
3545

3646
int i = 0;
3747
String str = data.toString();
38-
TrieNode<E> curr = root;
48+
TrieNode<Character> curr = root;
3949

4050
while (i < str.length()) {
41-
if (curr.children.get(str.substring(i, i + 1)) != null) {
42-
curr = curr.children.get(str.substring(i, i + 1));
51+
if (curr.children.get(str.charAt(i)) != null) {
52+
curr = curr.children.get(str.charAt(i));
4353
i++;
4454
} else {
4555
break;
4656
}
4757
}
4858

4959
while (i < str.length()) {
50-
curr.children.put((E) str.substring(i, i + 1), new TrieNode<>(null, new HashMap<>()));
51-
curr = curr.children.get(str.substring(i, i + 1));
60+
curr.children.put(str.charAt(i), new TrieNode<>(new HashMap<>()));
61+
curr = curr.children.get(str.charAt(i));
5262
i++;
5363
}
5464

55-
curr.data = data;
65+
curr.isCompleteWord = true;
5666
}
5767

5868
/**
@@ -63,29 +73,17 @@ public void insert(E data) {
6373
*/
6474
public boolean search(E data) {
6575

66-
int i = 0;
6776
String str = data.toString();
68-
TrieNode<E> curr = root;
77+
TrieNode<Character> curr = root;
6978

70-
while (i < str.length()) {
71-
if (curr.children.get(str.substring(i, i + 1)) == null) {
79+
for (int i = 0; i < str.length(); i++) {
80+
if (curr.children.get(str.charAt(i)) == null) {
7281
return false;
7382
}
74-
curr = curr.children.get(str.substring(i, i + 1));
75-
i++;
83+
curr = curr.children.get(str.charAt(i));
7684
}
7785

78-
return curr.data != null && curr.data.equals(data);
79-
}
80-
81-
private class TrieNode<T> {
82-
T data; // stores the complete string (required to determine whether the string is in the trie)
83-
HashMap<T, TrieNode<T>> children;
84-
85-
TrieNode(T data, HashMap<T, TrieNode<T>> children) {
86-
this.data = data;
87-
this.children = children;
88-
}
86+
return curr.isCompleteWord;
8987
}
9088

9189
// unit testing
@@ -101,4 +99,4 @@ public static void main(String[] args) {
10199
System.out.println(trie.search("raz"));
102100
System.out.println(trie.search("rampatra"));
103101
}
104-
}
102+
}

0 commit comments

Comments
 (0)