Huffman Coding is a lossless data compression algorithm where each character in the data is assigned a variable length prefix code. The least frequent character gets the largest code and the most frequent one gets the smallest code. Encoding the data using this technique is very easy and efficient. However, decoding the bitstream generated using this technique is inefficient.Decoders(or Decompressors)require the knowledge of the encoding mechanism used in order to decode the encoded data back to the original characters. Hence information about the encoding process needs to be passed to the decoder along with the encoded data as a table of characters and their corresponding codes. In regular Huffman coding of a large data, this table takes up a lot of memory space and also if a large no. of unique characters are present in the data then the compressed(or encoded) data size increases because of the presence of the codebook. Therefore to make the decoding process computationally efficient and still maintain a good compression ratio, Canonical Huffman codes were introduced.

In Canonical Huffman coding, the bit lengths of the standard Huffman codes generated for each symbol is used. The symbols are sorted first according to their bit lengths in non-decreasing order and then for each bit length, they are sorted lexicographically. The first symbol gets a code containing all zeros and of the same length as that of the original bit length. For the subsequent symbols, if the symbol has a bit length equal to that of the previous symbol, then the code of the previous symbol is

incremented by oneand assigned to the present symbol. Otherwise, if the symbol has a bit length greater than that of the previous symbol, afterincrementingthe code of the previous symbol iszeros are appended until the length becomes equal to the bit length of the current symboland the code is then assigned to the current symbol.

This process continues for the rest of the symbols.

**The following example illustrates the process:**

**Consider the following data:**

Character | Frequency |
---|---|

a | 10 |

b | 1 |

c | 15 |

d | 7 |

**Standard Huffman Codes Generated with bit lengths:**

Character | Huffman Codes | Bit lengths |
---|---|---|

a | 11 | 2 |

b | 100 | 3 |

c | 0 | 1 |

d | 101 | 3 |

**Step 1:** Sort the data according to bit lengths and then for each bit length sort the symbols lexicographically.

Character | Bit lenghts |
---|---|

c | 1 |

a | 2 |

b | 3 |

d | 3 |

**Step 2:** Assign the code of the first symbol with the same number of ‘0’s as the bit length.

Code for ‘c’:0

Next symbol ‘a’ has bit length 2 > bit length of the previous symbol ‘c’ which is 1.Increment the code of the previous symbol by 1 and append (2-1)=1 zeros and assign the code to ‘a’.

Code for ‘a’:10

Next symbol ‘b’ has bit length 3 > bit length of the previous symbol ‘a’ which is 2.Increment the code of the previous symbol by 1 and append (3-2)=1 zeros and assign the code to ‘b’.

Code for ‘b’:110

Next symbol ‘d’ has bit length 3 = bit length of the previous symbol ‘b’ which is 3.Increment the code of the previous symbol by 1 and assign it to ‘d’.

Code for ‘d’:111

**Step 3:** Final result.

Character | Canonical Huffman Codes |
---|---|

c | 0 |

a | 10 |

b | 110 |

d | 111 |

The basic advantage of this method is that the encoding information passed to the decoder can be made more compact and memory efficient. For example, one can simply pass the bit lengths of the characters or symbols to the decoder. The canonical codes can be generated easily from the lengths as they are sequential.

For generating Huffman codes uisng Huffman Tree refer here.

**Approach:** A simple and efficient approach is to generate a Huffman tree for the data and use a data structure similar to TreeMap in java to store the symbols and bit lengths such that the information always remains sorted. The canonical codes can then be obtained using incrementation and bitwise left shift operations.

`// Java Program for Canonical Huffman Encoding ` ` ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `// Nodes of Huffman tree ` `class` `Node { ` ` ` ` ` `int` `data; ` ` ` `char` `c; ` ` ` ` ` `Node left; ` ` ` `Node right; ` `} ` ` ` `// comparator class helps to compare the node ` `// on the basis of one of its attribute. ` `// Here we will be compared ` `// on the basis of data values of the nodes. ` `class` `Pq_compare ` `implements` `Comparator<Node> { ` ` ` `public` `int` `compare(Node a, Node b) ` ` ` `{ ` ` ` ` ` `return` `a.data - b.data; ` ` ` `} ` `} ` ` ` `class` `Canonical_Huffman { ` ` ` ` ` `// Treemap to store the ` ` ` `// code lengths(sorted) as keys ` ` ` `// and corresponding(sorted) ` ` ` `// set of characters as values ` ` ` `static` `TreeMap<Integer, TreeSet<Character> > data; ` ` ` ` ` `// Constructor to initialize the Treemap ` ` ` `public` `Canonical_Huffman() ` ` ` `{ ` ` ` `data = ` `new` `TreeMap<Integer, TreeSet<Character> >(); ` ` ` `} ` ` ` ` ` `// Recursive function ` ` ` `// to generate code lengths ` ` ` `// from regular Huffman codes ` ` ` `static` `void` `code_gen(Node root, ` `int` `code_length) ` ` ` `{ ` ` ` `if` `(root == ` `null` `) ` ` ` `return` `; ` ` ` ` ` `// base case; if the left and right are null ` ` ` `// then its a leaf node. ` ` ` `if` `(root.left == ` `null` `&& root.right == ` `null` `) { ` ` ` ` ` `// check if key is present or not. ` ` ` `// If not present add a new treeset ` ` ` `// as value along with the key ` ` ` `data.putIfAbsent(code_length, ` `new` `TreeSet<Character>()); ` ` ` ` ` `// c is the character in the node ` ` ` `data.get(code_length).add(root.c); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Add 1 when on going left or right. ` ` ` `code_gen(root.left, code_length + ` `1` `); ` ` ` `code_gen(root.right, code_length + ` `1` `); ` ` ` `} ` ` ` ` ` `static` `void` `testCanonicalHC(` `int` `n, ` `char` `chararr[], ` `int` `freq[]) ` ` ` `{ ` ` ` ` ` `// min-priority queue(min-heap). ` ` ` `PriorityQueue<Node> q ` ` ` `= ` `new` `PriorityQueue<Node>(n, ` `new` `Pq_compare()); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// creating a node object ` ` ` `// and adding it to the priority-queue. ` ` ` `Node node = ` `new` `Node(); ` ` ` ` ` `node.c = chararr[i]; ` ` ` `node.data = freq[i]; ` ` ` ` ` `node.left = ` `null` `; ` ` ` `node.right = ` `null` `; ` ` ` ` ` `// add functions adds ` ` ` `// the node to the queue. ` ` ` `q.add(node); ` ` ` `} ` ` ` ` ` `// create a root node ` ` ` `Node root = ` `null` `; ` ` ` ` ` `// extract the two minimum value ` ` ` `// from the heap each time until ` ` ` `// its size reduces to 1, extract until ` ` ` `// all the nodes are extracted. ` ` ` `while` `(q.size() > ` `1` `) { ` ` ` ` ` `// first min extract. ` ` ` `Node x = q.peek(); ` ` ` `q.poll(); ` ` ` ` ` `// second min extarct. ` ` ` `Node y = q.peek(); ` ` ` `q.poll(); ` ` ` ` ` `// new node f which is equal ` ` ` `Node nodeobj = ` `new` `Node(); ` ` ` ` ` `// to the sum of the frequency of the two nodes ` ` ` `// assigning values to the f node. ` ` ` `nodeobj.data = x.data + y.data; ` ` ` `nodeobj.c = ` `'-'` `; ` ` ` ` ` `// first extracted node as left child. ` ` ` `nodeobj.left = x; ` ` ` ` ` `// second extracted node as the right child. ` ` ` `nodeobj.right = y; ` ` ` ` ` `// marking the f node as the root node. ` ` ` `root = nodeobj; ` ` ` ` ` `// add this node to the priority-queue. ` ` ` `q.add(nodeobj); ` ` ` `} ` ` ` ` ` `// Creating a canonical Huffman object ` ` ` `Canonical_Huffman obj = ` `new` `Canonical_Huffman(); ` ` ` ` ` `// generate code lengths by traversing the tree ` ` ` `code_gen(root, ` `0` `); ` ` ` ` ` `// Object array to store the keys ` ` ` `Object[] arr = data.keySet().toArray(); ` ` ` ` ` `// Set initial canonical code=0 ` ` ` `int` `c_code = ` `0` `, curr_len = ` `0` `, next_len = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) { ` ` ` `Iterator it = data.get(arr[i]).iterator(); ` ` ` ` ` `// code length of current character ` ` ` `curr_len = (` `int` `)arr[i]; ` ` ` ` ` `while` `(it.hasNext()) { ` ` ` ` ` `// Display the canonical codes ` ` ` `System.out.println(it.next() + ` `":"` ` ` `+ Integer.toBinaryString(c_code)); ` ` ` ` ` `// if values set is not ` ` ` `// completed or if it is ` ` ` `// the last set set code length ` ` ` `// of next character as current ` ` ` `// code length ` ` ` `if` `(it.hasNext() || i == arr.length - ` `1` `) ` ` ` `next_len = curr_len; ` ` ` `else` ` ` `next_len = (` `int` `)arr[i + ` `1` `]; ` ` ` ` ` `// Generate canonical code ` ` ` `// for next character using ` ` ` `// regular code length of next ` ` ` `// character ` ` ` `c_code = (c_code + ` `1` `) << (next_len - curr_len); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` `throws` `IOException ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `char` `[] chararr = { ` `'a'` `, ` `'b'` `, ` `'c'` `, ` `'d'` `}; ` ` ` `int` `[] freq = { ` `10` `, ` `1` `, ` `15` `, ` `7` `}; ` ` ` `testCanonicalHC(n, chararr, freq); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

c:0 a:10 b:110 d:111

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Huffman Coding | Greedy Algo-3
- Efficient Huffman Coding for Sorted Input | Greedy Algo-4
- Image Compression using Huffman Coding
- Huffman Decoding
- Practice Questions on Huffman Encoding
- TCS Coding Practice Question | Reverse a String
- TCS Coding Practice Question | Palindrome String
- TCS Coding Practice Question | Check Palindrome Number
- TCS Coding Practice Question | Reverse a Number
- TCS Coding Practice Question | Sum of Digits of a number
- TCS Coding Practice Question | Checking Leap Year
- TCS Coding Practice Question | Checking Prime Number
- TCS Coding Practice Question | Prime Numbers upto N
- TCS Coding Practice Question | Greatest of 3 Numbers
- TCS Coding Practice Question | Concatenate 2 Strings
- TCS Coding Practice Question | Swap two Numbers
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- TCS Coding Practice Question | Average of 2 Numbers
- TCS Coding Practice Question | Factorial of a Number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.