0
0
DSA C++programming~10 mins

Count Words with Given Prefix in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Words with Given Prefix
Start at root node
For each char in prefix
Check if char child exists?
NoPrefix not found, return 0
Yes
Move to child node
Repeat for next char
After last char
Return count of words with this prefix
Start from the root of the trie and follow each character of the prefix. If any character is missing, return 0. Otherwise, after the last character, return the count of words that share this prefix.
Execution Sample
DSA C++
class TrieNode {
public:
  TrieNode* children[26];
  int prefixCount;
  TrieNode() : prefixCount(0) {
    for (int i = 0; i < 26; i++) children[i] = nullptr;
  }
};

int countWordsWithPrefix(TrieNode* root, const string& prefix) {
  TrieNode* curr = root;
  for (char c : prefix) {
    int idx = c - 'a';
    if (!curr->children[idx]) return 0;
    curr = curr->children[idx];
  }
  return curr->prefixCount;
}
This code counts how many words in the trie start with the given prefix by traversing nodes for each prefix character and returning the stored prefix count.
Execution Table
StepOperationCurrent NodeCharacterChild Exists?ActionPrefix CountVisual State
1Start at rootroot-N/AMove to next char5root(5)
2Check 'a'rootaYesMove to child 'a'3root(5) -> a(3)
3Check 'p'apYesMove to child 'p'3root(5) -> a(3) -> p(3)
4Check 'p'ppYesMove to child 'p'2root(5) -> a(3) -> p(3) -> p(2)
5Check 'l'plYesMove to child 'l'2root(5) -> a(3) -> p(3) -> p(2) -> l(2)
6Check 'e'leYesMove to child 'e'1root(5) -> a(3) -> p(3) -> p(2) -> l(2) -> e(1)
7Prefix ende-N/AReturn prefixCount1root(5) -> a(3) -> p(3) -> p(2) -> l(2) -> e(1)
💡 Traversal ends after last prefix character 'e'; prefixCount 1 returned.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
currrootnode 'a'node 'p'node 'p'node 'l'node 'e'node 'e'
prefixCount-332211
Key Moments - 3 Insights
Why do we return 0 if a child node for a prefix character does not exist?
Because if any character in the prefix is missing in the trie, it means no word starts with that prefix, so the count is zero.
Why do we return prefixCount at the last node of the prefix?
The prefixCount at the last node stores how many words have that prefix (see Step 7 in execution_table), so returning it gives the total count of words starting with the prefix.
What does prefixCount represent at each node?
prefixCount counts how many words pass through that node, meaning how many words have the prefix up to that character (visible in variable_tracker and execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the current node and character checked?
ANode 'p', character 'p'
BNode 'a', character 'p'
CNode 'p', character 'l'
DNode 'root', character 'a'
💡 Hint
Check the 'Current Node' and 'Character' columns in execution_table row for Step 4.
At which step does the traversal reach the last character of the prefix?
AStep 5
BStep 7
CStep 6
DStep 4
💡 Hint
Look for the step where the last prefix character 'e' is checked in execution_table.
If the prefix was 'appx' instead of 'apple', what would happen in the execution_table?
ATraversal would return prefixCount at node 'p'
BTraversal would return 0 at the missing child for 'x'
CTraversal would continue normally and return prefixCount at 'e'
DTraversal would skip the missing character and continue
💡 Hint
Refer to the 'Child Exists?' and 'Action' columns for missing child cases in execution_table.
Concept Snapshot
Count Words with Given Prefix in Trie:
- Start at root node
- For each prefix char, move to child node
- If child missing, return 0
- After last char, return prefixCount stored
- prefixCount = number of words sharing that prefix
Full Transcript
To count words with a given prefix in a trie, start at the root node and follow each character of the prefix. If at any point the child node for a character does not exist, return zero because no words have that prefix. If all characters exist, after the last character, return the prefixCount stored at that node. This count tells how many words start with the prefix. The code traverses nodes for each prefix character and returns the stored count. The execution table shows each step moving through nodes for characters 'a', 'p', 'p', 'l', 'e' and finally returning the count 1. Variables track the current node and prefixCount at each step. Key moments clarify why missing children return zero and why prefixCount is returned at the last node. The visual quiz tests understanding of node traversal and missing child handling.