0
0
DSA C++programming~10 mins

Word Search in Trie in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Word Search in Trie
Start at root node
For each character in word
Check if character exists in current node's children
Move to child node
Repeat for next character
After last character
Check if current node marks end of word
Word FOUND
Start from root, check each character in the word by moving through child nodes. If all characters match and last node marks word end, word is found.
Execution Sample
DSA C++
bool searchWord(TrieNode* root, string word) {
  TrieNode* current = root;
  for (char c : word) {
    if (!current->children[c - 'a']) return false;
    current = current->children[c - 'a'];
  }
  return current->isEndOfWord;
}
This code checks if a word exists in the trie by traversing nodes for each character.
Execution Table
StepOperationCurrent CharacterNode Exists?Move PointerVisual State
1Start at root-N/Acurrent = rootroot
2Check 'c''c'Yescurrent = node for 'c'root -> c
3Check 'a''a'Yescurrent = node for 'a'root -> c -> a
4Check 't''t'Yescurrent = node for 't'root -> c -> a -> t
5End of word check-N/ACheck isEndOfWord at 't'root -> c -> a -> t (isEndOfWord = true)
6Result-N/AReturn trueWord found
7Start at root-N/Acurrent = rootroot
8Check 'd''d'Yescurrent = node for 'd'root -> d
9Check 'o''o'NoReturn falseroot -> d (no 'o' child)
10Result-N/AReturn falseWord not found
💡 Traversal stops when a character node is missing or after checking last character's end marker.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 8After Step 9
currentrootnode 'c'node 'a'node 't'node 't'node 'd'node 'd'
current->isEndOfWordfalsefalsefalsetruetruefalsefalse
Key Moments - 3 Insights
Why do we return false immediately when a character node does not exist?
Because if the trie does not have a child node for the current character (see Step 9 in execution_table), the word cannot be formed, so we stop searching.
Why do we check isEndOfWord after the last character?
Because the path may exist but might not represent a complete word. Only if isEndOfWord is true (Step 5) do we confirm the word exists.
What happens if the word is empty?
The search starts and ends at root. Since no characters are checked, the result depends on whether root marks end of a word, usually false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current node after Step 3 when searching 'cat'?
ANode for 'c'
BNode for 'a'
CNode for 't'
DRoot node
💡 Hint
Check the 'Move Pointer' and 'Visual State' columns at Step 3 in execution_table.
At which step does the search for 'dog' fail due to missing node?
AStep 9
BStep 8
CStep 7
DStep 10
💡 Hint
Look for 'Node Exists?' column showing 'No' in execution_table.
If the last node's isEndOfWord was false for 'cat', what would be the search result?
AWord found
BPartial match found
CWord not found
DError in trie
💡 Hint
Refer to Step 5 and Step 6 in execution_table about isEndOfWord check.
Concept Snapshot
Word Search in Trie:
- Start at root node
- For each character, move to child node if exists
- If any character missing, word not found
- After last char, check isEndOfWord flag
- Return true if flag set, else false
Full Transcript
This visual execution shows how to search a word in a trie. We start at the root node and check each character of the word one by one. If the current character exists as a child node, we move the pointer to that node. If not, we stop and return false because the word cannot be formed. After checking all characters, we verify if the last node marks the end of a word. If yes, the word is found; otherwise, it is not. The execution table traces searching 'cat' successfully and 'dog' failing at the second character. Variable tracker shows how the current pointer moves through nodes. Key moments clarify why missing nodes cause immediate failure and why the end-of-word flag matters. The quiz tests understanding of pointer movement, failure step, and end-of-word significance.