0
0
DSA Cprogramming~10 mins

Huffman Encoding in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Huffman Encoding
Count frequency of each char
Create leaf nodes for each char
Build min-heap of leaf nodes
While more than one node in heap
Extract two smallest nodes
Create new internal node with sum freq
Insert new node back to heap
Heap has one node -> root of Huffman tree
Traverse tree to assign codes
Encode input using codes
The flow shows counting frequencies, building a min-heap, merging nodes to build the tree, then assigning codes.
Execution Sample
DSA C
char input[] = "abbccc";
// Frequencies: a=1, b=2, c=3
// Build tree and assign codes
// a: 10, b: 11, c: 0
// Encode input
This code counts frequencies of characters, builds Huffman tree, assigns codes, and encodes the input string.
Execution Table
StepActionHeap Content (freq:char)New Node CreatedHeap After InsertionNotes
1Count frequencies---a=1, b=2, c=3
2Create leaf nodes1:a, 2:b, 3:c--Leaf nodes created for each char
3Build min-heap1:a, 2:b, 3:c--Min-heap built with leaf nodes
4Extract two smallest1:a, 2:b, 3:c--Extracted 1:a and 2:b
5Create internal node-3:internal-New node freq=1+2=3
6Insert new node3:c, 3:internal-3:c, 3:internalHeap now has two nodes with freq=3
7Extract two smallest3:c, 3:internal--Extracted 3:c and 3:internal
8Create internal node-6:root-New node freq=3+3=6 root
9Insert new node6:root-6:rootHeap has one node, tree complete
10Assign codes---c=0, a=10, b=11
11Encode input---Input 'abbccc' encoded as 10 11 11 0 0 0
12End---Encoding complete
💡 Heap reduced to one node, Huffman tree built and encoding done
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9Final
freq_a01111
freq_b02222
freq_c03333
heapempty1:a,2:b,3:c3:c,3:internal6:root6:root
codesemptyemptyemptyc=0,a=10,b=11c=0,a=10,b=11
encoded_stringemptyemptyemptyempty10 11 11 0 0 0
Key Moments - 3 Insights
Why do we combine the two smallest frequency nodes first?
Combining smallest nodes first ensures the tree has minimal weighted path length, as shown in steps 4-8 where nodes with freq 1 and 2 are combined before others.
How do we know when the Huffman tree is complete?
When the heap has only one node left (step 9), that node is the root of the Huffman tree, so the tree is complete.
Why does character 'c' get the shortest code '0'?
Because 'c' has the highest frequency (3), it is placed closer to the root, resulting in a shorter code, as assigned in step 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, what is the frequency of the new internal node created?
A3
B2
C1
D6
💡 Hint
Check the 'New Node Created' column at step 5 in the execution_table.
At which step does the heap reduce to a single node?
AStep 6
BStep 9
CStep 4
DStep 11
💡 Hint
Look at the 'Heap After Insertion' column and the notes in the execution_table.
According to the variable_tracker, what is the encoded string after the final step?
A"10 11 11 0 0 0"
B"0 10 11 11 0 0"
C"11 10 0 0 0 11"
D"0 0 0 10 11 11"
💡 Hint
Check the 'encoded_string' row in the variable_tracker at the 'Final' column.
Concept Snapshot
Huffman Encoding builds a binary tree based on character frequencies.
1. Count frequencies.
2. Create leaf nodes and build a min-heap.
3. Merge two smallest nodes repeatedly until one node remains.
4. Assign codes by traversing the tree (left=0, right=1).
5. Encode input using these codes for compression.
Full Transcript
Huffman Encoding starts by counting how often each character appears. Then, each character becomes a leaf node with its frequency. These nodes go into a min-heap to always pick the smallest frequencies. We repeatedly take two smallest nodes, merge them into a new node with combined frequency, and put it back. When only one node remains, it is the root of the Huffman tree. We assign codes by going left (0) or right (1) in the tree. Characters with higher frequency get shorter codes. Finally, we encode the input string using these codes. This process compresses data efficiently.