Trie Insert Operation in DSA Java - Time & Space Complexity
We want to understand how long it takes to add a word into a trie data structure.
The question is: how does the time grow as the word gets longer?
Analyze the time complexity of the following code snippet.
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
current.children.putIfAbsent(ch, new TrieNode());
current = current.children.get(ch);
}
current.isEndOfWord = true;
}
This code adds each character of the word into the trie, creating nodes if needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the word.
- How many times: Exactly once per character, so as many times as the word's length.
As the word gets longer, the number of steps grows directly with its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The time 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: "Inserting a word takes constant time because we just add one word."
[OK] Correct: Each character must be processed, so longer words take more time.
Knowing how trie insertion scales helps you explain efficient word storage and search in interviews.
"What if we changed the data structure to use an array of fixed size for children 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, creating nodes if needed.Step 2: Identify the purpose of insert
It builds the Trie structure by adding new words character by character.Final Answer:
To add a new word character by character into the Trie -> Option CQuick Check:
Insert = Add word stepwise [OK]
- Confusing insert with search or delete
- Thinking insert counts words
- Assuming insert removes words
"cat" into a Trie in Java?Solution
Step 1: Check loop boundaries for string indexing
Java strings are zero-indexed, so loop should start at 0 and go to length-1.Step 2: Choose correct loop condition
The loop must run whilei < word.length()to cover all characters.Final Answer:
for (int i = 0; i < word.length(); i++) -> Option AQuick Check:
Loop from 0 to length-1 [OK]
- Using <= causes index out of bounds
- Starting loop at 1 skips first character
- Using wrong loop limits
"bat" and "ball" in order? Assume each node stores a character and a boolean isEnd to mark word end.Solution
Step 1: Insert "bat" into Trie
Insert nodes for 'b' -> 'a' -> 't' and mark 't' node as word end.Step 2: Insert "ball" sharing prefix
Reuse 'b' -> 'a', then add 'l' -> 'l' nodes and mark last 'l' as word end.Final Answer:
"b" -> "a" -> "t" (isEnd=true), "a" -> "l" -> "l" (isEnd=true) -> Option DQuick Check:
Shared prefix 'ba', separate endings [OK]
- Not marking correct end nodes
- Creating separate branches for shared prefix
- Confusing node characters and word ends
void insert(String word) {
TrieNode node = root;
for (int i = 0; i <= word.length(); i++) {
char ch = word.charAt(i);
if (!node.children.containsKey(ch)) {
node.children.put(ch, new TrieNode());
}
node = node.children.get(ch);
}
node.isEnd = true;
}Solution
Step 1: Analyze loop boundary
The loop uses i <= word.length(), which causes index out of bounds on charAt.Step 2: Correct loop condition
It should be i < word.length() to avoid accessing invalid index.Final Answer:
Loop condition should be i < word.length(), not i <= word.length() -> Option BQuick Check:
Index error fixed by i < length [OK]
- Using <= causes runtime error
- Setting isEnd inside loop
- Checking children incorrectly
"apple", "app", and "ape" into a Trie. After insertion, which of the following statements is TRUE about the Trie structure?Solution
Step 1: Insert "apple" and "app" sharing prefix
Both share 'a' -> 'p' -> 'p'. The second 'p' node marks end for "app".Step 2: Insert "ape" branching at second character
From 'a', branch to 'p' then 'e' node marked as end for "ape".Step 3: Check isEnd flags
The second 'p' node after 'a' has isEnd true for "app"; 'e' node after 'p' has isEnd true for "ape"; final 'e' node for "apple" has isEnd true.Final Answer:
The node for the second 'p' after 'a' will have isEnd = true because of "app" -> Option AQuick Check:
Shared prefix nodes mark word ends correctly [OK]
- Assuming branch nodes can't be word ends
- Thinking partial words are not marked
- Marking all nodes as word ends
Ready to prove it?
Pass the test and this pattern moves unknown
