16. Huffman Coding

BST Review

Fixed Width Encoding Scheme

Character Decimal Binary
A 65 1000001
B 66 1000010
C 67 1000011
... ... ...
X 88 1011000
Y 89 1011001
Z 90 1011010

BST Usage: Huffman Coding

Overview

Example

"SUSIE SAYS IT IS EASY"

Frequency Table Count (Frequency)
S 6
Space 4
I 3
A 2
E 2
Y 2
T 1
U 1
Linefeed 1

Steps to create a Huffman Tree

  1. Create nodes that have two data items of each character and its frequency.
  2. Combine lowest two frequency nodes into a tree with a new parent with the sum of their frequencies.
  3. Combine lowest two frequency nodes, including the new node we just created, into a tree with a new parent with the sum of their frequencies.
  4. Repeat the previous step until we have one tree with all nodes are linked.
  5. Label all left branches (edges) with 0 and all right branches (edges) with 1.

Step 1

If(1) U(1) T(1) Y(2) E(2) A(2) I(3) SP(4) S(6)

Step 2

T(1) (2) Y(2) E(2) A(2) I(3) SP(4) S(6) / \ If(1) U(1)

Step 3

Y(2) E(2) A(2) (3) I(3) SP(4) S(6) / \ T(1) (2) / \ If(1) U(1)

Step 4

A(2) (3) I(3) (4) SP(4) S(6) / \ / \ T(1) (2) Y(2) E(2) / \ If(1) U(1)

Final Huffman Tree

Root(24) _________|_________ 0| 1| S(6) (18) _____|_____ 0| 1| SP(4) (14) ____|____ 0| 1| I(3) (11) ___|___ 0| 1| (5) (6) __|__ __|__ 0| 1| 0| 1| (3) A(2) E(2) Y(2) __|__ 0| 1| T(1) (2) __|__ 0| 1| U(1) Lf(1)
Character Huffman Code Count
S 0 6
Space 10 4
I 110 3
A 1110 2
E 11110 2
Y 111110 2
T 1111110 1
U 11111110 1
Linefeed 11111111 1

Comparison with Fixed Width Encoding

Encoding Method Total Number of Bits
Fixed width encoding $22 * 7 = 154$
Huffman coding $61 + 42 + 33 + 24 + 25 + 26 + 17 + 18 + 1*8 = 77$

Back to Home Next Lecture