File tree Expand file tree Collapse file tree 2 files changed +17
-13
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +17
-13
lines changed Original file line number Diff line number Diff line change 3
3
public class _208 {
4
4
public static class Solution1 {
5
5
public static class TrieNode {
6
-
7
- char val ;
8
6
boolean isWord ;
9
7
TrieNode [] children = new TrieNode [26 ];
10
-
11
- // Initialize your data structure here.
12
- public TrieNode () {
13
- }
14
-
15
- public TrieNode (char c ) {
16
- this .val = c ;
17
- }
18
8
}
19
9
20
10
public static class Trie {
21
11
private TrieNode root ;
22
12
23
13
public Trie () {
24
- root = new TrieNode ();
25
- root .val = ' ' ;//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well
14
+ root = new TrieNode ();//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well
26
15
}
27
16
28
17
// Inserts a word into the trie.
29
18
public void insert (String word ) {
30
19
TrieNode node = root ;
31
20
for (int i = 0 ; i < word .length (); i ++) {
32
21
if (node .children [word .charAt (i ) - 'a' ] == null ) {
33
- node .children [word .charAt (i ) - 'a' ] = new TrieNode (word . charAt ( i ) );
22
+ node .children [word .charAt (i ) - 'a' ] = new TrieNode ();
34
23
}
35
24
node = node .children [word .charAt (i ) - 'a' ];
36
25
}
Original file line number Diff line number Diff line change @@ -19,4 +19,19 @@ public void test1() {
19
19
assertEquals (true , trie .search ("app" ));
20
20
}
21
21
22
+ @ Test
23
+ public void test2 () {
24
+ trie = new _208 .Solution1 .Trie ();
25
+ trie .insert ("fisher" );
26
+ trie .insert ("coder" );
27
+ trie .insert ("apple" );
28
+ trie .insert ("april" );
29
+ trie .insert ("cad" );
30
+ assertEquals (true , trie .search ("fisher" ));
31
+ assertEquals (true , trie .search ("apple" ));
32
+ assertEquals (true , trie .search ("coder" ));
33
+ assertEquals (true , trie .search ("april" ));
34
+ assertEquals (true , trie .search ("cad" ));
35
+ }
36
+
22
37
}
You can’t perform that action at this time.
0 commit comments