Huffman coding

Vicente González Ruiz

December 14, 2022

Contents

 1 Basics
 2 Building Huffman trees
  2.1 Example
 3 Encoder [2]
  3.1 Example
 4 Decoder
  4.1 Example
 5 Limits
 References

1 Basics

2 Building Huffman trees

  1. Create a list of nodes. Each node stores a symbol and its probability.
  2. While the number of nodes in the list > 1:

    1. Extract from the list the 2 nodes with the lowest probability.
    2. Insert in the list a new node (that is the root of a binary tree) whose probability is the sum of the probability of its leafs.

2.1 Example

3 Encoder [2]

  1. n := |C|;
  2. Q := C;
  3. for i := 1 to n-1 do

    1. allocate a new node z
    2. z.left := x := Extract-min(Q);
    3. z.right := y := Extract-min(Q);
    4. z.freq := x.freq + y.freq;
    5. Insert(Q,z);
  4. end for
  5. return Extract-min(Q); {return the root of the tree}

3.1 Example

String: BACADAEAFABBAAAGAH

A B C D E F G H
Frequency (in thousands) 50 20 5 5 5 5 5 5
Fixed-length codeword 000 001 010 011 100 101 110 111
Variable-length codeword 0 100 1010 1011 1100 1101 1110 1111

OUTPUT: 10001010010110110001101010010000111001111

4 Decoder

  1. prob_table := freq //Array with the frequency of occurrence of each char
  2. sec := str // Output that we want to rebuild
  3. tree := array //A key-value associating array, with each item’s key the node in that subtree and value its current sum probability, according to prob_table
  4. code_table := table //An array with length of the how many characters in prob_table, wich maps each code to its corresponding decoded character with each code initialised as empty strings.
  5. while more than one node in tree do sub_nodes := key of node with smallest probability in tree sec_sub_nodes = key of := key of node with second smallest propability in tree

    1. for node in sub_nodes do

      1. code_table[node].key := “1” + code_table[node].key;
    2. end for
    3. for node in sec_sub_nodes do

      1. code_table[node].key = “0” + code_table[node].key
    4. end for
    5. tree[sub_nodes + sec_sub_nodes] := tree[sub_nodes] + tree[sec_sub_nodes]
    6. delete tree[sub_nodes]
    7. delete tree[sec_sub_nodes]
  6. end while
  7. temp := ""
  8. result := ""
  9. for code in seq do

    1. temp := temp + code
    2. if temp in code_table then

      1. result := result + code_table[temp]
      2. temp := ""
    3. end if
  10. end for
  11. return result

4.1 Example

INPUT: BACADAEAFABBAAAGAH

5 Limits

[1]   David A Huffman. A method for the construction of minimum-redundancy codes. Proceedings of the Institute of Radio Engineers (IRE), 40(9):1098–1101, 1952.

[2]   Nelson M. and Gailly J. The Data Compression Book. M&T Books, 1996.