Trie Insert Operation in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to add a word to a trie grows as the word gets longer.
How does the length of the word affect the work done during insertion?
Analyze the time complexity of the following code snippet.
class TrieNode {
constructor() {
this.children = new Map();
this.isEnd = false;
}
}
function insert(root, word) {
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.isEnd = true;
}
This code adds each character of a word into the trie, creating new nodes if needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the word.
- How many times: Once for every character in the word (word length).
As the word gets longer, the number of steps grows directly with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows in a straight line with the word length.
Time Complexity: O(n)
This means the time to insert grows directly with the length of the word.
[X] Wrong: "Insertion time depends on the number of words already in the trie."
[OK] Correct: Insertion only depends on the length of the word being added, not how many words are stored.
Knowing how trie insertion scales helps you explain efficient word storage and search in many applications.
"What if we changed the trie to store only lowercase letters in an array instead of a map? How would the time complexity change?"
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 nodes if they don't exist.Step 2: Compare with other operations
Deleting removes words, searching finds words, and counting totals words, but insert specifically adds words.Final Answer:
To add a new word character by character into the Trie -> Option DQuick Check:
Insert = Add word step-by-step [OK]
- Confusing insert with search or delete
- Thinking insert adds whole word as one node
- Assuming insert counts words
"cat" into an empty Trie in JavaScript?Solution
Step 1: Analyze the loop for insertion
for (let char of word) { if (!node.children[char]) node.children[char] = new TrieNode(); node = node.children[char]; } loops over each character, checks if child node exists, creates if not, then moves to that child node.Step 2: Check other options for correctness
for (let i = 0; i < word.length; i++) { node = node.children[word[i]]; } does not create nodes if missing. while (word) { node.children[word[0]] = new TrieNode(); word = word.slice(1); } modifies the word string but does not move node properly. node.children[word] = new TrieNode(); tries to add whole word as one child.Final Answer:
for (let char of word) { if (!node.children[char]) node.children[char] = new TrieNode(); node = node.children[char]; } -> Option AQuick Check:
Loop chars, create missing nodes, move node [OK]
- Not creating nodes when missing
- Assigning whole word as one child
- Not updating current node pointer
["bat", "ball"] in order? Consider each node stores children in an object and a boolean isEnd for word end.Solution
Step 1: Insert "bat" into empty Trie
Creates nodes b -> a -> t with t's isEnd true.Step 2: Insert "ball" next
Nodes b -> a exist, then add l -> l with last l's isEnd true.Final Answer:
{"b":{"a":{"t":{"isEnd":true},"l":{"l":{"isEnd":true}}}}} -> Option CQuick Check:
Both words share prefix 'ba', separate branches for 't' and 'll' [OK]
- Marking wrong nodes as word end
- Not creating second 'l' node for 'ball'
- Overwriting nodes incorrectly
insert(word) {
let node = this.root;
for (let char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children;
}
node.isEnd = true;
}Solution
Step 1: Check node update inside loop
Inside loop, node is set to node.children (object), not node.children[char] (child node), so traversal breaks.Step 2: Confirm correct pointer update
Correct is node = node.children[char] to move down the Trie properly.Final Answer:
node is assigned to node.children instead of node.children[char] -> Option BQuick Check:
Update node to child node, not children object [OK]
- Assigning node to children object instead of child node
- Setting isEnd too early
- Ignoring empty input check
["apple", "app", "ape"] into a Trie and then check if the word "app" is a complete word in the Trie. Which approach correctly handles this scenario?Solution
Step 1: Insert words with isEnd marking
Each word is inserted letter by letter; isEnd is set true at last letter node to mark complete words.Step 2: Search for "app" with isEnd check
Search traverses nodes for 'a'->'p'->'p' and checks isEnd true to confirm "app" is a complete word, not just prefix.Final Answer:
Insert each word character by character, mark isEnd true at last character; then search for "app" checking isEnd at last node -> Option AQuick Check:
Mark word ends, check isEnd to confirm full word [OK]
- Not marking isEnd for shorter words
- Treating prefixes as complete words without isEnd
- Storing whole words as single nodes
Ready to prove it?
Pass the test and this pattern moves unknown
