class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEndOfWord = true; } search(word: string): boolean { let node = this.root; for (const char of word) { if (!node.children.has(char)) { return false; } node = node.children.get(char)!; } return node.isEndOfWord; } } const trie = new Trie(); trie.insert("apple"); trie.insert("app"); console.log(trie.search("app"));
The word 'app' was inserted into the Trie, so searching for 'app' returns true. The search method returns true only if the word exists and is marked as an end of a word.
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEndOfWord = true; } search(word: string): boolean { let node = this.root; for (const char of word) { if (!node.children.has(char)) { return false; } node = node.children.get(char)!; } return node.isEndOfWord; } } const trie = new Trie(); trie.insert("cat"); trie.insert("car"); console.log(trie.search("cap"));
The word 'cap' was not inserted. The search fails because the character 'p' is not found after 'ca', so the method returns false.
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEndOfWord = true; } search(word: string): boolean { let node = this.root; for (const char of word) { node = node.children.get(char)!; } return node.isEndOfWord; } } const trie = new Trie(); trie.insert("dog"); console.log(trie.search("don"));
The search method does not check if the character exists in the children map before accessing it. When searching for 'don', the character 'o' is found, but the next character 'n' is missing, so accessing undefined causes a TypeError.
class TrieNode { children: Map<string, TrieNode> = new Map(); isEndOfWord: boolean = false; } class Trie { root: TrieNode = new TrieNode(); insert(word: string): void { let node = this.root; for (const char of word) { if (!node.children.has(char)) { node.children.set(char, new TrieNode()); } node = node.children.get(char)!; } node.isEndOfWord = true; } countWords(node: TrieNode = this.root): number { let count = 0; if (node.isEndOfWord) count++; for (const child of node.children.values()) { count += this.countWords(child); } return count; } } const trie = new Trie(); trie.insert("bat"); trie.insert("bath"); trie.insert("batman"); trie.insert("bat"); trie.insert("batch"); console.log(trie.countWords());
The words inserted are 'bat', 'bath', 'batman', 'bat' (duplicate), and 'batch'. The duplicate 'bat' does not add to the count. So total unique words stored are 4.
Tries provide search time proportional to the length of the word (O(m)), not the number of words stored, making them efficient for prefix-based searches. They do not necessarily use less memory or automatically sort words, and search time can increase with word length.