Skip to content

Commit af80c33

Browse files
comments
1 parent 128c509 commit af80c33

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/main/java/data/structure/tree/TrieTree.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class Trie {
1111

1212
public Trie[] child = new Trie[26]; // 26 = alphabet size
1313

14-
public boolean leaf;
14+
public boolean leaf; // a node is called a leaf if it has no children
1515

1616
}
1717

@@ -30,9 +30,9 @@ public static void insert(Trie root, String key) {
3030
int index = key.toUpperCase().charAt(i) - 'A';
3131

3232
if (child.child[index] == null)
33-
child.child[index] = new Trie();
33+
child.child[index] = new Trie(); // this is the actual insert of the character (or key) in the tree. In other words it inserts a new Trie in the index. The index not being null means a character in the tree. e.g. 5 = f, 14=o
3434

35-
child = child.child[index];
35+
child = child.child[index]; // positioning the tree in this index
3636
}
3737

3838
child.leaf = true;
@@ -58,7 +58,8 @@ public static boolean search(Trie root, String key) {
5858
root = root.child[k];
5959
}
6060

61-
return root.leaf;
61+
// return root.leaf;
62+
return true; // make it possible to search for part of the keys
6263
}
6364

6465

src/test/java/data/structure/TrieTreeTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ public void test() {
1919

2020
Assertions.assertTrue(TrieTree.search(root, "foo"));
2121
Assertions.assertTrue(TrieTree.search(root, "bar"));
22-
Assertions.assertFalse(TrieTree.search(root, "fo"));
22+
23+
Assertions.assertTrue(TrieTree.search(root, "fo"));
24+
Assertions.assertTrue(TrieTree.search(root, "ba"));
25+
26+
Assertions.assertFalse(TrieTree.search(root, "fb"));
27+
2328
Assertions.assertFalse(TrieTree.search(root, "fooo"));
2429
Assertions.assertFalse(TrieTree.search(root, "barr"));
2530

0 commit comments

Comments
 (0)