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 33public class _208 {
44 public static class Solution1 {
55 public static class TrieNode {
6-
7- char val ;
86 boolean isWord ;
97 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- }
188 }
199
2010 public static class Trie {
2111 private TrieNode root ;
2212
2313 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
2615 }
2716
2817 // Inserts a word into the trie.
2918 public void insert (String word ) {
3019 TrieNode node = root ;
3120 for (int i = 0 ; i < word .length (); i ++) {
3221 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 ();
3423 }
3524 node = node .children [word .charAt (i ) - 'a' ];
3625 }
Original file line number Diff line number Diff line change @@ -19,4 +19,19 @@ public void test1() {
1919 assertEquals (true , trie .search ("app" ));
2020 }
2121
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+
2237}
You can’t perform that action at this time.
0 commit comments