|
| 1 | +package com.thealgorithms.datastructures.tries; |
| 2 | + |
| 3 | +/** |
| 4 | + * A Trie (prefix tree) data structure implementation for storing strings. |
| 5 | + * |
| 6 | + * <p> |
| 7 | + * The Trie allows for efficient insertion, search, and prefix-matching |
| 8 | + * operations. |
| 9 | + * It is commonly used for tasks such as autocomplete, spell checking, and |
| 10 | + * dictionary storage. |
| 11 | + * |
| 12 | + * <p> |
| 13 | + * This implementation supports lowercase English letters ('a'–'z'). |
| 14 | + * Each node stores an array of 26 children references for each alphabet letter. |
| 15 | + * |
| 16 | + * <p> |
| 17 | + * Trie is not thread-safe and should not be accessed by multiple threads |
| 18 | + * concurrently. |
| 19 | + */ |
| 20 | +public class Trie { |
| 21 | + |
| 22 | + /** The root node of the Trie. */ |
| 23 | + private final TrieNode root; |
| 24 | + |
| 25 | + /** Constructs an empty Trie with an empty root node. */ |
| 26 | + public Trie() { |
| 27 | + root = new TrieNode(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Inserts a word into the Trie. |
| 32 | + * |
| 33 | + * @param word the word to insert; must not be {@code null} or empty |
| 34 | + */ |
| 35 | + public void insert(String word) { |
| 36 | + TrieNode currentNode = root; |
| 37 | + for (char ch : word.toCharArray()) { |
| 38 | + int index = ch - 'a'; |
| 39 | + if (currentNode.children[index] == null) { |
| 40 | + currentNode.children[index] = new TrieNode(); |
| 41 | + } |
| 42 | + currentNode = currentNode.children[index]; |
| 43 | + } |
| 44 | + currentNode.isEndOfWord = true; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Checks whether the Trie contains a specific word. |
| 49 | + * |
| 50 | + * @param word the word to check for; must not be {@code null} |
| 51 | + * @return {@code true} if the Trie contains the specified word; {@code false} otherwise |
| 52 | + */ |
| 53 | + public boolean search(String word) { |
| 54 | + TrieNode currentNode = root; |
| 55 | + for (char ch : word.toCharArray()) { |
| 56 | + int index = ch - 'a'; |
| 57 | + if (currentNode.children[index] == null) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + currentNode = currentNode.children[index]; |
| 61 | + } |
| 62 | + return currentNode.isEndOfWord; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Checks whether there is any word in the Trie that starts with the given prefix. |
| 67 | + * |
| 68 | + * @param prefix the prefix to check for; must not be {@code null} |
| 69 | + * @return {@code true} if there is at least one word starting with the prefix; |
| 70 | + * {@code false} otherwise |
| 71 | + */ |
| 72 | + public boolean startsWith(String prefix) { |
| 73 | + TrieNode currentNode = root; |
| 74 | + for (char ch : prefix.toCharArray()) { |
| 75 | + int index = ch - 'a'; |
| 76 | + if (currentNode.children[index] == null) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + currentNode = currentNode.children[index]; |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Represents a single node in the Trie. |
| 86 | + */ |
| 87 | + private static class TrieNode { |
| 88 | + private final TrieNode[] children; |
| 89 | + private boolean isEndOfWord; |
| 90 | + |
| 91 | + /** Constructs an empty TrieNode with 26 possible children. */ |
| 92 | + TrieNode() { |
| 93 | + children = new TrieNode[26]; |
| 94 | + isEndOfWord = false; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments