Trie Search Operation in DSA C++ - Time & Space Complexity
We want to know how long it takes to find a word in a Trie data structure.
The question is: how does the search time grow as the word length changes?
Analyze the time complexity of the following code snippet.
bool search(TrieNode* root, const std::string& word) {
TrieNode* node = root;
for (char c : word) {
if (!node->children[c - 'a']) {
return false;
}
node = node->children[c - 'a'];
}
return node->isEndOfWord;
}
This code checks if a given word exists in the Trie by following each character's path.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character in the word.
- How many times: Exactly once per character in the word (word length).
As the word gets longer, the search checks more characters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The number of operations grows directly with the word length.
Time Complexity: O(n)
This means the search time grows linearly with the length of the word you are looking for.
[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 length of the word, not how many words are in the Trie.
Knowing Trie search time helps you explain efficient word lookups, a common task in coding problems.
"What if the Trie stored uppercase and lowercase letters separately? 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 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:
Trie search = check word presence [OK]
- Confusing search with insert operation
- Thinking search deletes words
- Assuming search counts words
Solution
Step 1: Understand Trie children representation
Trie children are usually stored in a map or unordered_map. To check existence, use find() method.Step 2: Identify correct syntax for existence check
Usingfind(ch) != end()confirms if character node exists without creating new nodes.Final Answer:
if (node->children.find(ch) != node->children.end()) -> Option DQuick Check:
Use find() to check key existence in map [OK]
- Using node->children[ch] != nullptr causes insertion
- Accessing children[ch] directly creates new node
- Checking children[ch] == nullptr can cause insertion
bool search(TrieNode* root, const string& word) {
TrieNode* node = root;
for (char ch : word) {
if (node->children.find(ch) == node->children.end())
return false;
node = node->children[ch];
}
return node->isEndOfWord;
}
What will be the output of search(root, "cat") if the Trie contains words: "car", "cat", "cap"?Solution
Step 1: Trace search for "cat" in Trie
Each character 'c', 'a', 't' is found in children nodes. The loop completes without returning false.Step 2: Check isEndOfWord flag at last node
Since "cat" is inserted, the last node's isEndOfWord is true, so function returns true.Final Answer:
true -> Option AQuick Check:
Word "cat" exists in Trie = true [OK]
- Ignoring isEndOfWord flag and returning true prematurely
- Assuming partial match means word exists
- Confusing false return with missing character
bool search(TrieNode* root, const string& word) {
TrieNode* node = root;
for (char ch : word) {
if (node->children[ch] == nullptr)
return false;
node = node->children[ch];
}
return node->isEndOfWord;
}Solution
Step 1: Analyze children access in if condition
Accessing node->children[ch] creates a new node if it doesn't exist, which is wrong during search.Step 2: Correct way to check existence
Use find() method to check if child exists without creating new nodes.Final Answer:
Using node->children[ch] in if condition inserts new nodes unintentionally -> Option BQuick Check:
Accessing map key directly creates nodes [OK]
- Using direct map access in condition causing side effects
- Ignoring empty string edge case
- Wrong loop variable type
Solution
Step 1: Check if "appl" is a complete word in Trie
"appl" is a prefix of "apple" and "application" but not a complete word, so isEndOfWord is false.Step 2: Verify other words
"app", "batch", and "bat" are all complete words stored in the Trie, so search returns true for them.Final Answer:
"appl" -> Option CQuick Check:
Prefix only, not full word = false [OK]
- Assuming prefix is always found as word
- Ignoring isEndOfWord flag
- Confusing similar words with exact matches
Ready to prove it?
Pass the test and this pattern moves unknown
