From af80c33e21cc3089d8ab333364ad4ad6a8bbd4be Mon Sep 17 00:00:00 2001 From: brendon <36212954+brendonmiranda@users.noreply.github.com> Date: Mon, 9 Sep 2024 12:25:18 +0100 Subject: [PATCH] comments --- src/main/java/data/structure/tree/TrieTree.java | 9 +++++---- src/test/java/data/structure/TrieTreeTest.java | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/data/structure/tree/TrieTree.java b/src/main/java/data/structure/tree/TrieTree.java index d4eec17..e9c0c3b 100644 --- a/src/main/java/data/structure/tree/TrieTree.java +++ b/src/main/java/data/structure/tree/TrieTree.java @@ -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 } @@ -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; @@ -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 } diff --git a/src/test/java/data/structure/TrieTreeTest.java b/src/test/java/data/structure/TrieTreeTest.java index acfee94..bb69c3e 100644 --- a/src/test/java/data/structure/TrieTreeTest.java +++ b/src/test/java/data/structure/TrieTreeTest.java @@ -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"));