0
0
DSA Typescriptprogramming~10 mins

Trie Insert Operation in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Trie Insert Operation
Start at root node
For each character in word
Check if child node exists
Move to child
Repeat for next character
Mark last node as end of word
Done
Insert each character by traversing or creating nodes, then mark the last node as end of word.
Execution Sample
DSA Typescript
insert(word: string) {
  let node = this.root;
  for (const char of word) {
    if (!node.children.has(char)) {
      node.children.set(char, new TrieNode());
    }
    node = node.children.get(char)!;
  }
  node.isEnd = true;
}
Inserts a word into the trie by creating nodes for missing characters and marking the end.
Execution Table
StepOperationCharacterNode VisitedPointer ChangesTrie Visual State
1Start insertion-rootnode = rootroot (children: {})
2Check 'c' in root childrencrootCreate node for 'c'root (children: {c}) c (children: {})
3Move to node 'c'cnode 'c'node = node 'c'root (children: {c}) c (children: {})
4Check 'a' in node 'c' childrenanode 'c'Create node for 'a'root (children: {c}) c (children: {a}) a (children: {})
5Move to node 'a'anode 'a'node = node 'a'root (children: {c}) c (children: {a}) a (children: {})
6Check 't' in node 'a' childrentnode 'a'Create node for 't'root (children: {c}) c (children: {a}) a (children: {t}) t (children: {})
7Move to node 't'tnode 't'node = node 't'root (children: {c}) c (children: {a}) a (children: {t}) t (children: {})
8Mark node 't' as end of word-node 't'node.isEnd = trueroot (children: {c}) c (children: {a}) a (children: {t}) t* (end)
9Insertion complete-node 't'-root (children: {c}) c (children: {a}) a (children: {t}) t* (end)
💡 All characters processed and last node marked as end of word.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 8Final
noderootnode 'c'node 'a'node 't'node 't' (isEnd=true)node 't' (isEnd=true)
Key Moments - 3 Insights
Why do we create a new node only if the character is not already a child?
Because if the child node exists (see step 3 and 5), we just move to it to reuse existing paths, avoiding duplicates.
Why do we mark the last node as end of word?
Marking the last node (step 8) tells the trie that a complete word ends here, distinguishing prefixes from full words.
What happens if we insert a word that shares prefix with existing words?
We reuse existing nodes for shared characters (steps 2-7), only creating new nodes for characters not present.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the node visited at step 5?
Anode 'c'
Bnode 'a'
Cnode 't'
Droot
💡 Hint
Check the 'Node Visited' column at step 5 in the execution table.
At which step is the node for character 't' created?
AStep 6
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Pointer Changes' column for creation of nodes in the execution table.
If the word 'cat' was already in the trie, what would happen at step 2?
ACreate new node for 'c'
BMark node 'c' as end of word
CMove to existing node 'c'
DStop insertion
💡 Hint
Refer to the condition check and pointer changes in the execution table steps.
Concept Snapshot
Trie Insert Operation:
- Start at root node
- For each character:
  - If child exists, move to it
  - Else create new child node
- Mark last node as end of word
- Reuses prefixes to save space
Full Transcript
The Trie Insert Operation starts at the root node. For each character in the word, it checks if a child node exists. If yes, it moves to that node; if not, it creates a new node. After processing all characters, it marks the last node as the end of the word. This process efficiently stores words by sharing common prefixes.