Skip to content

Commit

Permalink
Update trie example
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasmusson committed Jul 20, 2022
1 parent f371a0e commit cc1de0a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/algorithms/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void insert(String word) {
}

// Returns if the word is in the trie.
public boolean contains(String word) {
public boolean containsWord(String word) {
TrieNode t = containsNode(word);

if(t != null && t.isWholeWord)
Expand Down
39 changes: 21 additions & 18 deletions src/test/java/algorithms/TrieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ public class TrieTest {
public void SetUp() {
trie = new Trie();
}

@Test
public void SearchPrefix() {
trie.insert("tries");
trie.insert("trie");
trie.insert("tried");
trie.insert("try");

Assert.assertTrue(trie.contains("try"));
Assert.assertTrue(trie.startsWith("tr"));

trie.insert("Canada");
Assert.assertTrue(trie.startsWith("C"));
Assert.assertTrue(trie.startsWith("Ca"));
Assert.assertTrue(trie.startsWith("Can"));
Assert.assertTrue(trie.startsWith("Cana"));
Assert.assertTrue(trie.startsWith("Canad"));
Assert.assertTrue(trie.startsWith("Canada"));
Assert.assertTrue(trie.contains("Canada"));
public void StartsWith() {
trie.insert("Apple");
Assert.assertTrue(trie.startsWith("A"));
Assert.assertTrue(trie.startsWith("Ap"));
Assert.assertTrue(trie.startsWith("App"));
Assert.assertTrue(trie.startsWith("Appl"));
Assert.assertTrue(trie.startsWith("Apple"));
}

@Test
public void ContainsWord() {
trie.insert("pot");
trie.insert("past");
trie.insert("pass");
trie.insert("part");

Assert.assertTrue(trie.containsWord("pot"));
Assert.assertTrue(trie.containsWord("past"));
Assert.assertTrue(trie.containsWord("pass"));
Assert.assertTrue(trie.containsWord("part"));
Assert.assertFalse(trie.containsWord("ps"));
}
}
Binary file added target/classes/META-INF/algorithms.kotlin_module
Binary file not shown.
Binary file not shown.

0 comments on commit cc1de0a

Please sign in to comment.