diff --git a/src/main/java/com/thealgorithms/datastructures/tries/Trie.java b/src/main/java/com/thealgorithms/datastructures/tries/Trie.java new file mode 100644 index 000000000000..b6d5ed6afeb4 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/tries/Trie.java @@ -0,0 +1,83 @@ +package com.thealgorithms.datastructures.tries; + +/** + * Trie class which holds Strings of characters. + * + * Wikipedia + * + * @author Sailok Chinta + */ +public class Trie { + private static final char ROOT_CHAR = '*'; + + private final TrieNode root; + + public Trie() { + this.root = new TrieNode(ROOT_CHAR); + } + + /** + * Inserts the word into Trie + * + * @param word + */ + public void insert(String word) { + TrieNode head = root; + + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + + if (!head.getChildren().containsKey(c)) { + head.getChildren().put(c, new TrieNode(c)); + } + + head = head.getChildren().get(c); + } + + head.setEnd(true); + } + + /** + * Searches in the Trie if a word exists or not. + * + * @param word + * @return true / false + */ + public boolean search(String word) { + TrieNode head = root; + + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + + if (!head.getChildren().containsKey(c)) { + return false; + } + + head = head.getChildren().get(c); + } + + return head.isEnd(); + } + + /** + * Searches in the Trie if a prefix exists or not. + * + * @param prefix + * @return true / false + */ + public boolean startsWith(String prefix) { + TrieNode head = root; + + for (int i = 0; i < prefix.length(); i++) { + char c = prefix.charAt(i); + + if (!head.getChildren().containsKey(c)) { + return false; + } + + head = head.getChildren().get(c); + } + + return true; + } +} diff --git a/src/main/java/com/thealgorithms/datastructures/tries/TrieNode.java b/src/main/java/com/thealgorithms/datastructures/tries/TrieNode.java new file mode 100644 index 000000000000..0d8ee1f8ead1 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/tries/TrieNode.java @@ -0,0 +1,49 @@ +package com.thealgorithms.datastructures.tries; + +import java.util.HashMap; +import java.util.Map; + +/** + * TrieNode class which holds the characters and references to its child nodes + * + * Wikipedia + * + * @author Sailok Chinta + */ +public class TrieNode { + private Map children; + private char value; + private boolean end; + + public TrieNode(char value) { + this.value = value; + this.children = new HashMap<>(); + this.end = false; + } + + public Map getChildren() { + return children; + } + + /* + public void setChildren(Map children) { + this.children = children; + } + */ + + public char getValue() { + return value; + } + + public void setValue(char value) { + this.value = value; + } + + public boolean isEnd() { + return end; + } + + public void setEnd(boolean end) { + this.end = end; + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/tries/TrieTest.java b/src/test/java/com/thealgorithms/datastructures/tries/TrieTest.java new file mode 100644 index 000000000000..27e530330987 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/tries/TrieTest.java @@ -0,0 +1,36 @@ +package com.thealgorithms.datastructures.tries; + +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TrieTest { + private static final List WORDS = List.of("apple", "app", "rest", "rent", "rental"); + + @Test + void searchInTrieSuccess() { + Trie trie = new Trie(); + + for (String word : WORDS) { + trie.insert(word); + } + + Assertions.assertTrue(trie.search("app")); + Assertions.assertTrue(trie.search("apple")); + Assertions.assertFalse(trie.search("apply")); + } + + @Test + void startsWithPrefixInTrieSuccess() { + Trie trie = new Trie(); + + for (String word : WORDS) { + trie.insert(word); + } + + Assertions.assertTrue(trie.startsWith("app")); + Assertions.assertTrue(trie.startsWith("re")); + Assertions.assertTrue(trie.startsWith("rent")); + Assertions.assertFalse(trie.startsWith("bike")); + } +}