Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Sep 9, 2024
1 parent 128c509 commit af80c33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/main/java/data/structure/tree/TrieTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Trie {

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

public boolean leaf;
public boolean leaf; // a node is called a leaf if it has no children

}

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

if (child.child[index] == null)
child.child[index] = new Trie();
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

child = child.child[index];
child = child.child[index]; // positioning the tree in this index
}

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

return root.leaf;
// return root.leaf;
return true; // make it possible to search for part of the keys
}


Expand Down
7 changes: 6 additions & 1 deletion src/test/java/data/structure/TrieTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public void test() {

Assertions.assertTrue(TrieTree.search(root, "foo"));
Assertions.assertTrue(TrieTree.search(root, "bar"));
Assertions.assertFalse(TrieTree.search(root, "fo"));

Assertions.assertTrue(TrieTree.search(root, "fo"));
Assertions.assertTrue(TrieTree.search(root, "ba"));

Assertions.assertFalse(TrieTree.search(root, "fb"));

Assertions.assertFalse(TrieTree.search(root, "fooo"));
Assertions.assertFalse(TrieTree.search(root, "barr"));

Expand Down

0 comments on commit af80c33

Please sign in to comment.