Trie Search Operation in DSA C# - Time & Space Complexity
We want to understand how long it takes to find a word in a Trie.
How does the search time change as the word gets longer?
Analyze the time complexity of the following code snippet.
public bool Search(string word) {
TrieNode current = root;
foreach (char c in word) {
if (!current.Children.ContainsKey(c)) {
return false;
}
current = current.Children[c];
}
return current.IsEndOfWord;
}
This code checks if a word exists in the Trie by following nodes for each letter.
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 in the word (word length = n).
Each additional letter means one more step to check in the Trie.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The time grows directly with the length of the word.
Time Complexity: O(n)
This means the search time grows linearly with the word length.
[X] Wrong: "Searching a word in a Trie takes time proportional to the number of words stored."
[OK] Correct: The search depends only on the word length, not how many words are in the Trie.
Knowing Trie search time helps you explain fast word lookups in real apps like autocomplete or spell check.
"What if we changed the search to find all words with a given prefix? How would the time complexity change?"
Practice
Search operation in a Trie data structure do?Solution
Step 1: Understand the purpose of Search in Trie
The Search operation is designed to check if a specific word is present in the Trie data structure.Step 2: Differentiate from other operations
Insertion adds words, deletion removes words, and counting totals words. Search only checks existence.Final Answer:
Checks if a given word exists in the Trie -> Option AQuick Check:
Search = Check word presence [OK]
- Confusing search with insert operation
- Thinking search deletes words
- Assuming search counts words
Solution
Step 1: Identify return type for search
Search should return true or false indicating if the word exists, so return type must be bool.Step 2: Check method signature correctness
Method takes a string parameter for the word and returns bool, matching "public bool Search(string word)".Final Answer:
public bool Search(string word) -> Option BQuick Check:
Search returns bool for word presence [OK]
- Using void return type for search
- Returning int or string instead of bool
- Incorrect method parameter types
Trie trie = new Trie();
trie.Insert("car");
trie.Insert("cat");
Console.WriteLine(trie.Search("cat"));
Console.WriteLine(trie.Search("cap"));Solution
Step 1: Analyze inserted words
Words "car" and "cat" are inserted, so both exist in the Trie.Step 2: Check search results for "cat" and "cap"
"cat" exists, so search returns true; "cap" was not inserted, so search returns false.Final Answer:
True False -> Option CQuick Check:
Search("cat")=True, Search("cap")=False [OK]
- Assuming "cap" exists after inserting "car" and "cat"
- Mixing output order
- Confusing true/false outputs
public bool Search(string word) {
TrieNode node = root;
foreach (char c in word) {
if (!node.Children.ContainsKey(c))
return false;
node = node.Children[c];
}
return false;
}Solution
Step 1: Understand the search logic
The method checks each character in the word exists in the Trie nodes.Step 2: Identify the missing return condition
After the loop, it always returns false instead of checking if the current node marks the end of a valid word.Final Answer:
It should return true after checking all characters if node marks end of word -> Option DQuick Check:
Return true if end of word reached [OK]
- Returning false unconditionally after loop
- Not checking end of word flag
- Returning true too early
Solution
Step 1: Understand prefix search goal
Prefix search should confirm if the Trie contains any word starting with the given prefix.Step 2: Adjust search logic for prefix
Return true immediately after all prefix characters are found, no need to check if prefix is a complete word.Final Answer:
Return true as soon as all prefix characters are found, without checking end of word -> Option AQuick Check:
Prefix found = true without full word check [OK]
- Checking end of word for prefix search
- Returning false if prefix is not a full word
- Confusing prefix search with full word search
Ready to prove it?
Pass the test and this pattern moves unknown
