0
0
DSA Typescriptprogramming~10 mins

Trie Search Operation in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Trie Search Operation
Start at root node
For each character in word
Check if character exists in current node's children
Move to child node
After last character, check isEndOfWord
Word found
End
The search starts at the root and checks each character in the word. If a character is missing, search stops. If all characters are found, it checks if the last node marks the end of a word.
Execution Sample
DSA Typescript
function search(word: string): boolean {
  let node = root;
  for (const char of word) {
    if (!node.children.has(char)) return false;
    node = node.children.get(char)!;
  }
  return node.isEndOfWord;
}
This code searches for a word in the trie by traversing nodes for each character and returns true if the word exists.
Execution Table
StepOperationCurrent CharacterNode Children KeysPointer ChangeVisual State
1Start at root-a, b, cnode = rootroot → {a, b, c}
2Check 'c'ca, b, cnode = node.children.get('c')root → c node → {a, t}
3Check 'a'aa, tnode = node.children.get('a')c node → a node → {t}
4Check 't'ttnode = node.children.get('t')a node → t node → {}
5Check isEndOfWord---t node isEndOfWord = true
6Return result---Word found: true
💡 Search ends after checking all characters and confirming isEndOfWord is true
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
noderootc nodea nodet nodet node
Key Moments - 3 Insights
Why do we check isEndOfWord after traversing all characters?
Because a node can exist for a prefix but not mark a complete word. Checking isEndOfWord confirms the word ends here (see Step 5 in execution_table).
What happens if a character is missing in children?
The search stops immediately and returns false, as shown in Step 2 where if 'c' was missing, we would return false.
Why do we update 'node' pointer at each character?
To move deeper into the trie along the path of the word. Each update points to the next node representing the next character (see Pointer Change column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'node' variable after Step 3?
Aa node
Bc node
Croot node
Dt node
💡 Hint
Check the 'Pointer Change' and 'Variable Tracker' after Step 3
At which step does the search confirm the word exists?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Operation' and 'Visual State' columns for isEndOfWord check
If the character 'a' was missing in Step 3, what would happen?
ASearch continues to next character
BReturn true immediately
CReturn false immediately
DSkip character and continue
💡 Hint
Refer to the 'Operation' and 'Pointer Change' logic in Step 2 for missing character
Concept Snapshot
Trie Search Operation:
- Start at root node
- For each character, move to child node if exists
- If missing, return false
- After last char, check isEndOfWord
- Return true if word ends here, else false
Full Transcript
The Trie Search Operation starts at the root node. For each character in the word, it checks if the character exists among the current node's children. If it does, the pointer moves to that child node. If any character is missing, the search stops and returns false. After all characters are found, the search checks if the current node marks the end of a word (isEndOfWord). If yes, the word exists in the trie and returns true; otherwise, it returns false.