Title: Assignment 6: Huffman Code Generation
1Assignment 6 Huffman Code Generation
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Huffman Code
- Commonly used in compression schemes
- Idea
- Use variable-length code words
- Length of the word inversely proportional to the
frequency of its occurrences
3ASCII Encoding
- TALLAHASSEE
- ASCII values
- A 0100 0001
- E 0100 0101
- H 0100 1000
- L 0100 1100
- S 0101 0011
- T 0101 0100
4ASCII Encoding
- TALLAHASSEE
- 0101 0100 0100 0001 0100 1100 0100 1100 0100 0001
0100 1000 0100 0001 0101 0011 0101 0011 0100 0101
0100 0101 - 811 88 bits
-
5Huffman Encoding
- TALLAHASSEE
- A 10
- E 00
- H 010
- L 110
- S 111
- T 011
6Huffman Encoding
- TALLAHASSEE
- 011 10 110 110 10 010 10 111 111 00 00
- 28 bits
7Assignment 6
- Generate Huffman Code
- Frequency analysis
- Constructing the code tree
- Printing out the code table
8Frequency Analysis
- TALLAHASSEE
- A 3
- E 2
- H 1
- L 2
- S 2
- T 1
9Constructing the Code Tree
E2
A3
H1
L2
T1
S2
10Constructing the Code Tree
A3
L2
H1
T1
S2
E2
11Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
L2
T1
S2
E2
H1
12Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
L2
S2
E2
H1
T1
13Constructing the Code Tree
A3
L2
S2
E2
14Constructing the Code Tree
- Insert the parent node back to the heap
A3
L2
S2
E2
15Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
L2
S2
E2
16Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
L2
S2
E2
17Constructing the Code Tree
A3
L2
S2
18Constructing the Code Tree
- Insert the parent node back to the heap
A3
L2
S2
19Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
S2
L2
20Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
L2
S2
21Constructing the Code Tree
A3
4
E2
2
4
T1
L2
S2
H1
22Constructing the Code Tree
- Insert the parent node back to the heap
A3
4
E2
2
T1
H1
23Constructing the Code Tree
- Find two nodes with the smallest counter values
4
A3
E2
2
T1
H1
24Constructing the Code Tree
- Find two nodes with the smallest counter values
A3
25Constructing the Code Tree
7
4
A3
E2
2
T1
H1
26Constructing the Code Tree
- Insert the parent node back to the heap
27Constructing the Code Tree
- Find two nodes with the smallest counter values
28Constructing the Code Tree
- Find two nodes with the smallest counter values
4
E2
2
T1
H1
29Constructing the Code Tree
11
4
E2
2
T1
H1
30Constructing the Code Tree
- Insert the parent node back to the heap
11
4
E2
2
T1
H1
31Code Assignment
11
1
0
4
1
0
0
1
E2
2
0
1
1
00
0
10
T1
H1
010
011
110
111
32Properties of Huffman Code
- Higher the frequency, shorter the code
- The code for one character is not the prefix for
another code - 00 is not a prefix for 010
- 00 is a prefix for 001