Trie Search Operation in DSA C - Time & Space Complexity
We want to know how long it takes to find a word in a trie.
How does the search time change when the word gets longer?
Analyze the time complexity of the following code snippet.
// Search for a word in a trie
bool trieSearch(TrieNode* root, const char* word) {
TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (node->children[index] == NULL) {
return false;
}
node = node->children[index];
}
return node->isEndOfWord;
}
This code checks each letter of the word step-by-step in the trie.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each character of the input word.
- How many times: Once for each letter in the word (length n).
As the word gets longer, the search checks more letters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of steps grows directly with the word length.
Time Complexity: O(n)
This means the search time grows linearly with the length of the word.
[X] Wrong: "Searching a word in a trie depends on the total number of words stored."
[OK] Correct: The search only depends on the length of the word being searched, 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 the trie stored uppercase and lowercase letters separately? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the purpose of Trie search
The search operation in a Trie is designed to check if a specific word is present by traversing nodes corresponding to each character.Step 2: Identify what the search confirms
If all characters of the word are found sequentially and the last node marks the end of a word, the word exists in the Trie.Final Answer:
If a given word exists in the Trie -> Option CQuick Check:
Trie search checks word existence = C [OK]
- Confusing search with counting words
- Thinking search checks Trie emptiness
- Assuming search returns word length
Solution
Step 1: Understand character indexing in Trie
Characters are mapped to array indices by subtracting 'a' to get 0-based index for children array.Step 2: Check for non-null child node
To verify if a child node exists for character c, check if node->children[c - 'a'] is not NULL.Final Answer:
if (node->children[c - 'a'] != NULL) -> Option BQuick Check:
Index = c - 'a', check != NULL = B [OK]
- Using c directly as index without subtracting 'a'
- Checking for NULL with wrong comparison
- Adding 'a' to c instead of subtracting
bool search(struct TrieNode* root, const char* word) {
struct TrieNode* node = root;
for (int i = 0; word[i] != '\0'; i++) {
int index = word[i] - 'a';
if (node->children[index] == NULL) {
return false;
}
node = node->children[index];
}
return node->isEndOfWord;
}
Solution
Step 1: Understand inserted words and search target
Words "car" and "cat" are inserted, so nodes for 'c', 'a', 'r' and 't' exist with isEndOfWord true at 'r' and 't'.Step 2: Trace search for "cat"
Search traverses 'c' -> 'a' -> 't' nodes, all present, and node->isEndOfWord is true at 't', so returns true.Final Answer:
true -> Option AQuick Check:
Inserted "cat" found = true [OK]
- Assuming search returns false if similar word exists
- Confusing isEndOfWord with node presence
- Ignoring null checks in loop
bool search(struct TrieNode* root, const char* word) {
struct TrieNode* node = root;
int i = 0;
while (word[i] != '\0') {
int index = word[i] - 'a';
if (node->children[index] == NULL) {
return false;
}
node = node->children[index];
i++;
}
return false;
}Solution
Step 1: Analyze return statement after loop
The function returns false after loop ends, ignoring if current node marks end of word.Step 2: Identify missing check for word completion
It should return node->isEndOfWord to confirm full word presence, but returns false always.Final Answer:
Returns false even if word exists -> Option DQuick Check:
Missing isEndOfWord check causes false return [OK]
- Forgetting to check isEndOfWord at end
- Assuming node presence means word exists
- Ignoring root NULL check (less critical here)
"bathe", "bar", "bat", "barn"Solution
Step 1: Check each query word against inserted words
"bathe" is not inserted; "bar", "bat", and "barn" are inserted words in the Trie.Step 2: Understand Trie search returns false for non-inserted words
Since "bathe" is not inserted, search will fail at 'h' or after, returning false.Final Answer:
"bathe" -> Option AQuick Check:
Non-inserted word returns false = "bathe" [OK]
- Assuming prefix words always return true
- Confusing prefix presence with full word presence
- Ignoring isEndOfWord flag in search
Ready to prove it?
Pass the test and this pattern moves unknown
