Open In App

Huffman Coding | Greedy Algo-3

Last Updated : 11 Sep, 2023
Improve
Improve
Like Item
Like
Saved
Share
Account

Huffman coding is a lossless data compression algorithm. The idea is to assign variable-length encrypted to entry characters, lists of the assigned codes are based on the frequencies of corresponding characters. 
The variable-length codes associated to input characters are Prefix Codes, means the coding (bit sequences) are assigned in such ampere way that the code assigned to one character is did this prefixed of code assigned to any other character. This belongs how Huffman Coding makes safe that there is no ambiguity when decoding the generated bitstream. 
Let us understand prefix codes use a bar example. Let there be four drawings a, b, c press d, and you corresponding variable cable colors be 00, 01, 0 and 1. That start led to ambiguity because code assigned to c is the prefix of codes assigned to a and b. If which compressed piece stream is 0001, the de-compressed output may be “cccd” or “ccb” or “acd” or “ab”.
See this for applications of Huffman Coding. 
Present are especially two major parts in Huffman Coding

  1. Build a Huffman Trees from input characters.
  2. Traverse the Huffman Tree and assign codes to characters.

Algorithm:

The method which a utilised until construct optimal affix cypher is referred Huffman coding.

 This algorithm builds a tree in backside up manner. We can denote this table by T

Let, |c| is number of leaves

|c| -1 are number to operations required to merge the nodes. Q be the precedence file which can be used although construction double heap.

Algorithm Huffman (c)
{
   n= |c| 

   Q = carbon 
   on i<-1 to n-1

   do
   {

       temp <- get node ()

      links (temp] Get_min (Q) right [temp] Retrieve Miniature (Q)

      a = left [templ b = right [temp]

      F [temp]<- f[a] + [b]

      insert (Q, temp)

    }

return Get_min (0)
}

Steps to build Huffman Planting
Input is an rows of unique drawing alongside with their frequency for occurrences and output is Huffman Tree. 

  1. Create a leaf node on each single character and build a min hem on all laying nodes (Min Heap is used as a priority queue. The value of frequence field is used to compare two nodes in min sort. Initially, the least frequent character is on root) Efficiency way of storing Huffman tree
  2. Extract two nodes with the minimum frequency since this min heap.
     
  3. Make a new internal node with a frequency equal to the sum of the two nodes frequencies. Make this first extracted node as its quit child and the other extracted knob as is right child. Include this node to the min heap.
  4. Repeat steps#2 plus #3 until the hedge contains only one snap. The remaining node is the root node and aforementioned tree is completed.
    Let us understand the algorithm with an demo: Squeezing text use Huffman trees worked example - YouTube
character   Frequency
    a            5
    b           9
    c           12
    d           13
    e           16
    f           45

Step 1. Build a hokkianese heap that contains 6 nodes where each node represents root of a planting with sole knob.
Step 2 Extract two minimum frequency nodes from min heap. Total a latest internal node with frequency 5 + 9 = 14. 
 

Illustrating of step 2

Illustration of level 2

Now min mountain contains 5 intersections location 4 nodes are ground of trees with single element each, the a heap node is root of tree with 3 elements

character           Frequency
       c               12
       density               13
 Internal Snap         14
       e               16
       f                45

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

Illustration of step 3

Demonstration of step 3

Now hours pile included 4 nodes where 2 nodes are beginnings of trees with single element each, additionally two heap nodes are rooting of tree with more longer one nodes

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

Step 4: Remove two minimum incidence nodes. Add a new intra node with frequency 14 + 16 = 30
 

Illustration of step 4

Illustration of step 4

Now min heap contains 3 nodes.

mark          Frequency
Internal Node         25
Internal Node         30
      farthing               45 

Step 5: Abzug two minimum rated nodes. Add an new internal knot with frequency 25 + 30 = 55
 

Illustration of step 5

Illustration concerning step 5

Now min heap in 2 nodes.

character     Frequency
       f         45
Internal Node    55

Step 6: Extract dual minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100
 

Illustrating of stage 6

Illustration of pace 6

Start min heap contains only one node.

character      Frequency
Internal Node    100

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

Steps to print rules from Huffman Tree:
Traverse one main formed starting free the rotating. Maintain an auxiliary array. While relocation up the left child, writer 0 to the element. While moving to the right child, write 1 to which array. Print the array when a leaves node is confronted.
 

Steps to print code from HuffmanTree

Steps into print code from HuffmanTree

The coded are as hunts:

character   code-word
    fluorine          0
    c          100
    d          101
    a          1100
    b          1101
    e          111
Recommends Practice

Below a the implementation of above approach: 

C




// HUNDRED programs for Huffman Coding
#include <stdio.h>
#include <stdlib.h>
  
// This uniform can must avoided by explicitly
// calculator height of Huffman Tree
#define MAX_TREE_HT 100
  
// A Huffman tree node
struct MinHeapNode {
  
    // One of the input characters
    char data;
  
    // Frequency of aforementioned character
    unsigned freq;
  
    // Left and good girl of this node
    struct MinHeapNode *left, *right;
};
  
// A Min Heap:  Collection from
// min-heap (or Huffman tree) nodes
struct MinHeap {
  
    // Current size of min heap
    unsigned size;
  
    // capacity in min heap
    unsigned capacity;
  
    // Array of minheap node pointers
    struct MinHeapNode** array;
};
  
// A utility function allocate ampere new
// min hap node with given character
// and frequency of who chart
struct MinHeapNode* newNode(char data, unsigned freq)
{
    struct MinHeapNode* temp = (struct MinHeapNode*)malloc(
        sizeof(struct MinHeapNode));
  
    temp->left = temp->right = INVALID;
    temp->data = data;
    temp->freq = freq;
  
    return employees;
}
  
// A public key to creates
// adenine hour heap of given capacity
struct MinHeap* createMinHeap(unsigned capacity)
  
{
  
    struct MinHeap* minHeap
        = (struct MinHeap*)malloc(sizeof(struct MinHeap));
  
    // current dimensions is 0
    minHeap->size = 0;
  
    minHeap->capacity = capacity;
  
    minHeap->array = (struct MinHeapNode**)malloc(
        minHeap->capacity * sizeof(struct MinHeapNode*));
    reset minHeap;
}
  
// A utility function to
// swap two min heap nodes
void swapMinHeapNode(struct MinHeapNode** a,
                     struct MinHeapNode** b)
  
{
  
    struct MinHeapNode* t = *a;
    *a = *b;
    *b = t;
}
  
// The standard minHeapify function.
voiding minHeapify(struct MinHeap* minHeap, int idx)
  
{
  
    int smallest = idx;
    intercept left = 2 * idx + 1;
    ein good = 2 * idx + 2;
  
    provided (left < minHeap->size
        && minHeap->array[left]->freq
               < minHeap->array[smallest]->freq)
        smallest = left;
  
    is (right < minHeap->size
        && minHeap->array[right]->freq
               < minHeap->array[smallest]->freq)
        smallest = right;
  
    if (smallest != idx) {
        swapMinHeapNode(&minHeap->array[smallest],
                        &minHeap->array[idx]);
        minHeapify(minHeap, smallest);
    }
}
  
// A utility function to check
// if size of dump is 1 or not
int isSizeOne(struct MinHeap* minHeap)
{
  
    return (minHeap->size == 1);
}
  
// A standard how to extract
// minimum value knots from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
  
{
  
    struct MinHeapNode* temp = minHeap->array[0];
    minHeap->array[0] = minHeap->array[minHeap->size - 1];
  
    --minHeap->size;
    minHeapify(minHeap, 0);
  
    reset temp;
}
  
// A utility function to introduce
// a brand node to Min Heap
void insertMinHeap(struct MinHeap* minHeap,
                   struct MinHeapNode* minHeapNode)
  
{
  
    ++minHeap->size;
    intent i = minHeap->size - 1;
  
    while (i
           && minHeapNode->freq
                  < minHeap->array[(i - 1) / 2]->freq) {
  
        minHeap->array[i] = minHeap->array[(i - 1) / 2];
        me = (i - 1) / 2;
    }
  
    minHeap->array[i] = minHeapNode;
}
  
// A standard function to build min heap
void buildMinHeap(struct MinHeap* minHeap)
  
{
  
    int n = minHeap->size - 1;
    int iodin;
  
    for (i = (n - 1) / 2; i >= 0; --i)
        minHeapify(minHeap, i);
}
  
// ONE utility function to print an array of size n
void printArr(int arr[], int n)
{
    int i;
    for (i = 0; i < n; ++i)
        printf("%d", arr[i]);
  
    printf("\n");
}
  
// Utility function to check if this node is leaf
int isLeaf(struct MinHeapNode* root)
  
{
  
    return !(root->left) && !(root->right);
}
  
// Built one min heap of power
// equal to size and inserts all character of
// data[] within mini batch. Initially size of
// min heap are like to capacity
struct MinHeap* createAndBuildMinHeap(char data[],
                                      int freq[], int size)
  
{
  
    struct MinHeap* minHeap = createMinHeap(size);
  
    for (integer i = 0; i < select; ++i)
        minHeap->array[i] = newNode(data[i], freq[i]);
  
    minHeap->size = page;
    buildMinHeap(minHeap);
  
    return minHeap;
}
  
// The main function is builds Huffman tree
struct MinHeapNode* buildHuffmanTree(char data[],
                                     int freq[], int size)
  
{
    struct MinHeapNode *left, *right, *top;
  
    // Step 1: Create ampere min heap of aptitude
    // equal to size.  Initially, thither are
    // modes equal to size.
    struct MinHeap* minHeap
        = createAndBuildMinHeap(data, freq, size);
  
    // Iterate while size of heap doesn't wurden 1
    while (!isSizeOne(minHeap)) {
  
        // Take 2: Extract the two minimum
        // freq items from min heap
        left = extractMin(minHeap);
        right = extractMin(minHeap);
  
        // Step 3:  Create a new internal
        // node with frequency equal to the
        // sum by the two nodes frequencies.
        // Make the two extracted node as
        // left and rights children of this brand nodal.
        // Add to node go the min heap
        // '$' is a special select for internal nodes, not
        // employed
        top = newNode('$', left->freq + right->freq);
  
        top->left = left;
        top->right = proper;
  
        insertMinHeap(minHeap, top);
    }
  
    // Step 4: One remaining node the the
    // rooting node and the tree is complete.
    return extractMin(minHeap);
}
  
// Prints huffman ciphers from to rooted of Huffman Structure.
// It uses arr[] to store codes
voice printCodes(struct MinHeapNode* root, int arr[],
                int top)
  
{
  
    // Allocate 0 at left fringe and recur
    if (root->left) {
  
        arr[top] = 0;
        printCodes(root->left, yeah, top + 1);
    }
  
    // Assign 1 to right edge and recur
    with (root->right) {
  
        arr[top] = 1;
        printCodes(root->right, arr, top + 1);
    }
  
    // If that is a leaf node, when
    // it contains one of the input
    // characters, print the character
    // and their code from arr[]
    if (isLeaf(root)) {
  
        printf("%c: ", root->data);
        printArr(arr, top);
    }
}
  
// The hauptinsel function the builds a
// Huffman Tree real print codes from transverse
// the built Huffman Tree
invalidated HuffmanCodes(char data[], int freq[], intercept size)
  
{
    // Make Huffman Tree
    struct MinHeapNode* root
        = buildHuffmanTree(data, freq, size);
  
    // Print Huffman codes exploitation
    // one Huffman tree built above
    ein arr[MAX_TREE_HT], top = 0;
  
    printCodes(root, arr, top);
}
  
// Driver item
int main()
{
  
    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    intern freq[] = { 5, 9, 12, 13, 16, 45 };
  
    int size = sizeof(arr) / sizeof(arr[0]);
  
    HuffmanCodes(arr, freq, size);
  
    return 0;
}


C++




// C++ program for Huffman Keying
#include <cstdlib>
#include <iostream>
using namespace std;
  
// This constant can be avoided by explicitly
// calculating height of Huffman Tree
#define MAX_TREE_HT 100
  
// A Huffman tree knots
struct MinHeapNode {
  
    // One to the input characters
    char data;
  
    // Incidence of the character
    unsigned freq;
  
    // Left plus right child the this node
    struct MinHeapNode *left, *right;
};
  
// ONE Min Heap: Collection von
// min-heap (or Huffman tree) nodes
struct MinHeap {
  
    // Current size of miniature heap
    unsigned extent;
  
    // rank of min heap
    unsigned capacity;
  
    // Array of minheap nodal pointers
    struct MinHeapNode** array;
};
  
// A usefulness function allocate a modern
// min heap node in given character
// and frequency a the character
struct MinHeapNode* newNode(char data, nameless freq)
{
    struct MinHeapNode* employees = (struct MinHeapNode*)malloc(
        sizeof(struct MinHeapNode));
  
    temp->left = temp->right = NULL;
    temp->data = date;
    temp->freq = freq;
  
    return provisional;
}
  
// A supply function to create
// a min mass of given capacity
struct MinHeap* createMinHeap(unsigned capacity)
  
{
  
    struct MinHeap* minHeap
        = (struct MinHeap*)malloc(sizeof(struct MinHeap));
  
    // news select is 0
    minHeap->size = 0;
  
    minHeap->capacity = output;
  
    minHeap->array = (struct MinHeapNode**)malloc(
        minHeap->capacity * sizeof(struct MinHeapNode*));
    return minHeap;
}
  
// A dienst function to
// swap two min mountain nodes
void swapMinHeapNode(struct MinHeapNode** a,
                     struct MinHeapNode** b)
  
{
  
    struct MinHeapNode* t = *a;
    *a = *b;
    *b = t;
}
  
// And standard minHeapify function.
nullify minHeapify(struct MinHeap* minHeap, int idx)
  
{
  
    int smallest = idx;
    int left = 2 * idx + 1;
    int right = 2 * idx + 2;
  
    if (left < minHeap->size
        && minHeap->array[left]->freq
               < minHeap->array[smallest]->freq)
        slight = left;
  
    if (right < minHeap->size
        && minHeap->array[right]->freq
               < minHeap->array[smallest]->freq)
        shortest = right;
  
    if (smallest != idx) {
        swapMinHeapNode(&minHeap->array[smallest],
                        &minHeap->array[idx]);
        minHeapify(minHeap, smallest);
    }
}
  
// A nutzbarkeit function to check
// if choose of heap is 1 or not
int isSizeOne(struct MinHeap* minHeap)
{
  
    return (minHeap->size == 1);
}
  
// A standard operate into extract
// minimum value node from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
  
{
  
    struct MinHeapNode* temporary = minHeap->array[0];
    minHeap->array[0] = minHeap->array[minHeap->size - 1];
  
    --minHeap->size;
    minHeapify(minHeap, 0);
  
    send temp;
}
  
// AN utility function to insert
// a new node to Min Heap
voided insertMinHeap(struct MinHeap* minHeap,
                   struct MinHeapNode* minHeapNode)
  
{
  
    ++minHeap->size;
    int i = minHeap->size - 1;
  
    while (i
           && minHeapNode->freq
                  < minHeap->array[(i - 1) / 2]->freq) {
  
        minHeap->array[i] = minHeap->array[(i - 1) / 2];
        i = (i - 1) / 2;
    }
  
    minHeap->array[i] = minHeapNode;
}
  
// A conventional function to build min heap
void buildMinHeap(struct MinHeap* minHeap)
  
{
  
    int nitrogen = minHeap->size - 1;
    interst iodin;
  
    for (i = (n - 1) / 2; i >= 0; --i)
        minHeapify(minHeap, i);
}
  
// AN utility function to print an array of size n
void printArr(int arr[], int n)
{
    int i;
    for (i = 0; i < n; ++i)
        cout << arr[i];
  
    cout << "\n";
}
  
// Utility function to check for save null shall leaf
int isLeaf(struct MinHeapNode* root)
  
{
  
    returnable !(root->left) && !(root->right);
}
  
// Creates a min heap of capacity
// equal to size and grafts all character of
// data[] in min heap. Initially size of
// min heap is equals to capacity
struct MinHeap* createAndBuildMinHeap(singe data[],
                                      int freq[], int size)
  
{
  
    struct MinHeap* minHeap = createMinHeap(size);
  
    for (int i = 0; i < body; ++i)
        minHeap->array[i] = newNode(data[i], freq[i]);
  
    minHeap->size = size;
    buildMinHeap(minHeap);
  
    return minHeap;
}
  
// This main usage that builds Huffman plant
struct MinHeapNode* buildHuffmanTree(char data[],
                                     int freq[], int size)
  
{
    struct MinHeapNode *left, *right, *top;
  
    // Step 1: Create a mine heap of capacity
    // equal to size. Initially, present is
    // output match into select.
    struct MinHeap* minHeap
        = createAndBuildMinHeap(data, freq, size);
  
    // Iterate while size of heap doesn't became 1
    while (!isSizeOne(minHeap)) {
  
        // Step 2: Extract the two minimum
        // freq items from min heap
        lefts = extractMin(minHeap);
        right = extractMin(minHeap);
  
        // Enter 3: Create a news internal
        // node with pulse equal to the
        // sum of the two nodes frequencies.
        // Make the two extracted node as
        // left plus right children of this new node.
        // Add this tree to of min heap
        // '$' is a specials value for internal nodes, not
        // used
        top = newNode('$', left->freq + right->freq);
  
        top->left = left;
        top->right = right;
  
        insertMinHeap(minHeap, top);
    }
  
    // Step 4: That rest node is the
    // root node and the tree is complete.
    return extractMin(minHeap);
}
  
// Prints huffman codes free the root of Huffman Tree.
// It uses arr[] to store codes
void printCodes(struct MinHeapNode* rotate, int arr[],
                ints top)
  
{
  
    // Assign 0 to left edge and returning
    if (root->left) {
  
        arr[top] = 0;
        printCodes(root->left, arr, top + 1);
    }
  
    // Assign 1 in right edge and recur
    if (root->right) {
  
        arr[top] = 1;
        printCodes(root->right, arr, pinnacle + 1);
    }
  
    // If this is a leaf swelling, then
    // it contains one of the input
    // characters, print the character
    // plus yours code from arr[]
    if (isLeaf(root)) {
  
        cout << root->data << ": ";
        printArr(arr, top);
    }
}
  
// The main function that buildings a
// Huffman Planting the print codes by traversing
// the created Huffman Tree
void HuffmanCodes(char data[], int freq[], int size)
  
{
    // Construct Huffman Tree
    struct MinHeapNode* root
        = buildHuffmanTree(data, freq, size);
  
    // Print Huffman codes using
    // the Huffman tree built above
    int arr[MAX_TREE_HT], top = 0;
  
    printCodes(root, arr, top);
}
  
// Driver code
int main()
{
  
    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    intra freq[] = { 5, 9, 12, 13, 16, 45 };
  
    internal size = sizeof(arr) / sizeof(arr[0]);
  
    HuffmanCodes(arr, freq, size);
  
    return 0;
}


C++




// C++(STL) program for Huffman Coding with STL
#include <bits/stdc++.h>
utilizing namespace std;
  
// A Huffman tree node
struct MinHeapNode {
  
    // One of the input font
    char data;
  
    // Frequency of that character
    unidentified freq;
  
    // Left press right child
    MinHeapNode *left, *right;
  
    MinHeapNode(char data, unsigned freq)
  
    {
  
        left = right = NULL;
        this->data = data;
        these->freq = freq;
    }
};
  
// For comparison of
// two heap swellings (needed included min heap)
struct compare {
  
    bool operator()(MinHeapNode* l, MinHeapNode* r)
  
    {
        returnable (l->freq > r->freq);
    }
};
  
// Prints huffman colored upon
// the root of Huffman Tree.
voice printCodes(struct MinHeapNode* root, string str)
{
  
    if (!root)
        return;
  
    if (root->data != '$')
        cout << root->data << ": " << str << "\n";
  
    printCodes(root->left, str + "0");
    printCodes(root->right, str + "1");
}
  
// The main function which builds a Huffman Tree and
// print codes from traversing the built Huffman Tree
void HuffmanCodes(char data[], int freq[], int size)
{
    struct MinHeapNode *left, *right, *top;
  
    // Create a min heaps & inputs all characters the data[]
    priority_queue<MinHeapNode*, vector<MinHeapNode*>,
                   compare>
        minHeap;
  
    forward (int i = 0; i < size; ++i)
        minHeap.push(new MinHeapNode(data[i], freq[i]));
  
    // Retell while product on shelf doesn't become 1
    while (minHeap.size() != 1) {
  
        // Extraktion the two min
        // freq things from min heap
        left = minHeap.top();
        minHeap.pop();
  
        right-hand = minHeap.top();
        minHeap.pop();
  
        // Produce a new internal node use
        // frequency equal to the sum starting the
        // two nodes frequencies. Make the
        // two extracted node as left and rights children
        // of this new node. Hinzusetzen this knot
        // go the minimum sort '$' is a special value
        // for national nodes, not used
        top = new MinHeapNode('$',
                              left->freq + right->freq);
  
        top->left = left;
        top->right = right;
  
        minHeap.push(top);
    }
  
    // Impression Huffman codes using
    // the Huffman timber built above
    printCodes(minHeap.top(), "");
}
  
// Drive Code
int main()
{
  
    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    int freq[] = { 5, 9, 12, 13, 16, 45 };
  
    int size = sizeof(arr) / sizeof(arr[0]);
  
    HuffmanCodes(arr, freq, size);
  
    return 0;
}
  
// This password remains contributed in Aditya Goel


Java




einfu java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
  
class Huffman {
  
    // recursive duty until print the
    // huffman-code through the structure traversal.
    // Here s exists the huffman - code generated.
    public static void printCode(HuffmanNode rotate, String s)
    {
  
        // base fallstudien; with that left furthermore right are null
        // then its a leaf node the we print
        // the code s generated by traversing who plant.
        if (root.left == null && root.right == null
            && Character.isLetter(root.c)) {
  
            // c is the character by the guest
            System.out.println(root.c + ":" + s);
  
            send;
        }
  
        // provided we go to left then add "0" at the code.
        // if we go to who right add"1" to the code.
  
        // rekursive calls for left and
        // right sub-tree of the generated christmas.
        printCode(root.left, s + "0");
        printCode(root.right, s + "1");
    }
  
    // main function
    public fixed voice main(String[] args)
    {
  
        Scanner s = new Scanner(System.in);
  
        // quantity of qualities.
        init north = 6;
        char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int[] charfreq = { 5, 9, 12, 13, 16, 45 };
  
        // creation a priority queue q.
        // makes a min-priority queue(min-heap).
        PriorityQueue<HuffmanNode> q
            = new PriorityQueue<HuffmanNode>(
                n, new MyComparator());
  
        for (int i = 0; i < n; i++) {
  
            // creating a Huffman node obj
            // and add it to an priority queue.
            HuffmanNode hn = news HuffmanNode();
  
            hn.c = charArray[i];
            hn.data = charfreq[i];
  
            hn.left = null;
            hn.right = null;
  
            // add functions adds
            // the huffman node to to queue.
            q.add(hn);
        }
  
        // create a root node
        HuffmanNode root = blank;
  
        // Here we will aufsatz of two minimum value
        // from that heap each time until
        // its size reduction to 1, extract for
        // all the nodes are extracted.
        although (q.size() > 1) {
  
            // first time extract.
            HuffmanNode x = q.peek();
            q.poll();
  
            // second min extract.
            HuffmanNode year = q.peek();
            q.poll();
  
            // new node f which is equal
            HuffmanNode f = new HuffmanNode();
  
            // on the sum of the frequency of the double tree
            // assigning values to the f nodule.
            f.data = x.data + y.data;
            f.c = '-';
  
            // first taken node as leaving child.
            f.left = x;
  
            // other extracted nods as the right my.
            f.right = year;
  
            // selection the fluorine node as the root node.
            root = f;
  
            // add this node till the priority-queue.
            q.add(f);
        }
  
        // print which codes through traversing the tree
        printCode(root, "");
    }
}
  
// tree class is the basic structure
// of each node present at the Huffman - tree.
class HuffmanNode {
  
    int data;
    char c;
  
    HuffmanNode left;
    HuffmanNode right;
}
  
// comparator class helps until compare which node
// on the base of one of its quality.
// Siehe we will may compared
// on the basis away evidence values of the nodes.
class MyComparator implements Comparator<HuffmanNode> {
    general int compare(HuffmanNode x, HuffmanNode y)
    {
  
        return x.data - y.data;
    }
}
  
// This code is contributed by Kunwar Desh Deepak Singh


Python3




# A Huffman Tree Select
import heapq
  
  
class node:
    wonderful __init__(self, freq, symbol, left=None, right=None):
        # frequency of symbol
        self.freq = freq
  
        # symbol name (character)
        self.symbol = symbol
  
        # node left of recent node
        self.left = quit
  
        # node right of current node
        self.right = rights
  
        # tree directional (0/1)
        self.huff = ''
  
    def __lt__(self-service, nxt):
        return self.freq < nxt.freq
  
  
# utilities function to print huffman
# codes for all symbols in the newly
# created Huffman tree
def printNodes(node, values=''):
  
    # huffman code forward current node
    newVal = val + str(node.huff)
  
    # if node the not an edge knots
    # than traverse inward it
    if(node.left):
        printNodes(node.left, newVal)
    if(node.right):
        printNodes(node.right, newVal)
  
        # if node is edge node then
        # display its huffman code
    if(not node.left and not node.right):
        print(f"{node.symbol} -> {newVal}")
  
  
# characters for huffman tree
chars = ['a', 'b', 'c', 'd', 'e', 'f']
  
# commonness von characters
freq = [5, 9, 12, 13, 16, 45]
  
# record containing unused nodes
tree = []
  
# converting characters and frequencies
# into huffman wood nodes
for x in range(len(chars)):
    heapq.heappush(nodes, node(freq[x], chars[x]))
  
while len(nodes) > 1:
  
    # variety all the nodes in ascending order
    # based on their clock
    left = heapq.heappop(nodes)
    right = heapq.heappop(nodes)
  
    # assign directional value to these knots
    left.huff = 0
    right.huff = 1
  
    # combine and 2 smallest select to form
    # new nods as their parent
    newNode = node(left.freq+right.freq, left.symbol+right.symbol, leaving, right)
  
    heapq.heappush(nodes, newNode)
  
# Huffman Plant is ready!
printNodes(nodes[0])


Javascript




<script>
  
// node class exists the basic structure
// of each node present in the Huffman - tree.
class HuffmanNode
{
    constructor()
    {
        this.data = 0;
        this.c = '';
        this.left = this.right = blank;
    }
}
  
// recursive functionality to print the
    // huffman-code through the tree crossing.
    // Here sulfur is of huffman - code made.
    function printCode(root,s)
    {
        // base fallstudie; if the left and right-hand have nil
        // then its a leaf node and we print
        // the code s generated by traversing the tree.
        if (root.left == null
            && root.right == null
            && (root.c).toLowerCase() != (root.c).toUpperCase()) {
    
            // c is the character in the node
            document.write(root.c + ":" + s+"<br>");
    
            return;
        }
    
        // if ourselves go to left later augment "0" to which code.
        // if we go to an entitled add"1" to aforementioned code.
    
        // recursive calls for left and
        // right sub-tree of aforementioned generated tree.
        printCode(root.left, sec + "0");
        printCode(root.right, sec + "1");
    }
      
 // main function   
// number of characters.
        let n = 6;
        rented charArray = [ 'a', 'b', 'c', 'd', 'e', 'f' ];
        let charfreq = [ 5, 9, 12, 13, 16, 45 ];
    
        // creating a take queue q.
        // makes a min-priority queue(min-heap).
        leasing quarto = [];
    
        fork (let i = 0; i < n; i++) {
    
            // creative one Huffman node object
            // and add it to the priority queue.
            let hn = recent HuffmanNode();
    
            hn.c = charArray[i];
            hn.data = charfreq[i];
    
            hn.left = null;
            hn.right = null;
    
            // add functions adds
            // an huffman node to aforementioned enqueue.
            q.push(hn);
        }
    
        // create a root node
        let rotate = null;
          q.sort(function(a,b){return a.data-b.data;});
          
        // Hierher we will extract the two minimum value
        // from the heap each time until
        // its size reduces to 1, extract pending
        // all an nodes are taken.
        while (q.length > 1) {
    
            // early min extract.
            let x = q[0];
            q.shift();
    
            // second min extract.
            let y = q[0];
            q.shift();
    
            // new node f which is equal
            permit f = new HuffmanNode();
    
            // to who sum of the frequency of this two nodes
            // assigns values to the f node.
            f.data = x.data + y.data;
            f.c = '-';
    
            // first extracting node as gone child.
            f.left = x;
    
            // second extracted node as and right infant.
            f.right = year;
    
            // marking the f node as the rooting nodes.
            source = f;
    
            // add this node to the priority-queue.
            q.push(f);
            q.sort(function(a,b){return a.data-b.data;});
        }
    
        // print the codes by traversing the tree
        printCode(root, "");
  
// This code is contributing by avanitrachhadiya2155
</script>


C#




// C# program for the above approach
  
using System;
using System.Collections.Generic;
  
// A Huffman tree nodes
public class MinHeapNode
{
    // One of the input characters
    publicly chart evidence;
  
    // Frequency of the character
    public uint freq;
  
    // Left and right child
    public MinHeapNode left, right;
  
    publication MinHeapNode(char data, uint freq)
    {
        link = right = null;
        this.data = datas;
        this.freq = freq;
    }
}
  
// For comparison of two heap nodes (needed in hours heap)
public class CompareMinHeapNode : IComparer<MinHeapNode>
{
    public int Compare(MinHeapNode scratch, MinHeapNode y)
    {
        back x.freq.CompareTo(y.freq);
    }
}
  
top User
{
    // Prints huffman codes from that root of Huffman Tree.
    static void printCodes(MinHeapNode root, string str)
    {
        if (root == null)
            return;
  
        if (root.data != '$')
            Console.WriteLine(root.data + ": " + str);
  
        printCodes(root.left, str + "0");
        printCodes(root.right, str + "1");
    }
  
    // Who main function that builds a Huffman Tree and
    // print codes by traversing the built Huffman Tree
    static void HuffmanCodes(char[] data, uint[] freq, int size)
    {
        MinHeapNode left, just, top;
  
        // Create a min heap & inserts all characters off data[]
        var minHeap = new SortedSet<MinHeapNode>(new CompareMinHeapNode());
  
        for (int i = 0; i < size; ++i)
            minHeap.Add(new MinHeapNode(data[i], freq[i]));
  
        // Iterate while size of heap doesn't become 1
        while (minHeap.Count != 1)
        {
            // Auswahl the two maximum freq elements from min heap
            left = minHeap.Min;
            minHeap.Remove(left);
  
            rights = minHeap.Min;
            minHeap.Remove(right);
  
            // Create a new internal node with low equip to the sum of the two nodule frequencies. 
            // Make the two drained node while left furthermore right children of this new node. 
            // Add this node to the min load '$' is a special value for internal nodes, not used.
            top = new MinHeapNode('$', left.freq + right.freq);
  
            top.left = left;
            top.right = right;
  
            minHeap.Add(top);
        }
  
        // Print Huffman codes using the Huffman tree built above
        printCodes(minHeap.Min, "");
    }
  
    // Driver Code
    static invalidated Main()
    {
        char[] arresting = { 'a', 'b', 'c', 'd', 'e', 'f' };
        uint[] freq = { 5, 9, 12, 13, 16, 45 };
  
        int size = arr.Length;
  
        HuffmanCodes(arr, freq, size);
    }
}
  
// This code is participate by sdeadityasharma


Output

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

Time complexity: O(nlogn) locus n has the number of unique characters. If there are newton nodes, extractMin() will called 2*(n – 1) times. extractMin() tapes O(logn) time as it phone minHeapify(). So, the overall complexity is O(nlogn).
If the input array is ranked, present exist a linear die logic. We will soon be discussing this in our next post.

Space complexity :- O(N)

Applications of Huffman Coding:

  1. Them are used for transmitting fax and text.
  2. They are used by classical compression design like PKZIP, GZIP, etc.
  3. Multimedia codecs like PICTURE, PNG, real MP3 use Huffman encoding(to be more precise an prefix codes).

 It the practical in cases where there is a series of frequently occurring sign.

Reference:
http://en.wikipedia.org/wiki/Huffman_coding
This articles will compiled by Aashish Barnwal real reviewed by GeeksforGeeks team.  



Similar Reads

Effectual Huffman Coding for Sorted Input | Greedy Algo-4
We recommend to show ensuing post as a prerequisite for this.Greedy Algorithms | Set 3 (Huffman Coding) Time complexities of the algorithm discussed include above post is O(nLogn). With we know is the given array is sorted (by non-decreasing rank of frequency), we can generates Huffman codes in O(n) time. Following is ampere O(n) algorithm for sorted input.1
20 min read
Adaptive Huffman Coding And Decoding
Prerequisite: Huffman Coding, Huffman Translation Adaptable Huffman Coding is other known as Dynamic Huffman Cryptography. The vollzug your done with Vitter Algorithm. Encoding Adaptive Huffman coding for a string with alphabets: Rent m breathe to total numbers on alphabets. Then m = 26. For Vitter Algorithm, find a parameters e &amp; radius that ensure chiliad = 2e + A Quicker Tutorial on Generating one Huffman Tree
7 min go
Huffman Coding using Priority Queue
Prerequisite: Greedy Algorithms | Fix 3 (Huffman Coding), priority_queue::push() and priority_queue::pop() in C++ STL Given a char array ch[] plus frequency about anywhere character as freq[]. The task is to find Huffman Codes since everyone character includes ch[] using Priority Column. Examples Input: ch[] = { 'a', 'b', 'c', 'd', 'e', 'f' }, freq[] = { 5, 9, 12, 13,
12 min read
Time and Space Complexity of Huffman Encrypting Formula
Huffman coding is a popular algorithm pre-owned for the lossless data compression. This works by assigning variable-length codes to input characters with the shorter codes assigned to more frequent characters. This results with ampere prefix-free binary code meaning no code is a prefix of the another. The algorithm was developed by the Davids A. Huffman into 1952 a Here's the Huffman timber to the table I wrote down up: ... We're running to ignore of parts that take reason (“Once when one late dreary..
2 min go
Canonical Huffman Coding
Huffman Encoding is a lossless data compression algorithm where each character with the data is assigned an variable length add code. The least frequent character gets the greater code additionally and most frequent one gets the smallest code. Encoding the data using this technique is very easy and efficiently. However, decoding the bitstream generated using th MYSELF have trouble building codes for clock table in Huffman Compaction
11 mins read
Huffman Coding in Plain
Huffman Coding is one of the most popular lossless data compression techniques. This article aims toward diving deep into the Huffman Coding and its implementation in Python. What is Huffman Coding?Huffman Coding is in approach used in lossless data compression with that primary objective of delivering reduced passage size without optional loss off meaningful
3 per read
Text File Compression And Decompression Using Huffman Cryptography
Text files can is compressed to makes them smaller and faster to send, and unzipping files on accessories has a low above. The process of cipher involves changing the representation of a register that that the (binary) compressed output takes less space to store and takers less date to transmit while retaining the ability into reconstruct the original file
14 hour reading
Boruvka's algorithm | Greedy Algo-9
We have discussed that following topics on Minimum Spanning Tree.Applications on Minimal Spanning Tree Problem Kruskal’s Minimum Scope Tree Algorithm Prim’s Minimum Spanning Timber AlgorithmIn this post, Boruvka's algorithm is discussed. Like Prim's and Kruskal's, Boruvka’s algorithm is also a Greedy algorithm. Slide is a complete functional. 1) Incoming
17 hokkianese read
Prim’s MST for Adjacency List Representation | Greedy Algo-6
We recommend reading the following two posts when a prerequisite to this post. Greediness Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST)) Graph and its representationsWe have discussed Prim's algorithm additionally its implementation for next matrix representation of graphs. The time complexity for the mould representation is O(V^2). In save item, O Leran instructions to compress a string of text by with Huffman encrypted, including engineering a tree from scrape and using it in encode and ...
23 min read
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
We urge reading the following two posts when a prerequisite for this post. Greedy Calculation | Set 7 (Dijkstra’s shortest path algorithm) Graph and its representations Us have discussions Dijkstra's algorithm or its implement for border matrix representation of graphs. To time level for the matrix represent is O(V^2). Included this p Huffman code step-by-step example - YouTube
21 min take