14
14
* Some resources:
15
15
* <a href="https://en.wikipedia.org/wiki/Trie">Trie Data Structure</a>
16
16
* <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>
17
18
*
18
19
* @author rampatra
19
20
* @since 9/22/15
20
21
*/
21
22
public class Trie <E > {
22
23
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 ;
24
34
25
35
Trie () {
26
- root = new TrieNode <>(null , new HashMap <>());
36
+ root = new TrieNode <>(new HashMap <>());
27
37
}
28
38
29
39
/**
30
- * Inserts {@param data} in trie.
40
+ * Inserts {@code data} in trie.
31
41
*
32
42
* @param data
33
43
*/
34
44
public void insert (E data ) {
35
45
36
46
int i = 0 ;
37
47
String str = data .toString ();
38
- TrieNode <E > curr = root ;
48
+ TrieNode <Character > curr = root ;
39
49
40
50
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 ));
43
53
i ++;
44
54
} else {
45
55
break ;
46
56
}
47
57
}
48
58
49
59
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 ));
52
62
i ++;
53
63
}
54
64
55
- curr .data = data ;
65
+ curr .isCompleteWord = true ;
56
66
}
57
67
58
68
/**
@@ -63,29 +73,17 @@ public void insert(E data) {
63
73
*/
64
74
public boolean search (E data ) {
65
75
66
- int i = 0 ;
67
76
String str = data .toString ();
68
- TrieNode <E > curr = root ;
77
+ TrieNode <Character > curr = root ;
69
78
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 ) {
72
81
return false ;
73
82
}
74
- curr = curr .children .get (str .substring (i , i + 1 ));
75
- i ++;
83
+ curr = curr .children .get (str .charAt (i ));
76
84
}
77
85
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 ;
89
87
}
90
88
91
89
// unit testing
@@ -101,4 +99,4 @@ public static void main(String[] args) {
101
99
System .out .println (trie .search ("raz" ));
102
100
System .out .println (trie .search ("rampatra" ));
103
101
}
104
- }
102
+ }
0 commit comments