🖨️ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1

Huffman Codes & Data Compression

2

Learning Goals

3

Data Compression

4

The Problem

5

Example

6

Variable-Length Codes

7

Prefix Codes

8

Ambiguity

9

Binary Tree Representation

10

Cost of Tree

11

Huffman's Algorithm

12

Greedy Intuition

13

Huffman(C)

n = |C|
Q = C  // Min-Priority Queue keyed by frequency
for i = 1 to n-1
  z = Allocate-Node()
  x = Extract-Min(Q)
  y = Extract-Min(Q)
  z.left = x
  z.right = y
  z.freq = x.freq + y.freq
  Insert(Q, z)
return Extract-Min(Q) // Root
14

Step-by-Step Construction

15

Step 2

16

Step 3

17

Step 4

18

Step 5

19

Time Complexity

20

Analysis

21

Proof of Correctness

22

Lemma 1: Interpretation

23

Lemma 2: Optimal Substructure

24

Properties

25

Full Binary Tree

26

Entropy

27

Decoding

28

How to Decode

29

Encoding/Decoding Table

30

Practice Problems

31

Fibonacci Frequencies

32

Wrap up

33

Common Pitfalls & Anti-Patterns

34

Watch Out For...

35

Interactive Demo

36

Huffman Tree Builder

37

Interactive Practice

38

Activity 1: Build Huffman Tree

39

Solution 1

40

Activity 2: Calculate Expected Length

41

Solution 2

42

Activity 3: Decode Huffman String

43

Solution 3

44

Activity 4: Fixed vs Variable Comparison

45

Solution 4

46

Lecture Summary

47

All Interactive Demos

48

Supplementary Resources