Trie Search Operation in DSA Java - 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 length or Trie size grows?
Analyze the time complexity of the following code snippet.
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!current.children.containsKey(ch)) {
return false;
}
current = current.children.get(ch);
}
return current.isEndOfWord;
}
This code checks if a word exists in the Trie by following each character step-by-step.
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).
As the word gets longer, the search checks more characters one by one.
| Input Size (word length) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The time grows directly with the length of the word.
Time Complexity: O(m)
This means the search time grows linearly with the length of the word being searched.
[X] Wrong: "The search time depends on the total number of words stored in the Trie."
[OK] Correct: The search only follows the characters of the word, so it depends on word length, not how many words are stored.
Knowing this helps you explain why Tries are efficient for prefix searches and autocomplete features.
"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, but search only checks existence.Final Answer:
Checks if a given word exists in the Trie -> Option BQuick 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 returns true or false depending on whether the word exists, so return type must be boolean.Step 2: Check method parameters and return type
Method takes a String word and returns boolean, so signature ispublic boolean search(String word).Final Answer:
public boolean search(String word) -> Option AQuick Check:
Search returns boolean [OK]
- Using void return type for search
- Returning int or String instead of boolean
- Incorrect method parameters
Trie trie = new Trie();
trie.insert("cat");
trie.insert("car");
System.out.println(trie.search("cat"));
System.out.println(trie.search("cap"));Solution
Step 1: Insert words "cat" and "car" into Trie
Both words are stored in the Trie nodes.Step 2: Search for "cat" and "cap"
"cat" exists, so search returns true; "cap" does not exist, so search returns false.Final Answer:
true false -> Option AQuick Check:
Search("cat")=true, Search("cap")=false [OK]
- Assuming partial matches return true
- Confusing inserted words with similar words
- Expecting search to insert missing words
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i <= word.length(); i++) {
char ch = word.charAt(i);
if (!current.children.containsKey(ch)) {
return false;
}
current = current.children.get(ch);
}
return current.isEndOfWord;
}Solution
Step 1: Analyze loop boundary
The loop uses i <= word.length(), which causes index out of bounds error when i equals word.length().Step 2: Correct loop condition
Change to i < word.length() to iterate valid indices only.Final Answer:
Change loop condition to i < word.length() -> Option DQuick Check:
Loop index must be less than length [OK]
- Using <= causes index out of bounds
- Ignoring null checks on children
- Wrong method to check map keys
true and why?
Options:
A) search("app")
B) search("bathe")
C) search("bath")
D) search("bat")Solution
Step 1: Check each word in the Trie
Words stored are "apple", "app", "bat", and "bath".Step 2: Evaluate each search query
"app" exists, so true; "bat" exists, so true; "bath" exists, so true; "bathe" does not exist, so false.Final Answer:
"app", "bat", and "bath" return true -> Option CQuick Check:
Search returns true only for stored words [OK]
- Assuming partial or longer words return true
- Confusing prefixes with full words
- Expecting non-inserted words to return true
Ready to prove it?
Pass the test and this pattern moves unknown
