Assignment 6: Huffman Code Generation Andy Wang Data Structures, Algorithms, and Generic Programming
Huffman Code Commonly used in compression schemes Idea: Use variable-length code words Length of the word inversely proportional to the frequency of its occurrences
ASCII Encoding TALLAHASSEE ASCII values A 0100 0001 E 0100 0101
ASCII Encoding TALLAHASSEE 0101 0100 0100 0001 0100 1100 0100 1100 0100 0001 0100 1000 0100 0001 0101 0011 0101 0011 0100 0101 0100 0101 8*11 = 88 bits
Huffman Encoding TALLAHASSEE A 10 E 00 H 010 L 110 S 111 T 011
Huffman Encoding TALLAHASSEE 011 10 110 110 10 010 10 111 111 00 00 28 bits
Assignment 6 Generate Huffman Code Frequency analysis Constructing the code tree Printing out the code table
Frequency Analysis TALLAHASSEE A 3 E 2 H 1 L 2 S 2 T 1
Constructing the Code Tree Create tree nodes E:2 A:3 H:1 L:2 T:1 S:2
Constructing the Code Tree Build a modified heap H:1 L:2 T:1 A:3 E:2 S:2
Constructing the Code Tree Find two nodes with the smallest counter values T:1 L:2 S:2 A:3 E:2 H:1
Constructing the Code Tree Find two nodes with the smallest counter values E:2 L:2 S:2 A:3 H:1 T:1
Constructing the Code Tree Create a parent node E:2 L:2 S:2 A:3 H:1 T:1 :2
Constructing the Code Tree Insert the parent node back to the heap E:2 L:2 S:2 H:1 T:1 :2 A:3
Constructing the Code Tree Find two nodes with the smallest counter values H:1 T:1 :2 L:2 S:2 A:3 E:2
Constructing the Code Tree Find two nodes with the smallest counter values L:2 A:3 S:2 E:2 H:1 T:1 :2
Constructing the Code Tree Create a parent node L:2 A:3 S:2 E:2 H:1 T:1 :2 :4
Constructing the Code Tree Insert the parent node back to the heap L:2 A:3 E:2 H:1 T:1 :2 :4 S:2
Constructing the Code Tree Find two nodes with the smallest counter values S:2 E:2 H:1 T:1 :2 :4 A:3 L:2
Constructing the Code Tree Find two nodes with the smallest counter values E:2 H:1 T:1 :2 :4 A:3 L:2 S:2
Constructing the Code Tree Create a parent node A:3 :4 E:2 :2 :4 H:1 T:1 L:2 S:2
Constructing the Code Tree Insert the parent node back to the heap L:2 S:2 :4 A:3 :4 E:2 :2 H:1 T:1
Constructing the Code Tree Find two nodes with the smallest counter values L:2 S:2 :4 :4 A:3 E:2 :2 H:1 T:1
Constructing the Code Tree Find two nodes with the smallest counter values E:2 H:1 T:1 :2 :4 A:3 L:2 S:2 :4
Constructing the Code Tree Create a parent node :7 :4 A:3 L:2 S:2 :4 E:2 :2 H:1 T:1
Constructing the Code Tree Insert the parent node back to the heap A:3 L:2 S:2 :4 :7 E:2 H:1 T:1 :2 :4
Constructing the Code Tree Find two nodes with the smallest counter values E:2 H:1 T:1 :2 :4 A:3 L:2 S:2 :4 :7
Constructing the Code Tree Find two nodes with the smallest counter values :4 A:3 L:2 S:2 :4 :7 E:2 :2 H:1 T:1
Constructing the Code Tree Create a parent node :11 :4 A:3 L:2 S:2 :4 :7 E:2 :2 H:1 T:1
Constructing the Code Tree Insert the parent node back to the heap :11 :4 A:3 L:2 S:2 :4 :7 E:2 :2 H:1 T:1
Code Assignment :11 1 :4 A:3 L:2 S:2 :4 :7 1 1 E:2 :2 00 1 10 1 H:1 :4 A:3 L:2 S:2 :4 :7 1 1 E:2 :2 00 1 10 1 H:1 T:1 010 011 110 111
Properties 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