{"id":25056,"date":"2017-05-27T18:37:10","date_gmt":"2017-05-27T13:07:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25056"},"modified":"2017-05-27T18:37:10","modified_gmt":"2017-05-27T13:07:10","slug":"huffman-coding","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/huffman-coding\/","title":{"rendered":"Huffman Coding"},"content":{"rendered":"<p>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.<span id=\"more-26851\"><\/span> The most frequent character gets the smallest code and the least frequent character gets the largest code.<\/p>\n<p>The variable-length codes assigned to input characters are <a href=\"https:\/\/www.wikitechy.com\/technology\/activity-selection-problem\/\">Prefix Codes<\/a>, 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.<\/p>\n<p>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 \u201ccccd\u201d or \u201cccb\u201d or \u201cacd\u201d or \u201cab\u201d.<\/p>\n<p>See this for applications of Huffman Coding.<\/p>\n<p>There are mainly two major parts in Huffman Coding<\/p>\n<ol>\n<li>Build a Huffman Tree from input characters.<\/li>\n<li>Traverse the Huffman Tree and assign codes to characters.<\/li>\n<\/ol>\n[ad type=\u201dbanner\u201d]\n<h4 id=\"steps-to-build-huffman-treeinput-is-array-of-unique-characters-along-with-their-frequency-of-occurrences-and-output-is-huffman-tree\"><strong>Steps to build Huffman Tree<\/strong><br \/>\nInput is array of unique characters along with their frequency of occurrences and output is Huffman Tree.<\/h4>\n<p><strong>1.<\/strong> 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)<\/p>\n<p><strong>2.<\/strong> Extract two nodes with the minimum frequency from the min heap.<\/p>\n<p><strong>3.<\/strong> 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.<\/p>\n<p><strong>4.<\/strong> Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root node and the tree is complete.<\/p>\n<p>Let us understand the algorithm with an example:<\/p>\n<pre>character   Frequency\r\n    a\t        5\r\n    b           9\r\n    c           12\r\n    d           13\r\n    e           16\r\n    f           45<\/pre>\n<p><strong>Step 1.<\/strong> Build a min heap that contains 6 nodes where each node represents root of a tree with single node.<\/p>\n<p><strong>Step 2<\/strong> Extract two minimum frequency nodes from min heap. Add a new internal node with frequency 5 + 9 = 14.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-25078 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-1-1.png\" alt=\"Huffman Coding\" width=\"300\" height=\"129\" \/><br \/>\nNow 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<\/p>\n[ad type=\u201dbanner\u201d]\n<pre>character           Frequency\r\n       c               12\r\n       d               13\r\n Internal Node         14\r\n       e               16\r\n       f                45\r\n<\/pre>\n<p><strong>Step 3:<\/strong> Extract two minimum frequency nodes from heap. Add a new internal node with frequency 12 + 13 = 25<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25068\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-2.png\" alt=\"Huffman Coding\" width=\"300\" height=\"132\" \/><\/p>\n<p>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.<\/p>\n<pre>character           Frequency\r\nInternal Node          14\r\n       e               16\r\nInternal Node          25\r\n       f               45\r\n<\/pre>\n<p><strong>Step 4:<\/strong> Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30<br \/>\n<img decoding=\"async\" class=\"aligncenter size-full wp-image-25070\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-3.png\" alt=\"Huffman Coding\" width=\"300\" height=\"162\" \/><br \/>\nNow min heap contains 3 nodes.<\/p>\n<pre>character          Frequency\r\nInternal Node         25\r\nInternal Node         30\r\n      f               45<\/pre>\n<p><strong>Step 5:<\/strong> Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25074\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-4.png\" alt=\"Huffman Coding\" width=\"300\" height=\"121\" \/><br \/>\nNow min heap contains 2 nodes.<\/p>\n<pre>character     Frequency\r\n       f         45\r\nInternal Node    55<\/pre>\n<p><strong>Step 6:<\/strong> Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25073\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-5.png\" alt=\"Huffman Coding\" width=\"300\" height=\"167\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-5.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-5-215x120.png 215w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><br \/>\nNow min heap contains only one node.<\/p>\n<pre>character      Frequency\r\nInternal Node    100<\/pre>\n<p>Since the heap contains only one node, the algorithm stops here.<\/p>\n<p><strong>Steps to print codes from Huffman Tree:<\/strong><br \/>\nTraverse 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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-25065\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-1.jpg\" alt=\"Huffman Coding\" width=\"300\" height=\"167\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-1.jpg 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Huffman-Coding-1-215x120.jpg 215w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>The codes are as follows:<\/p>\n<pre>character   code-word\r\n    f          0\r\n    c          100\r\n    d          101\r\n    a          1100\r\n    b          1101\r\n    e          111<\/pre>\n<h4 id=\"ad-typebanner\">[ad type=\u201dbanner\u201d]<\/h4>\n<h4 id=\"c-programming\"><strong>C Programming<\/strong><\/h4>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u2013minHeap-%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\u2013i)%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\u2019t%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\u2019%24\u2019%20is%20a%20special%20value%20for%20internal%20nodes%2C%20not%20used%0A%20%20%20%20%20%20%20%20top%20%3D%20newNode(\u2018%24\u2019%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\u2019a\u2019%2C%20\u2019b\u2019%2C%20\u2019c\u2019%2C%20\u2019d\u2019%2C%20\u2019e\u2019%2C%20\u2019f\u2019%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\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<div class=\"responsive-tabs-wrapper\">\n<div class=\"responsive-tabs responsive-tabs--enabled\">\n<div id=\"tablist1-panel1\" class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\n<h4 id=\"c-programming-2\" class=\"syntaxhighlighter nogutter c\"><strong>C++\u00a0<\/strong><strong>Programming<\/strong><\/h4>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%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\u2019%24\u2032)%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\u2019t%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\u2019%24\u2019%20is%20a%20special%20value%20for%20internal%20nodes%2C%20not%20used%0A%20%20%20%20%20%20%20%20top%20%3D%20new%20MinHeapNode(\u2018%24\u2019%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\u2019a\u2019%2C%20\u2019b\u2019%2C%20\u2019c\u2019%2C%20\u2019d\u2019%2C%20\u2019e\u2019%2C%20\u2019f\u2019%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\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n<pre>f: 0\r\nc: 100\r\nd: 101\r\na: 1100\r\nb: 1101\r\ne: 111<\/pre>\n<p><strong>Time complexity:<\/strong> O(nlogn) where n is the number of unique characters. If there are n nodes, extractMin() is called 2*(n \u2013 1) times. extractMin() takes O(logn) time as it calles minHeapify(). So, overall complexity is O(nlogn).<\/p>\n<p>If the input array is sorted, there exists a linear time algorithm. We will soon be discussing in our next post.<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Huffman Coding &#8211; Greedy Algorithm &#8211; Huffman coding is a lossless data compression algorithm. The idea is to assign variable-legth codes to input characters.<\/p>\n","protected":false},"author":1,"featured_media":25368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70144],"tags":[71082,71090,73006,71079,71063,73004,71083,71085,73009,73005,73007,71089,71100,71107,71086,73008,71080,71088,73010,71118,71115,71095,71087,71099,71106,71111,71073,71064,71102,71094],"class_list":["post-25056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-greedy-algorithm","tag-application-of-huffman-coding-in-data-compression","tag-c-program-to-implement-huffman-code","tag-huffman-code-generator","tag-huffman-coding-algorithm-in-c","tag-huffman-coding-c-program","tag-huffman-coding-c","tag-huffman-coding-example","tag-huffman-coding-example-pdf","tag-huffman-coding-example-with-probabilities","tag-huffman-coding-in-c","tag-huffman-coding-java","tag-huffman-coding-program-in-c","tag-huffman-coding-program-in-c-with-output","tag-huffman-coding-program-in-java","tag-huffman-coding-steps","tag-huffman-coding-tutorial","tag-huffman-coding-with-example","tag-huffman-compression-algorithm","tag-huffman-decoding","tag-huffman-encoding-and-decoding-example-in-java","tag-huffman-encoding-tree","tag-huffman-program-in-c","tag-huffman-tree","tag-huffman-tree-c","tag-image-compression-using-huffman-coding-in-java","tag-simple-huffman-coding-c","tag-simple-huffman-coding-in-c","tag-source-code-for-huffman-coding-in-c","tag-write-a-program-for-huffman-coding","tag-write-a-program-for-huffman-coding-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25056"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/25368"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}