What if you could find all words starting with a few letters instantly, no matter how big your list is?
Why Prefix Search Using Trie in DSA C++?
Imagine you have a huge phone book and you want to find all names starting with "Sam". You start flipping pages one by one, checking each name carefully.
This manual search is slow and tiring. You waste time checking every name, even those that don't start with "Sam". It's easy to miss some or get tired and make mistakes.
A Trie is like a smart tree that organizes words by their letters. It lets you jump directly to all names starting with "Sam" without checking every name. This saves time and effort.
for (string name : phoneBook) { if (name.substr(0, 3) == "Sam") { cout << name << endl; } }
Trie trie; trie.insert("Samuel"); trie.insert("Samantha"); vector<string> results = trie.searchPrefix("Sam");
It enables lightning-fast search for all words sharing the same beginning, making autocomplete and dictionary lookups smooth and instant.
When you type in a search box on your phone, the suggestions appear instantly because the system uses prefix search with a Trie behind the scenes.
Manual search checks every word, which is slow and error-prone.
Trie organizes words by letters, speeding up prefix searches.
Prefix search with Trie makes autocomplete and quick lookups possible.
Practice
Solution
Step 1: Understand Trie usage
A Trie stores characters of words in a tree structure, allowing quick prefix lookups.Step 2: Identify prefix search goal
Prefix search means finding all words starting with a given prefix efficiently, which Trie supports well.Final Answer:
To efficiently find all words that start with a given prefix -> Option DQuick Check:
Trie = Prefix search efficiency [OK]
- Confusing Trie with sorting algorithms
- Thinking Trie is a hash table
- Assuming Trie reverses strings
Solution
Step 1: Check Trie node structure
A Trie node typically has a boolean to mark word end and an array of pointers for children nodes.Step 2: Match correct syntax
struct TrieNode { bool isEnd; TrieNode* children[26]; }; correctly declares a boolean and an array of 26 pointers for English lowercase letters.Final Answer:
struct TrieNode { bool isEnd; TrieNode* children[26]; }; -> Option AQuick Check:
Trie node = bool + pointer array [OK]
- Using vector<int> instead of pointers
- Missing pointer (*) in children array
- Declaring children as single object, not array
Trie trie;
trie.insert("cat");
trie.insert("car");
trie.insert("dog");
vector<string> result = trie.searchPrefix("ca");
for (auto &word : result) std::cout << word << " ";Solution
Step 1: Insert words into Trie
Words "cat", "car", and "dog" are inserted. Trie stores them by characters.Step 2: Search prefix "ca"
Searching prefix "ca" returns all words starting with "ca", which are "cat" and "car".Final Answer:
cat car -> Option AQuick Check:
Prefix "ca" matches "cat" and "car" [OK]
- Including words without the prefix
- Assuming order is alphabetical by default
- Confusing prefix search with full word search
void insert(string word) {
TrieNode* node = root;
for (char c : word) {
int index = c - 'a';
if (!node->children[index])
node->children[index] = new TrieNode();
node = node->children[index];
}
node->isEnd = false;
}Solution
Step 1: Review isEnd assignment
After inserting a word, the node representing last character should mark end of word by setting isEnd to true.Step 2: Identify incorrect isEnd value
Code sets isEnd to false, which is wrong and will cause search failures.Final Answer:
isEnd should be set to true after insertion -> Option BQuick Check:
Mark word end with isEnd = true [OK]
- Setting isEnd to false instead of true
- Forgetting to initialize root node
- Miscomputing index from character
Solution
Step 1: Perform prefix search "app" in Trie
Traverse Trie nodes for prefix "app" to reach subtree containing all matching words.Step 2: Filter collected words by length 5
From subtree, collect only words whose length equals 5 to meet the length condition.Final Answer:
Search prefix "app" in Trie, then collect words with length 5 only -> Option CQuick Check:
Prefix search + length filter = Search prefix "app" in Trie, then collect words with length 5 only [OK]
- Returning all prefix matches without length check
- Filtering all words instead of prefix subtree
- Using hash map losing prefix search efficiency
Ready to prove it?
Pass the test and this pattern moves unknown
