0
0
DSA Javascriptprogramming~10 mins

Trie Search Operation in DSA Javascript - 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 as child
Move to child
Repeat for next character
After last character
Check if current node marks word end
Word found
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 and the last node marks the end of a word, the word exists.
Execution Sample
DSA Javascript
function search(word) {
  let node = root;
  for (let char of word) {
    if (!node.children[char]) return false;
    node = node.children[char];
  }
  return node.isEnd === true;
}
This code checks each character of the word in the trie. If any character is missing, it returns false. If all characters are found and the last node marks the end of a word, it returns true.
Execution Table
StepOperationCurrent CharacterNode Children KeysPointer ChangeVisual State
1Start search-c, bnode = rootroot → {c, b}
2Check charactercanode = node.children['c']root → c → {a}
3Check characterar, tnode = node.children['a']root → c → a → {r, t}
4Check charactertnode = node.children['t']root → c → a → t (isEnd: true)
5Check end of word---Word found: 'cat' exists
6Start search-c, bnode = rootroot → {c, b}
7Check charactercanode = node.children['c']root → c → {a}
8Check characterar, tnode = node.children['a']root → c → a → {r, t}
9Check characterrnode = node.children['r']root → c → a → r (isEnd: true)
10Check end of word---Word found: 'car' exists
11Start search-c, bnode = rootroot → {c, b}
12Check charactercanode = node.children['c']root → c → {a}
13Check characteroNo child 'o', stopWord not found: 'co' missing
14End search---Word 'co' does not exist
💡 Search stops when a character is missing or after confirming the last node marks word end.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 8After Step 9After Step 11After Step 12After Step 13Final
noderootnode.children['c']node.children['a']node.children['t']rootnode.children['c']node.children['a']node.children['r']rootnode.children['c']nullnull
current character-cat-car-co-
Key Moments - 3 Insights
Why does the search stop immediately when a character is missing in the children?
Because if the current node does not have the next character as a child (see Step 13), the word cannot be formed in the trie, so the search ends early.
Why do we check if the last node has isEnd true after traversing all characters?
Because the trie may have nodes for prefixes of words. Only if isEnd is true (Step 5 and 10) does the node represent a complete stored word.
What happens if we search for a prefix that is not a complete word?
The search will find all characters but fail at the isEnd check (Step 14), returning false since the prefix is not marked as a full word.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the current node's children keys?
A["t"]
B[]
C["a"]
D["r"]
💡 Hint
Check the 'Node Children Keys' column at Step 4 in the execution_table.
At which step does the search stop because the character is missing?
AStep 14
BStep 9
CStep 13
DStep 5
💡 Hint
Look for the step where 'No child' is mentioned in the 'Pointer Change' column.
If the word 'car' was not marked with isEnd true, what would be the search result at Step 10?
AWord not found
BSearch continues
CWord found
DError
💡 Hint
Refer to the 'Check end of word' operation and the importance of isEnd flag in the execution_table.
Concept Snapshot
Trie Search Operation:
- Start at root node
- For each character, move to child node if exists
- If missing, stop and return false
- After last char, check if node marks word end
- Return true if word exists, else false
Full Transcript
The trie search operation begins at the root node. For each character in the word, it checks if the character exists as a child of the current node. If it does, the pointer moves to that child node. If any character is missing, the search stops immediately and returns false. After all characters are found, the search checks if the current node marks the end of a word using the isEnd flag. If true, the word exists in the trie; otherwise, it does not. This process ensures efficient prefix-based search in the trie data structure.