What if you could find or add any word instantly, no matter how big your list is?
Why Trie Insert Operation in DSA Typescript?
Imagine you have a huge phone book written on paper. You want to find or add a new name quickly, but you have to flip through every page one by one.
It takes a lot of time and you might lose your place or make mistakes.
Manually searching or adding names in a big list is slow and tiring.
You can easily miss names or add duplicates because you have no quick way to check if a name exists.
It's like looking for a word in a dictionary without alphabetical order.
A Trie is like a smart tree that stores words letter by letter.
When you add a word, you follow the path of letters and create new branches only if needed.
This way, you quickly find or add words without checking the whole list.
let phoneBook = []; // To add a name phoneBook.push('Alice'); // To check if name exists phoneBook.includes('Alice');
class TrieNode { children: Map<string, TrieNode> = new Map(); isWordEnd: boolean = false; } class Trie { root = new TrieNode(); insert(word: string) { let current = this.root; for (const letter of word) { if (!current.children.has(letter)) { current.children.set(letter, new TrieNode()); } current = current.children.get(letter)!; } current.isWordEnd = true; } }
It lets you add and find words instantly, even in huge lists, by following letter paths.
Autocomplete in search engines uses Tries to quickly suggest words as you type each letter.
Manual searching is slow and error-prone for large word lists.
Trie stores words letter by letter, making insertions fast and organized.
It enables quick word lookup and autocomplete features.
Practice
insert operation in a Trie data structure?Solution
Step 1: Understand the insert operation
The insert operation adds each character of a word into the Trie one by one, creating new nodes if needed.Step 2: Differentiate from other operations
Deleting removes words, searching finds words, counting totals words. Insert specifically adds words.Final Answer:
To add a word character by character into the Trie -> Option AQuick Check:
Insert = Add word [OK]
- Confusing insert with search or delete
- Thinking insert counts words
- Assuming insert replaces words
root?Solution
Step 1: Understand variable declaration in TypeScript
Usingletdeclares a variable that can be reassigned, suitable for traversal nodes.Step 2: Check syntax correctness
let node = root; uses correct assignment withlet. node = root; misses declaration keyword. const node = root() wrongly calls root as a function. var node == root; uses invalid comparison operator.Final Answer:
let node = root; -> Option AQuick Check:
Correct variable declaration = let node = root; [OK]
- Missing let or const keyword
- Using == instead of = for assignment
- Calling node as a function
class TrieNode {
children: Map = new Map();
isEndOfWord = false;
}
const root = new TrieNode();
function insert(word: string) {
let node = 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;
}
insert("cat");
console.log(Array.from(root.children.keys()));Solution
Step 1: Trace insertion of "cat" into Trie
Starting at root, 'c' is not present, so a new node for 'c' is created. Then move to 'c' node, 'a' is added as child, then 't' as child of 'a'.Step 2: Check root's children keys
Root only has one child node for 'c'. The keys of root.children are ['c'].Final Answer:
["c"] -> Option DQuick Check:
Root children keys after insert = ["c"] [OK]
- Expecting all letters at root level
- Confusing keys with full words
- Ignoring Map keys method
function insert(word: string) {
let node = 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;
}Solution
Step 1: Check new object creation syntax
In TypeScript, when creating a new object, parentheses are required even if no arguments are passed.new TrieNodeshould benew TrieNode().Step 2: Verify other parts
Using 'let' for node is fine since node changes. The get method is safe here because we just set the key if missing. isEndOfWord is set at the end, initial value doesn't cause error.Final Answer:
Missing parentheses when creating new TrieNode -> Option BQuick Check:
new object needs () [OK]
- Omitting () after new keyword
- Confusing let and const for variables
- Ignoring possible undefined from get()
Solution
Step 1: Analyze common prefixes in words
Words "bat", "ball", and "batman" share prefix 'b' -> 'a'. From 'a', 't' leads to "bat" and "batman", 'l' leads to "ball".Step 2: Understand Trie branching
Node 'b' has child 'a'. Node 'a' branches to 't' and 'l'. 'm' is child of 'a'->'t'->'m' for "batman". So 'm' is not direct child of 'b'.Final Answer:
The node for 'b' has children 'a' only, and 'a' has children 't' and 'l' -> Option CQuick Check:
Trie branches at shared prefixes [OK]
- Assuming all words start new branches at root
- Thinking 'm' is direct child of 'b'
- Confusing end of word nodes
Ready to prove it?
Pass the test and this pattern moves unknown
