Trie Insert Operation in DSA C# - Time & Space Complexity
We want to understand how the time to add a word to a trie grows as the word gets longer.
How does the number of steps change when the word length changes?
Analyze the time complexity of the following code snippet.
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c)) {
node.Children[c] = new TrieNode();
}
node = node.Children[c];
}
node.IsWord = true;
}
This code adds each character of the 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 input word.
- How many times: Exactly once per character, so as many times as the word length.
Each character in the word causes a step in the loop, so the steps grow directly with word length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The number of steps 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 it once."
[OK] Correct: Each character must be checked or added, so longer words take more steps.
Understanding this helps you explain how tries efficiently handle many words, especially when words share prefixes.
"What if we changed the trie to store only lowercase letters? How would that affect the time complexity of insert?"
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, counting totals words. Insert specifically adds words.Final Answer:
To add a word character by character into the Trie -> Option DQuick Check:
Insert = Add word step-by-step [OK]
- Confusing Insert with Delete or Search
- Thinking Insert counts words
- Assuming Insert removes nodes
Solution
Step 1: Identify correct data structure for children
Trie nodes use a dictionary mapping characters to child nodes for flexible branching.Step 2: Check class members
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } uses Dictionary and a boolean flag for word end, which is standard.Final Answer:
public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } -> Option AQuick Check:
Children dictionary + end flag = public class TrieNode { public Dictionary Children = new Dictionary(); public bool IsEndOfWord = false; } [OK]
- Using List<int> instead of dictionary for children
- Using int array without node references
- Using string instead of node references
Trie trie = new Trie();
trie.Insert("cat");
var childrenKeys = trie.Root.Children.Keys.ToList();
childrenKeys.Sort();
Console.WriteLine(string.Join(",", childrenKeys));Solution
Step 1: Insert "cat" character by character
First character 'c' is added as a child of root node.Step 2: Check root's children keys
Only 'c' is directly under root; 'a' and 't' are children of subsequent nodes.Final Answer:
"c" -> Option CQuick Check:
Root children keys = first letter only [OK]
- Assuming all letters appear at root children
- Confusing nested children with root children
- Printing full word as a key
public void Insert(string word) {
var node = Root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c)) {
node.Children[c] = new TrieNode();
}
node = node.Children[c];
}
node.IsEndOfWord = false;
}Solution
Step 1: Analyze IsEndOfWord assignment
After inserting all characters, IsEndOfWord marks the end of a valid word and must be true.Step 2: Identify the bug
Code sets IsEndOfWord to false, which incorrectly marks the word as not complete.Final Answer:
The IsEndOfWord flag should be set to true after insertion -> Option AQuick Check:
End flag = true after insert [OK]
- Setting IsEndOfWord to false instead of true
- Reinitializing root unnecessarily
- Clearing children during insert
Solution
Step 1: Insert each word fully
Insert each word character by character, creating nodes as needed.Step 2: Mark end of each word
Set IsEndOfWord = true only at the last character node of each inserted word to mark valid words.Final Answer:
Insert "bat", Insert "ball", Insert "batman"; set IsEndOfWord true only at last character of each word -> Option BQuick Check:
End flag true only at word ends [OK]
- Marking root or all nodes as end of word
- Not marking end of word nodes
- Setting end flag false after insert
Ready to prove it?
Pass the test and this pattern moves unknown
