Skip to content

Commit 3b5b887

Browse files
refactor 208
1 parent 5849210 commit 3b5b887

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/main/java/com/fishercoder/solutions/_208.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,23 @@
33
public 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
}

src/test/java/com/fishercoder/_208Test.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)