Huffman Trees and Codes-V1
Huffman Trees and Codes-V1
Codes
Greedy Technique
What is Encoding?
• Encoding a text means assigning some sequence of bits called the
codeword to each of the text’s symbols.
• Fixed-length encoding assigns to each symbol a bit string of the same
length. Eg. ASCII code.
• Variable-length encoding assigns codewords of different lengths to
different symbols.
• This idea was used in the telegraph code invented in the mid-19th
century by Samuel Morse. In that code, frequent letters such as e (.)
and a (.−) are assigned short sequences of dots and dashes while
infrequent letters such as q (−−.−) and z (−−..) have longer ones.
Problem in variable-length encoding
• How many bits of an encoded text represent the first (or, more
generally, the ith) symbol?
• Prefix-free (or simply prefix) codes avoid this complication.
• In a prefix code, no codeword is a prefix of a codeword of another
symbol. Hence, with such an encoding, scan a bit string until we get
the first group of bits that is a codeword for some symbol, replace
these bits by this symbol, and repeat this operation until the bit
string’s end is reached.
Huffman’s algorithm
• Huffman’s algorithm generates Huffman codes by constructing Huffman tree.
Huffman codes has shorter codewords to more frequent symbols and longer
codewords to less frequent symbols
Steps for constructing Huffman tree:
• Step 1 Initialize n one-node trees and label them with the symbols of the
alphabet given. Record the frequency of each symbol in its tree’s root to indicate
the tree’s weight. (More generally, the weight of a tree will be equal to the sum
of the frequencies in the tree’s leaves.)
• Step 2 Repeat the following operation until a single tree is obtained. Find two
trees with the smallest weight (ties can be broken arbitrarily). Make them the left
and right subtree of a new tree and record the sum of their weights in the root of
the new tree as its weight.
Example of constructing a Huffman
coding tree
Example of constructing a Huffman
coding tree
Example of constructing a Huffman
coding tree
Example of constructing a Huffman
coding tree
Example of constructing a Huffman
coding tree
Huffman codes for the example
Huffman encoding and decoding
• Huffman codes are prefix codes.