banner



How To Sort An Array In Increasing Order In Java

Huffman Coding

In this tutorial, you will learn how Huffman Coding works. Also, y'all will find working examples of Huffman Coding in C, C++, Coffee and Python.

Huffman Coding is a technique of compressing data to reduce its size without losing any of the details. Information technology was first adult by David Huffman.

Huffman Coding is more often than not useful to compress the data in which there are oftentimes occurring characters.


How Huffman Coding works?

Suppose the string below is to be sent over a network.

string
Initial string

Each grapheme occupies viii $.25. There are a total of 15 characters in the above string. Thus, a total of 8 * 15 = 120 bits are required to send this string.

Using the Huffman Coding technique, we can shrink the cord to a smaller size.

Huffman coding kickoff creates a tree using the frequencies of the character and so generates code for each character.

Once the information is encoded, it has to be decoded. Decoding is done using the same tree.

Huffman Coding prevents whatever ambiguity in the decoding procedure using the concept of prefix code ie. a code associated with a character should not be present in the prefix of whatever other code. The tree created in a higher place helps in maintaining the property.

Huffman coding is washed with the help of the following steps.

  1. Summate the frequency of each character in the string.
    frequency of string
    Frequency of string
  2. Sort the characters in increasing order of the frequency. These are stored in a priority queue Q.
    huffman coding
    Characters sorted according to the frequency
  3. Make each unique graphic symbol as a leaf node.
  4. Create an empty node z. Assign the minimum frequency to the left child of z and assign the 2d minimum frequency to the right kid of z. Set the value of the z every bit the sum of the above two minimum frequencies.
    huffman coding
    Getting the sum of the to the lowest degree numbers
  5. Remove these two minimum frequencies from Q and add the sum into the list of frequencies (* announce the internal nodes in the figure higher up).
  6. Insert node z into the tree.
  7. Echo steps 3 to 5 for all the characters.
    huffman coding
    Echo steps 3 to 5 for all the characters.
    huffman coding
    Repeat steps 3 to 5 for all the characters.
  8. For each non-foliage node, assign 0 to the left edge and ane to the right border.
    huffman coding
    Assign 0 to the left edge and 1 to the right edge

For sending the above string over a network, we have to transport the tree equally well equally the in a higher place compressed-code. The total size is given by the table beneath.

Character Frequency Code Size
A 5 11 5*ii = ten
B i 100 i*3 = 3
C 6 0 vi*one = half-dozen
D 3 101 3*3 = nine
four * 8 = 32 $.25 fifteen $.25 28 bits

Without encoding, the total size of the string was 120 bits. Subsequently encoding the size is reduced to 32 + fifteen + 28 = 75.


Decoding the lawmaking

For decoding the lawmaking, we tin take the code and traverse through the tree to observe the graphic symbol.

Allow 101 is to be decoded, nosotros tin can traverse from the root every bit in the effigy below.

huffman coding
Decoding

Huffman Coding Algorithm

create a priority queue Q consisting of each unique grapheme. sort then in ascending society of their frequencies. for all the unique characters:     create a newNode     extract minimum value from Q and assign it to leftChild of newNode     extract minimum value from Q and assign it to rightChild of newNode     calculate the sum of these ii minimum values and assign it to the value of newNode     insert this newNode into the tree render rootNode

Python, Java and C/C++ Examples

                # Huffman Coding in python  string = 'BCAADDDCCACACAC'   # Creating tree nodes class NodeTree(object):      def __init__(self, left=None, right=None):         self.left = left         self.right = correct      def children(self):         return (self.left, self.right)      def nodes(cocky):         return (self.left, cocky.right)      def __str__(self):         return '%s_%s' % (self.left, self.correct)   # Master part implementing huffman coding def huffman_code_tree(node, left=Truthful, binString=''):     if blazon(node) is str:         return {node: binString}     (l, r) = node.children()     d = dict()     d.update(huffman_code_tree(l, Truthful, binString + '0'))     d.update(huffman_code_tree(r, False, binString + '1'))     return d   # Calculating frequency freq = {} for c in cord:     if c in freq:         freq[c] += i     else:         freq[c] = 1  freq = sorted(freq.items(), central=lambda x: x[1], opposite=True)  nodes = freq  while len(nodes) > 1:     (key1, c1) = nodes[-one]     (key2, c2) = nodes[-ii]     nodes = nodes[:-2]     node = NodeTree(key1, key2)     nodes.append((node, c1 + c2))      nodes = sorted(nodes, primal=lambda x: 10[one], reverse=True)  huffmanCode = huffman_code_tree(nodes[0][0])  print(' Char | Huffman code ') print('----------------------') for (char, frequency) in freq:     print(' %-4r |%12s' % (char, huffmanCode[char]))              
                // Huffman Coding in Java  import java.util.PriorityQueue; import java.util.Comparator;  class HuffmanNode {   int detail;   char c;   HuffmanNode left;   HuffmanNode right; }  // For comparing the nodes form ImplementComparator implements Comparator<HuffmanNode> {   public int compare(HuffmanNode x, HuffmanNode y) {     render ten.detail - y.item;   } }  // IMplementing the huffman algorithm public class Huffman {   public static void printCode(HuffmanNode root, String due south) {     if (root.left == aught && root.right == zippo && Graphic symbol.isLetter(root.c)) {        System.out.println(root.c + "   |  " + s);        render;     }     printCode(root.left, south + "0");     printCode(root.right, south + "1");   }    public static void principal(String[] args) {      int n = 4;     char[] charArray = { 'A', 'B', 'C', 'D' };     int[] charfreq = { five, 1, half dozen, 3 };      PriorityQueue<HuffmanNode> q = new PriorityQueue<HuffmanNode>(due north, new ImplementComparator());      for (int i = 0; i < n; i++) {       HuffmanNode hn = new HuffmanNode();        hn.c = charArray[i];       hn.item = charfreq[i];        hn.left = null;       hn.right = null;        q.add(hn);     }      HuffmanNode root = zilch;      while (q.size() > i) {        HuffmanNode x = q.peek();       q.poll();        HuffmanNode y = q.peek();       q.poll();        HuffmanNode f = new HuffmanNode();        f.item = x.item + y.detail;       f.c = '-';       f.left = ten;       f.correct = y;       root = f;        q.add(f);     }     Organisation.out.println(" Char | Huffman lawmaking ");     Organization.out.println("--------------------");     printCode(root, "");   } }              
                // Huffman Coding in C  #include <stdio.h> #include <stdlib.h>  #ascertain MAX_TREE_HT 50  struct MinHNode {   char detail;   unsigned freq;   struct MinHNode *left, *right; };  struct MinHeap {   unsigned size;   unsigned capacity;   struct MinHNode **array; };  // Create nodes struct MinHNode *newNode(char item, unsigned freq) {   struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode));    temp->left = temp->right = Zippo;   temp->item = item;   temp->freq = freq;    render temp; }  // Create min heap struct MinHeap *createMinH(unsigned capacity) {   struct MinHeap *minHeap = (struct MinHeap *)malloc(sizeof(struct MinHeap));    minHeap->size = 0;    minHeap->capacity = capacity;    minHeap->assortment = (struct MinHNode **)malloc(minHeap->chapters * sizeof(struct MinHNode *));   return minHeap; }  // Function to bandy void swapMinHNode(struct MinHNode **a, struct MinHNode **b) {   struct MinHNode *t = *a;   *a = *b;   *b = t; }  // Heapify void minHeapify(struct MinHeap *minHeap, int idx) {   int smallest = idx;   int left = 2 * idx + one;   int correct = ii * idx + 2;    if (left < minHeap->size && minHeap->assortment[left]->freq < minHeap->assortment[smallest]->freq)     smallest = left;    if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)     smallest = right;    if (smallest != idx) {     swapMinHNode(&minHeap->array[smallest], &minHeap->assortment[idx]);     minHeapify(minHeap, smallest);   } }  // Check if size if i int checkSizeOne(struct MinHeap *minHeap) {   return (minHeap->size == ane); }  // Excerpt min struct MinHNode *extractMin(struct MinHeap *minHeap) {   struct MinHNode *temp = minHeap->array[0];   minHeap->array[0] = minHeap->assortment[minHeap->size - one];    --minHeap->size;   minHeapify(minHeap, 0);    return temp; }  // Insertion function void insertMinHeap(struct MinHeap *minHeap, struct MinHNode *minHeapNode) {   ++minHeap->size;   int i = minHeap->size - 1;    while (i && minHeapNode->freq < minHeap->array[(i - i) / two]->freq) {     minHeap->array[i] = minHeap->array[(i - one) / two];     i = (i - ane) / ii;   }   minHeap->array[i] = minHeapNode; }  void buildMinHeap(struct MinHeap *minHeap) {   int north = minHeap->size - 1;   int i;    for (i = (n - 1) / ii; i >= 0; --i)     minHeapify(minHeap, i); }  int isLeaf(struct MinHNode *root) {   return !(root->left) && !(root->right); }  struct MinHeap *createAndBuildMinHeap(char item[], int freq[], int size) {   struct MinHeap *minHeap = createMinH(size);    for (int i = 0; i < size; ++i)     minHeap->array[i] = newNode(detail[i], freq[i]);    minHeap->size = size;   buildMinHeap(minHeap);    render minHeap; }  struct MinHNode *buildHuffmanTree(char detail[], int freq[], int size) {   struct MinHNode *left, *right, *height;   struct MinHeap *minHeap = createAndBuildMinHeap(item, freq, size);    while (!checkSizeOne(minHeap)) {     left = extractMin(minHeap);     right = extractMin(minHeap);      acme = newNode('$', left->freq + correct->freq);      elevation->left = left;     height->correct = right;      insertMinHeap(minHeap, top);   }   return extractMin(minHeap); }  void printHCodes(struct MinHNode *root, int arr[], int summit) {   if (root->left) {     arr[elevation] = 0;     printHCodes(root->left, arr, peak + 1);   }   if (root->right) {     arr[top] = ane;     printHCodes(root->right, arr, peak + ane);   }   if (isLeaf(root)) {     printf("  %c   | ", root->particular);     printArray(arr, pinnacle);   } }  // Wrapper office void HuffmanCodes(char detail[], int freq[], int size) {   struct MinHNode *root = buildHuffmanTree(item, freq, size);    int arr[MAX_TREE_HT], superlative = 0;    printHCodes(root, arr, pinnacle); }  // Print the assortment void printArray(int arr[], int due north) {   int i;   for (i = 0; i < n; ++i)     printf("%d", arr[i]);    printf("\northward"); }  int main() {   char arr[] = {'A', 'B', 'C', 'D'};   int freq[] = {5, i, six, three};    int size = sizeof(arr) / sizeof(arr[0]);    printf(" Char | Huffman code ");   printf("\n--------------------\n");    HuffmanCodes(arr, freq, size); }              
                // Huffman Coding in C++  #include <iostream> using namespace std;  #define MAX_TREE_HT 50  struct MinHNode {   unsigned freq;   char item;   struct MinHNode *left, *right; };  struct MinH {   unsigned size;   unsigned capacity;   struct MinHNode **array; };  // Creating Huffman tree node struct MinHNode *newNode(char particular, unsigned freq) {   struct MinHNode *temp = (struct MinHNode *)malloc(sizeof(struct MinHNode));    temp->left = temp->right = NULL;   temp->item = detail;   temp->freq = freq;    return temp; }  // Create min heap using given capacity struct MinH *createMinH(unsigned capacity) {   struct MinH *minHeap = (struct MinH *)malloc(sizeof(struct MinH));   minHeap->size = 0;   minHeap->capacity = capacity;   minHeap->array = (struct MinHNode **)malloc(minHeap->capacity * sizeof(struct MinHNode *));   return minHeap; }  // Impress the array void printArray(int arr[], int n) {   int i;   for (i = 0; i < due north; ++i)     cout << arr[i];    cout << "\north"; }  // Swap role void swapMinHNode(struct MinHNode **a, struct MinHNode **b) {   struct MinHNode *t = *a;   *a = *b;   *b = t; }  // Heapify void minHeapify(struct MinH *minHeap, int idx) {   int smallest = idx;   int left = 2 * idx + 1;   int right = 2 * idx + 2;    if (left < minHeap->size && minHeap->assortment[left]->freq < minHeap->array[smallest]->freq)     smallest = left;    if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)     smallest = right;    if (smallest != idx) {     swapMinHNode(&minHeap->array[smallest],            &minHeap->assortment[idx]);     minHeapify(minHeap, smallest);   } }  // Check if size if ane int checkSizeOne(struct MinH *minHeap) {   return (minHeap->size == 1); }  // Extract the min struct MinHNode *extractMin(struct MinH *minHeap) {   struct MinHNode *temp = minHeap->assortment[0];   minHeap->array[0] = minHeap->array[minHeap->size - one];    --minHeap->size;   minHeapify(minHeap, 0);    return temp; }  // Insertion void insertMinHeap(struct MinH *minHeap, struct MinHNode *minHeapNode) {   ++minHeap->size;   int i = minHeap->size - 1;    while (i && minHeapNode->freq < minHeap->array[(i - 1) / two]->freq) {     minHeap->array[i] = minHeap->array[(i - 1) / two];     i = (i - i) / ii;   }    minHeap->array[i] = minHeapNode; }  // BUild min heap void buildMinHeap(struct MinH *minHeap) {   int n = minHeap->size - 1;   int i;    for (i = (n - 1) / two; i >= 0; --i)     minHeapify(minHeap, i); }  int isLeaf(struct MinHNode *root) {   render !(root->left) && !(root->right); }  struct MinH *createAndBuildMinHeap(char item[], int freq[], int size) {   struct MinH *minHeap = createMinH(size);    for (int i = 0; i < size; ++i)     minHeap->array[i] = newNode(item[i], freq[i]);    minHeap->size = size;   buildMinHeap(minHeap);    return minHeap; }  struct MinHNode *buildHfTree(char item[], int freq[], int size) {   struct MinHNode *left, *right, *top;   struct MinH *minHeap = createAndBuildMinHeap(item, freq, size);    while (!checkSizeOne(minHeap)) {     left = extractMin(minHeap);     right = extractMin(minHeap);      top = newNode('$', left->freq + right->freq);      top->left = left;     top->correct = correct;      insertMinHeap(minHeap, top);   }   return extractMin(minHeap); } void printHCodes(struct MinHNode *root, int arr[], int top) {   if (root->left) {     arr[meridian] = 0;     printHCodes(root->left, arr, top + one);   }    if (root->right) {     arr[top] = 1;     printHCodes(root->correct, arr, top + 1);   }   if (isLeaf(root)) {     cout << root->item << "  | ";     printArray(arr, superlative);   } }  // Wrapper function void HuffmanCodes(char item[], int freq[], int size) {   struct MinHNode *root = buildHfTree(particular, freq, size);    int arr[MAX_TREE_HT], top = 0;    printHCodes(root, arr, elevation); }  int main() {   char arr[] = {'A', 'B', 'C', 'D'};   int freq[] = {5, 1, 6, three};    int size = sizeof(arr) / sizeof(arr[0]);    cout << "Char | Huffman code ";   cout << "\northward----------------------\n";   HuffmanCodes(arr, freq, size); }              

Huffman Coding Complication

The time complexity for encoding each unique character based on its frequency is O(nlog n).

Extracting minimum frequency from the priority queue takes identify two*(northward-1) times and its complexity is O(log n). Thus the overall complexity is O(nlog due north).


Huffman Coding Applications

  • Huffman coding is used in conventional compression formats like GZIP, BZIP2, PKZIP, etc.
  • For text and fax transmissions.

How To Sort An Array In Increasing Order In Java,

Source: https://www.programiz.com/dsa/huffman-coding

Posted by: florywitabir.blogspot.com

0 Response to "How To Sort An Array In Increasing Order In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel