0
0
DSA Typescriptprogramming~10 mins

Autocomplete System with Trie in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Autocomplete System with Trie
Start: Insert words into Trie
For each word: For each char
If char node missing: Create node
Move to char node
Mark end of word
Autocomplete query: Traverse Trie by prefix
If prefix node found: DFS to collect words
Return list of words starting with prefix
Build a Trie by inserting words character by character, then find autocomplete suggestions by traversing the Trie nodes matching the prefix and collecting all words below.
Execution Sample
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isWordEnd: boolean = false;
}

// Insert 'cat'
// Insert 'car'
// Autocomplete 'ca'
Insert words 'cat' and 'car' into the Trie, then find all words starting with prefix 'ca'.
Execution Table
StepOperationNodes in TriePointer ChangesVisual State
1Insert 'c'Root -> cCreate node for 'c' under rootroot └─ c
2Insert 'a' after 'c'Root -> c -> aCreate node for 'a' under 'c'root └─ c └─ a
3Insert 't' after 'a'Root -> c -> a -> t (word end)Create node for 't' under 'a', mark isWordEndroot └─ c └─ a └─ t*
4Insert 'c' (existing)Root -> c -> a -> t*Reuse node 'c' at rootroot └─ c └─ a └─ t*
5Insert 'a' (existing)Root -> c -> a -> t*Reuse node 'a' under 'c'root └─ c └─ a └─ t*
6Insert 'r' after 'a'Root -> c -> a -> t*, r (word end)Create node 'r' under 'a', mark isWordEndroot └─ c └─ a ├─ t* └─ r*
7Autocomplete 'ca' prefixTraverse root -> c -> aPointer moves to node 'a'At node 'a' with children 't*' and 'r*'
8Collect words from 'a' nodeWords found: 'cat', 'car'DFS visits 't*' and 'r*' nodesSuggestions: ['cat', 'car']
9Return suggestionsAutocomplete completeReturn ['cat', 'car']Output: ['cat', 'car']
💡 All characters inserted and prefix traversal complete, suggestions collected.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 6After Step 7Final
Trie RootEmptyHas child 'c'Has path c->a->t*Has path c->a->t*, r*Pointer at node 'a'Pointer at node 'a'
Current NodeRootNode 'c'Node 't'Node 'r'Node 'a'Node 'a'
isWordEnd flagsNoneNone't' true't' and 'r' true't' and 'r' true't' and 'r' true
Suggestions ListEmptyEmptyEmptyEmptyEmpty['cat', 'car']
Key Moments - 3 Insights
Why do we create new nodes only if the character node does not exist?
Because existing nodes represent shared prefixes, so we reuse them to save space and avoid duplicates. See execution_table steps 4 and 5 where 'c' and 'a' nodes are reused.
How do we know when a word ends in the Trie?
We mark the last character node of a word with isWordEnd = true. In execution_table step 3 and 6, nodes 't' and 'r' are marked to indicate 'cat' and 'car' are complete words.
How does autocomplete find all words starting with a prefix?
After reaching the prefix node, we perform a depth-first search (DFS) to collect all words below it. See execution_table step 8 where DFS visits 't*' and 'r*' nodes to find 'cat' and 'car'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the visual state after step 6?
Aroot └─ c └─ a └─ t*
Broot └─ c └─ a ├─ t* └─ r*
Croot └─ c └─ r*
Droot └─ a └─ t*
💡 Hint
Check the Visual State column in execution_table row for step 6.
At which step does the pointer move to the prefix node 'a' during autocomplete?
AStep 7
BStep 5
CStep 3
DStep 9
💡 Hint
Look at the Pointer Changes column in execution_table for step 7.
If we insert the word 'cap' after 'car', how would the Trie nodes under 'a' change?
AAdd a new child node 'p' under 'c'
BReplace node 'r' with 'p'
CAdd a new child node 'p' under 'a' marked as word end
DNo change, 'cap' is ignored
💡 Hint
New words add new nodes for characters not already present under the prefix node.
Concept Snapshot
Autocomplete with Trie:
- Insert words char by char, creating nodes if missing
- Mark last char node as word end
- For prefix search, traverse Trie nodes matching prefix
- From prefix node, DFS to collect all words
- Efficient prefix-based word retrieval
- Shared prefixes save space and speed up search
Full Transcript
This visualization shows how an autocomplete system uses a Trie data structure. Words are inserted one character at a time, creating new nodes only when needed. Each word's last character node is marked to indicate a complete word. To autocomplete, we traverse the Trie to the prefix node, then collect all words below it using depth-first search. The execution table tracks each insertion and traversal step, showing how nodes are created and reused. The variable tracker follows pointers and word-end flags. Key moments clarify why nodes are reused, how word ends are marked, and how autocomplete collects suggestions. The quiz tests understanding of Trie structure and traversal steps.