0
0
DSA Goprogramming~10 mins

Word Search in Trie in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Word Search in Trie
Start at root node
For each letter in word
Check if letter child exists?
NoWord NOT found
Yes
Move to child node
Repeat for next letter
After last letter, check isWord flag
If isWord true -> Word FOUND
Else -> Word NOT found
Start from the root and check each letter of the word in the trie nodes. If all letters exist and the last node marks a word end, the word is found.
Execution Sample
DSA Go
func Search(root *TrieNode, word string) bool {
    node := root
    for _, ch := range word {
        if node.children[ch-'a'] == nil {
            return false
        }
        node = node.children[ch-'a']
    }
    return node.isWord
}
This code searches for a word in a trie by traversing nodes for each letter and checking if the final node marks a complete word.
Execution Table
StepOperationCurrent LetterNode Children CheckMove PointerVisual State
1Start at root-Root has child 'c' for 'c'?Move to child 'c'root -> c
2Check letter 'a'aNode 'c' has child 'a'?Move to child 'a'root -> c -> a
3Check letter 't'tNode 'a' has child 't'?Move to child 't'root -> c -> a -> t
4End of word-Node 't' isWord?No moveroot -> c -> a -> t (isWord=true)
5Result---Word FOUND
💡 Traversal ends after last letter; isWord flag true means word found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
noderootnode at 'c'node at 'a'node at 't'node at 't'node at 't'
current letter-cat--
Key Moments - 3 Insights
Why do we check if the child node exists before moving?
Because if the child node for the current letter does not exist (see execution_table step 2), the word cannot be in the trie, so we stop searching.
Why do we check the isWord flag at the last node?
Because the path may exist but not represent a complete word. Only if isWord is true (execution_table step 4) do we confirm the word is stored.
What happens if the word is empty?
The loop does not run, so we check if root.isWord is true. Usually root.isWord is false, so empty word is not found.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the node pointer after checking letter 'a'?
ANode at 't'
BNode at 'c'
CNode at 'a'
DRoot node
💡 Hint
Check the 'Move Pointer' column at step 2 in execution_table.
At which step does the search confirm the word is found?
AStep 3
BStep 5
CStep 2
DStep 4
💡 Hint
Look at the 'Result' row in execution_table where word FOUND is stated.
If the node for letter 't' was missing, what would happen?
ASearch returns false at step 3
BSearch continues to next letter
CSearch returns true
DSearch checks isWord flag
💡 Hint
Refer to 'Node Children Check' and 'Operation' columns in execution_table step 3.
Concept Snapshot
Word Search in Trie:
- Start at root node
- For each letter, check if child node exists
- Move pointer to child node if exists
- After last letter, check isWord flag
- If true, word found; else not found
Full Transcript
To search a word in a trie, start at the root node. For each letter in the word, check if the current node has a child node for that letter. If not, the word is not in the trie. If yes, move to that child node and continue. After processing all letters, check if the last node marks the end of a word (isWord flag). If it does, the word is found; otherwise, it is not. This process ensures only complete words stored in the trie are recognized.