Huffman coding is a lossless data compression algorithm. The idea is to assign variable-legth codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters. The most frequent character gets the smallest code and the least frequent character gets the largest code.

The variable-length codes assigned to input characters are Prefix Codes, means the codes (bit sequences) are assigned in such a way that the code assigned to one character is not prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding the generated bit stream.

Let us understand prefix codes with a counter example. Let there be four characters a, b, c and d, and their corresponding variable length codes be 00, 01, 0 and 1. This coding leads to ambiguity because code assigned to c is prefix of codes assigned to a and b. If the compressed bit stream is 0001, the de-compressed output may be “cccd” or “ccb” or “acd” or “ab”.

See this for applications of Huffman Coding.

There are mainly two major parts in Huffman Coding

  1. Build a Huffman Tree from input characters.
  2. Traverse the Huffman Tree and assign codes to characters.
[ad type=”banner”]

Steps to build Huffman Tree
Input is array of unique characters along with their frequency of occurrences and output is Huffman Tree.

1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min Heap is used as a priority queue. The value of frequency field is used to compare two nodes in min heap. Initially, the least frequent character is at root)

2. Extract two nodes with the minimum frequency from the min heap.

3. Create a new internal node with frequency equal to the sum of the two nodes frequencies. Make the first extracted node as its left child and the other extracted node as its right child. Add this node to the min heap.

4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root node and the tree is complete.

Let us understand the algorithm with an example:

character   Frequency
    a	        5
    b           9
    c           12
    d           13
    e           16
    f           45

Step 1. Build a min heap that contains 6 nodes where each node represents root of a tree with single node.

Step 2 Extract two minimum frequency nodes from min heap. Add a new internal node with frequency 5 + 9 = 14.

Huffman Coding
Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each, and one heap node is root of tree with 3 elements

[ad type=”banner”]
character           Frequency
       c               12
       d               13
 Internal Node         14
       e               16
       f                45

Step 3: Extract two minimum frequency nodes from heap. Add a new internal node with frequency 12 + 13 = 25

Huffman Coding

Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each, and two heap nodes are root of tree with more than one nodes.

character           Frequency
Internal Node          14
       e               16
Internal Node          25
       f               45

Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30
Huffman Coding
Now min heap contains 3 nodes.

character          Frequency
Internal Node         25
Internal Node         30
      f               45

Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55
Huffman Coding
Now min heap contains 2 nodes.

character     Frequency
       f         45
Internal Node    55

Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100
Huffman Coding
Now min heap contains only one node.

character      Frequency
Internal Node    100

Since the heap contains only one node, the algorithm stops here.

Steps to print codes from Huffman Tree:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to the left child, write 0 to the array. While moving to the right child, write 1 to the array. Print the array when a leaf node is encountered.

Huffman CodingThe codes are as follows:

character   code-word
    f          0
    c          100
    d          101
    a          1100
    b          1101
    e          111

[ad type=”banner”]

C Programming

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20Huffman%20Coding%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20This%20constant%20can%20be%20avoided%20by%20explicitly%20calculating%20height%20of%20Huffman%20Tree%0A%23define%20MAX_TREE_HT%20100%0A%20%0A%2F%2F%20A%20Huffman%20tree%20node%0Astruct%20MinHeapNode%0A%7B%0A%20%20%20%20char%20data%3B%20%20%2F%2F%20One%20of%20the%20input%20characters%0A%20%20%20%20unsigned%20freq%3B%20%20%2F%2F%20Frequency%20of%20the%20character%0A%20%20%20%20struct%20MinHeapNode%20*left%2C%20*right%3B%20%2F%2F%20Left%20and%20right%20child%20of%20this%20node%0A%7D%3B%0A%20%0A%2F%2F%20A%20Min%20Heap%3A%20%20Collection%20of%20min%20heap%20(or%20Hufmman%20tree)%20nodes%0Astruct%20MinHeap%0A%7B%0A%20%20%20%20unsigned%20size%3B%20%20%20%20%2F%2F%20Current%20size%20of%20min%20heap%0A%20%20%20%20unsigned%20capacity%3B%20%20%20%2F%2F%20capacity%20of%20min%20heap%0A%20%20%20%20struct%20MinHeapNode%20**array%3B%20%20%2F%2F%20Attay%20of%20minheap%20node%20pointers%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20allocate%20a%20new%20min%20heap%20node%20with%20given%20character%0A%2F%2F%20and%20frequency%20of%20the%20character%0Astruct%20MinHeapNode*%20newNode(char%20data%2C%20unsigned%20freq)%0A%7B%0A%20%20%20%20struct%20MinHeapNode*%20temp%20%3D%0A%20%20%20%20%20%20%20%20%20%20(struct%20MinHeapNode*)%20malloc(sizeof(struct%20MinHeapNode))%3B%0A%20%20%20%20temp-%3Eleft%20%3D%20temp-%3Eright%20%3D%20NULL%3B%0A%20%20%20%20temp-%3Edata%20%3D%20data%3B%0A%20%20%20%20temp-%3Efreq%20%3D%20freq%3B%0A%20%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20min%20heap%20of%20given%20capacity%0Astruct%20MinHeap*%20createMinHeap(unsigned%20capacity)%0A%7B%0A%20%20%20%20struct%20MinHeap*%20minHeap%20%3D%0A%20%20%20%20%20%20%20%20%20(struct%20MinHeap*)%20malloc(sizeof(struct%20MinHeap))%3B%0A%20%20%20%20minHeap-%3Esize%20%3D%200%3B%20%20%2F%2F%20current%20size%20is%200%0A%20%20%20%20minHeap-%3Ecapacity%20%3D%20capacity%3B%0A%20%20%20%20minHeap-%3Earray%20%3D%0A%20%20%20%20%20(struct%20MinHeapNode**)malloc(minHeap-%3Ecapacity%20*%20sizeof(struct%20MinHeapNode*))%3B%0A%20%20%20%20return%20minHeap%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20swap%20two%20min%20heap%20nodes%0Avoid%20swapMinHeapNode(struct%20MinHeapNode**%20a%2C%20struct%20MinHeapNode**%20b)%0A%7B%0A%20%20%20%20struct%20MinHeapNode*%20t%20%3D%20*a%3B%0A%20%20%20%20*a%20%3D%20*b%3B%0A%20%20%20%20*b%20%3D%20t%3B%0A%7D%0A%20%0A%2F%2F%20The%20standard%20minHeapify%20function.%0Avoid%20minHeapify(struct%20MinHeap*%20minHeap%2C%20int%20idx)%0A%7B%0A%20%20%20%20int%20smallest%20%3D%20idx%3B%0A%20%20%20%20int%20left%20%3D%202%20*%20idx%20%2B%201%3B%0A%20%20%20%20int%20right%20%3D%202%20*%20idx%20%2B%202%3B%0A%20%0A%20%20%20%20if%20(left%20%3C%20minHeap-%3Esize%20%26%26%0A%20%20%20%20%20%20%20%20minHeap-%3Earray%5Bleft%5D-%3Efreq%20%3C%20minHeap-%3Earray%5Bsmallest%5D-%3Efreq)%0A%20%20%20%20%20%20smallest%20%3D%20left%3B%0A%20%0A%20%20%20%20if%20(right%20%3C%20minHeap-%3Esize%20%26%26%0A%20%20%20%20%20%20%20%20minHeap-%3Earray%5Bright%5D-%3Efreq%20%3C%20minHeap-%3Earray%5Bsmallest%5D-%3Efreq)%0A%20%20%20%20%20%20smallest%20%3D%20right%3B%0A%20%0A%20%20%20%20if%20(smallest%20!%3D%20idx)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20swapMinHeapNode(%26minHeap-%3Earray%5Bsmallest%5D%2C%20%26minHeap-%3Earray%5Bidx%5D)%3B%0A%20%20%20%20%20%20%20%20minHeapify(minHeap%2C%20smallest)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20check%20if%20size%20of%20heap%20is%201%20or%20not%0Aint%20isSizeOne(struct%20MinHeap*%20minHeap)%0A%7B%0A%20%20%20%20return%20(minHeap-%3Esize%20%3D%3D%201)%3B%0A%7D%0A%20%0A%2F%2F%20A%20standard%20function%20to%20extract%20minimum%20value%20node%20from%20heap%0Astruct%20MinHeapNode*%20extractMin(struct%20MinHeap*%20minHeap)%0A%7B%0A%20%20%20%20struct%20MinHeapNode*%20temp%20%3D%20minHeap-%3Earray%5B0%5D%3B%0A%20%20%20%20minHeap-%3Earray%5B0%5D%20%3D%20minHeap-%3Earray%5BminHeap-%3Esize%20-%201%5D%3B%0A%20%20%20%20–minHeap-%3Esize%3B%0A%20%20%20%20minHeapify(minHeap%2C%200)%3B%0A%20%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20insert%20a%20new%20node%20to%20Min%20Heap%0Avoid%20insertMinHeap(struct%20MinHeap*%20minHeap%2C%20struct%20MinHeapNode*%20minHeapNode)%0A%7B%0A%20%20%20%20%2B%2BminHeap-%3Esize%3B%0A%20%20%20%20int%20i%20%3D%20minHeap-%3Esize%20-%201%3B%0A%20%20%20%20while%20(i%20%26%26%20minHeapNode-%3Efreq%20%3C%20minHeap-%3Earray%5B(i%20-%201)%2F2%5D-%3Efreq)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20minHeap-%3Earray%5Bi%5D%20%3D%20minHeap-%3Earray%5B(i%20-%201)%2F2%5D%3B%0A%20%20%20%20%20%20%20%20i%20%3D%20(i%20-%201)%2F2%3B%0A%20%20%20%20%7D%0A%20%20%20%20minHeap-%3Earray%5Bi%5D%20%3D%20minHeapNode%3B%0A%7D%0A%20%0A%2F%2F%20A%20standard%20funvtion%20to%20build%20min%20heap%0Avoid%20buildMinHeap(struct%20MinHeap*%20minHeap)%0A%7B%0A%20%20%20%20int%20n%20%3D%20minHeap-%3Esize%20-%201%3B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%20(n%20-%201)%20%2F%202%3B%20i%20%3E%3D%200%3B%20–i)%0A%20%20%20%20%20%20%20%20minHeapify(minHeap%2C%20i)%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20an%20array%20of%20size%20n%0Avoid%20printArr(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20printf(%22%25d%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Utility%20function%20to%20check%20if%20this%20node%20is%20leaf%0Aint%20isLeaf(struct%20MinHeapNode*%20root)%0A%7B%0A%20%20%20%20return%20!(root-%3Eleft)%20%26%26%20!(root-%3Eright)%20%3B%0A%7D%0A%20%0A%2F%2F%20Creates%20a%20min%20heap%20of%20capacity%20equal%20to%20size%20and%20inserts%20all%20character%20of%20%0A%2F%2F%20data%5B%5D%20in%20min%20heap.%20Initially%20size%20of%20min%20heap%20is%20equal%20to%20capacity%0Astruct%20MinHeap*%20createAndBuildMinHeap(char%20data%5B%5D%2C%20int%20freq%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20struct%20MinHeap*%20minHeap%20%3D%20createMinHeap(size)%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20size%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20minHeap-%3Earray%5Bi%5D%20%3D%20newNode(data%5Bi%5D%2C%20freq%5Bi%5D)%3B%0A%20%20%20%20minHeap-%3Esize%20%3D%20size%3B%0A%20%20%20%20buildMinHeap(minHeap)%3B%0A%20%20%20%20return%20minHeap%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20builds%20Huffman%20tree%0Astruct%20MinHeapNode*%20buildHuffmanTree(char%20data%5B%5D%2C%20int%20freq%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20struct%20MinHeapNode%20*left%2C%20*right%2C%20*top%3B%0A%20%0A%20%20%20%20%2F%2F%20Step%201%3A%20Create%20a%20min%20heap%20of%20capacity%20equal%20to%20size.%20%20Initially%2C%20there%20are%0A%20%20%20%20%2F%2F%20modes%20equal%20to%20size.%0A%20%20%20%20struct%20MinHeap*%20minHeap%20%3D%20createAndBuildMinHeap(data%2C%20freq%2C%20size)%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20while%20size%20of%20heap%20doesn’t%20become%201%0A%20%20%20%20while%20(!isSizeOne(minHeap))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Step%202%3A%20Extract%20the%20two%20minimum%20freq%20items%20from%20min%20heap%0A%20%20%20%20%20%20%20%20left%20%3D%20extractMin(minHeap)%3B%0A%20%20%20%20%20%20%20%20right%20%3D%20extractMin(minHeap)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Step%203%3A%20%20Create%20a%20new%20internal%20node%20with%20frequency%20equal%20to%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20sum%20of%20the%20two%20nodes%20frequencies.%20Make%20the%20two%20extracted%20node%20as%0A%20%20%20%20%20%20%20%20%2F%2F%20left%20and%20right%20children%20of%20this%20new%20node.%20Add%20this%20node%20to%20the%20min%20heap%0A%20%20%20%20%20%20%20%20%2F%2F%20’%24’%20is%20a%20special%20value%20for%20internal%20nodes%2C%20not%20used%0A%20%20%20%20%20%20%20%20top%20%3D%20newNode(‘%24’%2C%20left-%3Efreq%20%2B%20right-%3Efreq)%3B%0A%20%20%20%20%20%20%20%20top-%3Eleft%20%3D%20left%3B%0A%20%20%20%20%20%20%20%20top-%3Eright%20%3D%20right%3B%0A%20%20%20%20%20%20%20%20insertMinHeap(minHeap%2C%20top)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Step%204%3A%20The%20remaining%20node%20is%20the%20root%20node%20and%20the%20tree%20is%20complete.%0A%20%20%20%20return%20extractMin(minHeap)%3B%0A%7D%0A%20%0A%2F%2F%20Prints%20huffman%20codes%20from%20the%20root%20of%20Huffman%20Tree.%20%20It%20uses%20arr%5B%5D%20to%0A%2F%2F%20store%20codes%0Avoid%20printCodes(struct%20MinHeapNode*%20root%2C%20int%20arr%5B%5D%2C%20int%20top)%0A%7B%0A%20%20%20%20%2F%2F%20Assign%200%20to%20left%20edge%20and%20recur%0A%20%20%20%20if%20(root-%3Eleft)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20arr%5Btop%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20printCodes(root-%3Eleft%2C%20arr%2C%20top%20%2B%201)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Assign%201%20to%20right%20edge%20and%20recur%0A%20%20%20%20if%20(root-%3Eright)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20arr%5Btop%5D%20%3D%201%3B%0A%20%20%20%20%20%20%20%20printCodes(root-%3Eright%2C%20arr%2C%20top%20%2B%201)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20If%20this%20is%20a%20leaf%20node%2C%20then%20it%20contains%20one%20of%20the%20input%0A%20%20%20%20%2F%2F%20characters%2C%20print%20the%20character%20and%20its%20code%20from%20arr%5B%5D%0A%20%20%20%20if%20(isLeaf(root))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25c%3A%20%22%2C%20root-%3Edata)%3B%0A%20%20%20%20%20%20%20%20printArr(arr%2C%20top)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20builds%20a%20Huffman%20Tree%20and%20print%20codes%20by%20traversing%0A%2F%2F%20the%20built%20Huffman%20Tree%0Avoid%20HuffmanCodes(char%20data%5B%5D%2C%20int%20freq%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%2F%2F%20%20Construct%20Huffman%20Tree%0A%20%20%20struct%20MinHeapNode*%20root%20%3D%20buildHuffmanTree(data%2C%20freq%2C%20size)%3B%0A%20%0A%20%20%20%2F%2F%20Print%20Huffman%20codes%20using%20the%20Huffman%20tree%20built%20above%0A%20%20%20int%20arr%5BMAX_TREE_HT%5D%2C%20top%20%3D%200%3B%0A%20%20%20printCodes(root%2C%20arr%2C%20top)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20arr%5B%5D%20%3D%20%7B’a’%2C%20’b’%2C%20’c’%2C%20’d’%2C%20’e’%2C%20’f’%7D%3B%0A%20%20%20%20int%20freq%5B%5D%20%3D%20%7B5%2C%209%2C%2012%2C%2013%2C%2016%2C%2045%7D%3B%0A%20%20%20%20int%20size%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20HuffmanCodes(arr%2C%20freq%2C%20size)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

C++ Programming

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20Huffman%20Coding%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20Huffman%20tree%20node%0Astruct%20MinHeapNode%0A%7B%0A%20%20%20%20char%20data%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20One%20of%20the%20input%20characters%0A%20%20%20%20unsigned%20freq%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Frequency%20of%20the%20character%0A%20%20%20%20MinHeapNode%20*left%2C%20*right%3B%20%2F%2F%20Left%20and%20right%20child%0A%20%0A%20%20%20%20MinHeapNode(char%20data%2C%20unsigned%20freq)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20left%20%3D%20right%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20this-%3Edata%20%3D%20data%3B%0A%20%20%20%20%20%20%20%20this-%3Efreq%20%3D%20freq%3B%0A%20%20%20%20%7D%0A%7D%3B%0A%20%0A%2F%2F%20For%20comparison%20of%20two%20heap%20nodes%20(needed%20in%20min%20heap)%0Astruct%20compare%0A%7B%0A%20%20%20%20bool%20operator()(MinHeapNode*%20l%2C%20MinHeapNode*%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20(l-%3Efreq%20%3E%20r-%3Efreq)%3B%0A%20%20%20%20%7D%0A%7D%3B%0A%20%0A%2F%2F%20Prints%20huffman%20codes%20from%20the%20root%20of%20Huffman%20Tree.%0Avoid%20printCodes(struct%20MinHeapNode*%20root%2C%20string%20str)%0A%7B%0A%20%20%20%20if%20(!root)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20if%20(root-%3Edata%20!%3D%20’%24′)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20root-%3Edata%20%3C%3C%20%22%3A%20%22%20%3C%3C%20str%20%3C%3C%20%22%5Cn%22%3B%0A%20%0A%20%20%20%20printCodes(root-%3Eleft%2C%20str%20%2B%20%220%22)%3B%0A%20%20%20%20printCodes(root-%3Eright%2C%20str%20%2B%20%221%22)%3B%0A%7D%0A%20%0A%2F%2F%20The%20main%20function%20that%20builds%20a%20Huffman%20Tree%20and%0A%2F%2F%20print%20codes%20by%20traversing%20the%20built%20Huffman%20Tree%0Avoid%20HuffmanCodes(char%20data%5B%5D%2C%20int%20freq%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20struct%20MinHeapNode%20*left%2C%20*right%2C%20*top%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20min%20heap%20%26%20inserts%20all%20characters%20of%20data%5B%5D%0A%20%20%20%20priority_queue%3CMinHeapNode*%2C%20vector%3CMinHeapNode*%3E%2C%20compare%3E%20minHeap%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20size%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20minHeap.push(new%20MinHeapNode(data%5Bi%5D%2C%20freq%5Bi%5D))%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20while%20size%20of%20heap%20doesn’t%20become%201%0A%20%20%20%20while%20(minHeap.size()%20!%3D%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Extract%20the%20two%20minimum%20freq%20items%20from%20min%20heap%0A%20%20%20%20%20%20%20%20left%20%3D%20minHeap.top()%3B%0A%20%20%20%20%20%20%20%20minHeap.pop()%3B%0A%20%0A%20%20%20%20%20%20%20%20right%20%3D%20minHeap.top()%3B%0A%20%20%20%20%20%20%20%20minHeap.pop()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20new%20internal%20node%20with%20frequency%20equal%20to%20the%0A%20%20%20%20%20%20%20%20%2F%2F%20sum%20of%20the%20two%20nodes%20frequencies.%20Make%20the%20two%20extracted%0A%20%20%20%20%20%20%20%20%2F%2F%20node%20as%20left%20and%20right%20children%20of%20this%20new%20node.%20Add%0A%20%20%20%20%20%20%20%20%2F%2F%20this%20node%20to%20the%20min%20heap%0A%20%20%20%20%20%20%20%20%2F%2F%20’%24’%20is%20a%20special%20value%20for%20internal%20nodes%2C%20not%20used%0A%20%20%20%20%20%20%20%20top%20%3D%20new%20MinHeapNode(‘%24’%2C%20left-%3Efreq%20%2B%20right-%3Efreq)%3B%0A%20%20%20%20%20%20%20%20top-%3Eleft%20%3D%20left%3B%0A%20%20%20%20%20%20%20%20top-%3Eright%20%3D%20right%3B%0A%20%20%20%20%20%20%20%20minHeap.push(top)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Print%20Huffman%20codes%20using%20the%20Huffman%20tree%20built%20above%0A%20%20%20%20printCodes(minHeap.top()%2C%20%22%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20arr%5B%5D%20%3D%20%7B%20’a’%2C%20’b’%2C%20’c’%2C%20’d’%2C%20’e’%2C%20’f’%20%7D%3B%0A%20%20%20%20int%20freq%5B%5D%20%3D%20%7B%205%2C%209%2C%2012%2C%2013%2C%2016%2C%2045%20%7D%3B%0A%20%20%20%20int%20size%20%3D%20sizeof(arr)%20%2F%20sizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20HuffmanCodes(arr%2C%20freq%2C%20size)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Aditya%20Goel” message=”” highlight=”” provider=”manual”/]

 

f: 0
c: 100
d: 101
a: 1100
b: 1101
e: 111

Time complexity: O(nlogn) where n is the number of unique characters. If there are n nodes, extractMin() is called 2*(n – 1) times. extractMin() takes O(logn) time as it calles minHeapify(). So, overall complexity is O(nlogn).

If the input array is sorted, there exists a linear time algorithm. We will soon be discussing in our next post.

Categorized in: