|
| 1 | +/** |
| 2 | + * Represents a node in a Trie data structure. |
| 3 | + */ |
| 4 | +class TrieNode { |
| 5 | + /** |
| 6 | + * An object that stores child nodes for each character in the alphabet. |
| 7 | + */ |
| 8 | + children: { [key: string]: TrieNode } = {}; |
| 9 | + |
| 10 | + /** |
| 11 | + * Indicates whether the node represents the end of a word. |
| 12 | + */ |
| 13 | + isWord: boolean = false; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Trie Data structure for storing and searching words. |
| 18 | + */ |
| 19 | +export class Trie { |
| 20 | + /** |
| 21 | + * The root node of the Trie. |
| 22 | + */ |
| 23 | + root: TrieNode = new TrieNode(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a new Trie instance. |
| 27 | + */ |
| 28 | + constructor() {} |
| 29 | + |
| 30 | + /** |
| 31 | + * Inserts a word into the Trie. |
| 32 | + * |
| 33 | + * @param word - The word to insert into the Trie. |
| 34 | + */ |
| 35 | + private insertNode(node: TrieNode, word: string): void { |
| 36 | + for (const char of word) { |
| 37 | + if (!node.children[char]) { |
| 38 | + node.children[char] = new TrieNode(); |
| 39 | + } |
| 40 | + node = node.children[char]; |
| 41 | + } |
| 42 | + node.isWord = true; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Searches for a word in the Trie. |
| 47 | + * |
| 48 | + * @param word - The word to search for. |
| 49 | + * @param isPrefixMatch - Indicates whether to perform a prefix match (default: false). |
| 50 | + * If true, the method returns true if the Trie contains words with the specified prefix. |
| 51 | + * If false, the method returns true only if an exact match is found. |
| 52 | + * @returns True if the word (or prefix) is found in the Trie; otherwise, false. |
| 53 | + */ |
| 54 | + public find(word: string, isPrefixMatch: boolean = false): boolean { |
| 55 | + return this.searchNode(this.root, word, isPrefixMatch); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Adds a word to the Trie. |
| 60 | + * |
| 61 | + * @param word - The word to add to the Trie. |
| 62 | + * @returns The Trie instance, allowing for method chaining. |
| 63 | + */ |
| 64 | + public add(word: string): this { |
| 65 | + this.insertNode(this.root, word); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Searches for a word in the Trie. |
| 71 | + * |
| 72 | + * @param node - The current Trie node being examined. |
| 73 | + * @param word - The word to search for. |
| 74 | + * @param prefixMatch - Indicates whether to perform a prefix match. |
| 75 | + * @returns True if the word (or prefix) is found in the Trie; otherwise, false. |
| 76 | + * @private |
| 77 | + */ |
| 78 | + private searchNode( |
| 79 | + node: TrieNode, |
| 80 | + word: string, |
| 81 | + prefixMatch: boolean |
| 82 | + ): boolean { |
| 83 | + for (const char of word) { |
| 84 | + if (!node.children[char]) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + node = node.children[char]; |
| 88 | + } |
| 89 | + return prefixMatch || node.isWord; |
| 90 | + } |
| 91 | +} |
0 commit comments