0
0
DSA Goprogramming~10 mins

Prefix Search Using Trie in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Prefix Search Using Trie
Start at root node
For each character in prefix
Check if character exists in current node's children
Move to child
Repeat for next character
Prefix found
Return all words from this node's subtree
Start from the root and follow each prefix character down the trie. If all characters match, collect all words below.
Execution Sample
DSA Go
type TrieNode struct {
  children map[rune]*TrieNode
  isEnd bool
}

func (t *TrieNode) SearchPrefix(prefix string) bool {
  node := t
  for _, ch := range prefix {
    if node.children == nil || node.children[ch] == nil {
      return false
    }
    node = node.children[ch]
  }
  return true
}
This code checks if a prefix exists in the trie by traversing nodes for each character.
Execution Table
StepOperationCurrent CharacterNode Children KeysPointer ChangesVisual State
1Start at root-a, b, ccurrent = rootroot -> {a,b,c}
2Check 'b' in childrenba, b, ccurrent = node for 'b'root -> b -> {...}
3Check 'a' in childrenaa, rcurrent = node for 'a'root -> b -> a -> {...}
4Check 't' in childrenttcurrent = node for 't'root -> b -> a -> t -> {...}
5Prefix found---Prefix 'bat' exists in trie
6Collect words from current node---Words: 'bat', 'bath', 'batman'
7End search---Search complete
💡 Traversal ends when all prefix characters are found or a character is missing.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
currentrootnode for 'b'node for 'a'node for 't'node for 't'
Key Moments - 3 Insights
Why do we stop searching if a character is not found in children?
Because if the character is missing, the prefix cannot exist in the trie. See execution_table step 2 where we check children keys.
How do we find all words starting with the prefix after traversal?
After reaching the last prefix node, we explore all child nodes recursively to collect words. Refer to execution_table step 6.
Why do we keep updating the 'current' pointer during traversal?
To move down the trie nodes matching each prefix character. This is shown in variable_tracker where 'current' changes each step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'current' node after checking character 'a'?
Anode for 'b'
Bnode for 't'
Cnode for 'a'
Droot
💡 Hint
Check variable_tracker column 'After Step 3' for 'current' value.
At which step does the prefix search confirm the prefix exists?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at execution_table row where 'Prefix found' is noted.
If the prefix was 'batman', which step would fail if 'm' was missing in children?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
The failure happens when a character is not found; see step 5 where prefix is confirmed.
Concept Snapshot
Prefix Search Using Trie:
- Start at root node
- For each prefix character, move to child node
- If child missing, prefix not found
- If all found, prefix exists
- Collect all words from last node's subtree
Full Transcript
Prefix search in a trie starts at the root node. For each character in the prefix, we check if it exists in the current node's children. If yes, we move the pointer to that child node. If any character is missing, the prefix does not exist. When all characters are found, the prefix exists. Then, we collect all words from the subtree rooted at the last node. This process is shown step-by-step in the execution table and variable tracker.