{"id":837,"date":"2024-10-14T14:20:28","date_gmt":"2024-10-14T08:50:28","guid":{"rendered":"https:\/\/codexplained.in\/?p=837"},"modified":"2025-11-24T15:46:35","modified_gmt":"2025-11-24T10:16:35","slug":"implementation-of-huffman-coding","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=837","title":{"rendered":"Implementation of Huffman Coding"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Problem Statement<\/h3>\n\n\n\n<p>The goal is to implement Huffman coding, which involves:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Building a frequency table from the input characters.<\/li>\n\n\n\n<li>Creating a binary tree based on the frequencies.<\/li>\n\n\n\n<li>Generating binary codes for each character from the tree.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Priority Queue<\/strong>: We will use a min-heap (priority queue) to efficiently extract the two nodes with the lowest frequency during the tree construction.<\/li>\n\n\n\n<li><strong>Binary Tree<\/strong>: Each character and its frequency will be represented as a leaf node in a binary tree. Internal nodes will represent the combined frequency of their children.<\/li>\n\n\n\n<li><strong>Code Generation<\/strong>: Traverse the tree to generate codes based on the path taken (left for 0 and right for 1).<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Programming Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Count the frequency of each character in the input string.<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: Build a priority queue from the frequency data.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: Create the Huffman tree by repeatedly merging the two least frequent nodes.<\/li>\n\n\n\n<li><strong>Step 4<\/strong>: Generate codes by traversing the tree.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C Program for Huffman Coding<\/h3>\n\n\n\n<p>Here\u2019s a complete C program to implement Huffman coding:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\n\/\/ Node structure for the Huffman tree\nstruct Node {\n    char character;\n    int frequency;\n    struct Node *left, *right;\n};\n\n\/\/ Min-Heap structure\nstruct MinHeap {\n    int size;\n    int capacity;\n    struct Node **array;\n};\n\n\/\/ Function to create a new min-heap node\nstruct Node* newNode(char character, int frequency) {\n    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));\n    temp-&gt;left = temp-&gt;right = NULL;\n    temp-&gt;character = character;\n    temp-&gt;frequency = frequency;\n    return temp;\n}\n\n\/\/ Function to create a min-heap\nstruct MinHeap* createMinHeap(int capacity) {\n    struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));\n    minHeap-&gt;size = 0;\n    minHeap-&gt;capacity = capacity;\n    minHeap-&gt;array = (struct Node**)malloc(minHeap-&gt;capacity * sizeof(struct Node*));\n    return minHeap;\n}\n\n\/\/ Function to swap two nodes\nvoid swapNode(struct Node** a, struct Node** b) {\n    struct Node* t = *a;\n    *a = *b;\n    *b = t;\n}\n\n\/\/ Function to heapify the min-heap\nvoid minHeapify(struct MinHeap* minHeap, int idx) {\n    int smallest = idx;\n    int left = 2 * idx + 1;\n    int right = 2 * idx + 2;\n\n    if (left &lt; minHeap-&gt;size &amp;&amp; minHeap-&gt;array&#x5B;left]-&gt;frequency &lt; minHeap-&gt;array&#x5B;smallest]-&gt;frequency) {\n        smallest = left;\n    }\n    if (right &lt; minHeap-&gt;size &amp;&amp; minHeap-&gt;array&#x5B;right]-&gt;frequency &lt; minHeap-&gt;array&#x5B;smallest]-&gt;frequency) {\n        smallest = right;\n    }\n    if (smallest != idx) {\n        swapNode(&amp;minHeap-&gt;array&#x5B;smallest], &amp;minHeap-&gt;array&#x5B;idx]);\n        minHeapify(minHeap, smallest);\n    }\n}\n\n\/\/ Function to check if size is 1\nint isSizeOne(struct MinHeap* minHeap) {\n    return (minHeap-&gt;size == 1);\n}\n\n\/\/ Function to extract the minimum node\nstruct Node* extractMin(struct MinHeap* minHeap) {\n    struct Node* temp = minHeap-&gt;array&#x5B;0];\n    minHeap-&gt;array&#x5B;0] = minHeap-&gt;array&#x5B;minHeap-&gt;size - 1];\n    --minHeap-&gt;size;\n    minHeapify(minHeap, 0);\n    return temp;\n}\n\n\/\/ Function to insert a new node\nvoid insertMinHeap(struct MinHeap* minHeap, struct Node* node) {\n    ++minHeap-&gt;size;\n    int i = minHeap-&gt;size - 1;\n\n    while (i &amp;&amp; node-&gt;frequency &lt; minHeap-&gt;array&#x5B;(i - 1) \/ 2]-&gt;frequency) {\n        minHeap-&gt;array&#x5B;i] = minHeap-&gt;array&#x5B;(i - 1) \/ 2];\n        i = (i - 1) \/ 2;\n    }\n    minHeap-&gt;array&#x5B;i] = node;\n}\n\n\/\/ Function to build the Huffman tree\nstruct Node* buildHuffmanTree(char characters&#x5B;], int frequencies&#x5B;], int size) {\n    struct Node *left, *right, *top;\n\n    struct MinHeap* minHeap = createMinHeap(size);\n\n    for (int i = 0; i &lt; size; ++i) {\n        minHeap-&gt;array&#x5B;i] = newNode(characters&#x5B;i], frequencies&#x5B;i]);\n    }\n    minHeap-&gt;size = size;\n\n    while (!isSizeOne(minHeap)) {\n        left = extractMin(minHeap);\n        right = extractMin(minHeap);\n\n        top = newNode(&#039;$&#039;, left-&gt;frequency + right-&gt;frequency);\n        top-&gt;left = left;\n        top-&gt;right = right;\n\n        insertMinHeap(minHeap, top);\n    }\n\n    return extractMin(minHeap);\n}\n\n\/\/ Function to print the Huffman codes\nvoid printCodes(struct Node* root, int arr&#x5B;], int top) {\n    if (root-&gt;left) {\n        arr&#x5B;top] = 0;\n        printCodes(root-&gt;left, arr, top + 1);\n    }\n    if (root-&gt;right) {\n        arr&#x5B;top] = 1;\n        printCodes(root-&gt;right, arr, top + 1);\n    }\n    if (!root-&gt;left &amp;&amp; !root-&gt;right) {\n        printf(&quot;%c: &quot;, root-&gt;character);\n        for (int i = 0; i &lt; top; ++i) {\n            printf(&quot;%d&quot;, arr&#x5B;i]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n}\n\n\/\/ Main function\nint main() {\n    char characters&#x5B;] = {&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;, &#039;e&#039;, &#039;f&#039;};\n    int frequencies&#x5B;] = {5, 9, 12, 13, 16, 45};\n    int size = sizeof(characters) \/ sizeof(characters&#x5B;0]);\n\n    struct Node* root = buildHuffmanTree(characters, frequencies, size);\n\n    int arr&#x5B;100], top = 0;\n\n    printf(&quot;Huffman Codes:\\n&quot;);\n    printCodes(root, arr, top);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Node Structure<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Each node represents a character and its frequency, along with pointers to its left and right children.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Min-Heap Implementation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We create a min-heap to store the nodes. This helps in efficiently extracting the two nodes with the smallest frequencies.<\/li>\n\n\n\n<li>Functions are provided for min-heap operations like insertion, extraction, and heapification.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Building the Huffman Tree<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We build the tree by repeatedly extracting the two smallest nodes, creating a new internal node with these two as children, and inserting it back into the min-heap.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Generating Huffman Codes<\/strong>:\n<ul class=\"wp-block-list\">\n<li>We traverse the tree recursively to generate codes. Moving left corresponds to appending a <code>0<\/code> to the code, while moving right appends a <code>1<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The main function initializes a set of characters and their frequencies, builds the Huffman tree, and prints the generated codes.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<p>The program uses a predefined set of characters and their frequencies, so there&#8217;s no user input in this version. Here&#8217;s an example of the output:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">yamlCopy code<code>Huffman Codes:\ne: 000\na: 001\nd: 01\nb: 10\nc: 110\nf: 111\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p>In the output, each character is followed by its corresponding Huffman code. The most frequent characters (like &#8216;f&#8217;) get the longest codes, while the least frequent characters (like &#8216;e&#8217;) get shorter codes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program implements Huffman coding efficiently using dynamic data structures like trees and heaps. It provides a clear way to compress data based on character frequencies, making it valuable for various applications in data compression and encoding. You can modify the character set and frequencies to see how the Huffman codes change based on different inputs.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Problem Statement The goal is to implement Huffman coding, which involves: Key Concepts Dynamic Programming Approach C Program for Huffman Coding Here\u2019s a complete C program to implement Huffman coding: Explanation of the Code Input and Output Example The program uses a predefined set of characters and their frequencies, so there&#8217;s no user input in [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=837"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":1427,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/837\/revisions\/1427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/838"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}