0
0
DSA Typescriptprogramming~10 mins

Huffman Encoding in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Huffman Encoding
Count frequency of each character
Create leaf nodes for each char
Build min-heap from leaf nodes
While heap size > 1
Extract two smallest nodes
Create new internal node with these two as children
Insert new node back into heap
Back to While
Heap has one node: root of Huffman Tree
Traverse tree to assign codes
Output codes for each character
Back to While
This flow shows how Huffman Encoding builds a tree from character frequencies, then assigns binary codes by traversing the tree.
Execution Sample
DSA Typescript
const freq = {a:5, b:9, c:12, d:13, e:16, f:45};
// Build Huffman Tree and assign codes
// Output codes for each character
This code builds a Huffman tree from given character frequencies and outputs the binary codes.
Execution Table
StepHeap Content (nodes with freq)ActionNew Node CreatedHeap After Insertion
1[a:5, b:9, c:12, d:13, e:16, f:45]Extract two smallest: a:5, b:9Node(ab):14[Node(ab):14, c:12, d:13, e:16, f:45]
2[Node(ab):14, c:12, d:13, e:16, f:45]Extract two smallest: c:12, d:13Node(cd):25[Node(ab):14, Node(cd):25, e:16, f:45]
3[Node(ab):14, Node(cd):25, e:16, f:45]Extract two smallest: Node(ab):14, e:16Node(abe):30[Node(cd):25, Node(abe):30, f:45]
4[Node(cd):25, Node(abe):30, f:45]Extract two smallest: Node(cd):25, Node(abe):30Node(cdabe):55[f:45, Node(cdabe):55]
5[f:45, Node(cdabe):55]Extract two smallest: f:45, Node(cdabe):55Node(root):100[Node(root):100]
6[Node(root):100]Only one node left, stop-[Node(root):100]
💡 Heap size is 1, Huffman tree is complete with root node frequency 100
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Heap[a:5, b:9, c:12, d:13, e:16, f:45][Node(ab):14, c:12, d:13, e:16, f:45][Node(ab):14, Node(cd):25, e:16, f:45][Node(cd):25, Node(abe):30, f:45][f:45, Node(cdabe):55][Node(root):100][Node(root):100]
New Node-Node(ab):14Node(cd):25Node(abe):30Node(cdabe):55Node(root):100-
Key Moments - 3 Insights
Why do we always pick the two smallest frequency nodes from the heap?
Picking the two smallest nodes ensures the tree has minimal weighted path length, which leads to optimal prefix codes. See execution_table steps 1-5 where smallest nodes are combined each time.
What happens when the heap size becomes 1?
When heap size is 1, it means the Huffman tree is fully built with a single root node. This is shown in execution_table step 6 where the process stops.
Why do we insert the new combined node back into the heap?
Inserting the new node back keeps the heap updated with combined frequencies so the next smallest pairs can be extracted. This is shown in execution_table after each new node creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what nodes are extracted from the heap?
ANode(ab):14 and e:16
Bc:12 and d:13
Ca:5 and b:9
Df:45 and Node(cdab e):55
💡 Hint
Check the 'Action' column at Step 3 in execution_table
At which step does the heap size become 1, ending the tree building?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look at the 'Exit Note' and Step 6 in execution_table
If the frequency of 'f' was smaller than 'e', how would the heap content change after Step 4?
ANode(abe):30 would be replaced by Node(abf)
BNode(cd):25 would be replaced by Node(cf)
CNode(cdab e):55 would have a smaller frequency
DHeap would contain f:45 and Node(cdab e):55 as before
💡 Hint
Consider how combining smallest nodes changes new node frequencies in variable_tracker
Concept Snapshot
Huffman Encoding builds a binary tree from character frequencies.
Use a min-heap to repeatedly combine two smallest nodes.
Each combined node frequency is sum of children.
Stop when one node remains: the root.
Traverse tree to assign binary codes (left=0, right=1).
Codes are prefix-free and optimal for compression.
Full Transcript
Huffman Encoding starts by counting how often each character appears. Then, it creates leaf nodes for each character with their frequencies. These nodes go into a min-heap. We repeatedly take out the two smallest nodes, combine them into a new node with frequency equal to their sum, and put this new node back into the heap. This continues until only one node remains, which is the root of the Huffman tree. Finally, we traverse the tree to assign binary codes to each character, where going left adds '0' and right adds '1'. This method produces the shortest possible codes for the characters based on their frequencies.