Author: Not specified Language: c
Description: Not specified Timestamp: 2018-05-20 23:39:14 +0000
View raw paste Reply
  1. // C program for Huffman Coding
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // This constant can be avoided by explicitly
  6. // calculating height of Huffman Tree
  7. #define MAX_TREE_HT 100
  8.  
  9. // A Huffman tree node
  10. struct MinHeapNode {
  11.  
  12.         // One of the input characters
  13.         char data;
  14.  
  15.         // Frequency of the character
  16.         unsigned freq;
  17.  
  18.         // Left and right child of this node
  19.         struct MinHeapNode *left, *right;
  20. };
  21.  
  22. // A Min Heap: Collection of
  23. // min heap (or Hufmman tree) nodes
  24. struct MinHeap {
  25.  
  26.         // Current size of min heap
  27.         unsigned size;
  28.  
  29.         // capacity of min heap
  30.         unsigned capacity;
  31.  
  32.         // Attay of minheap node pointers
  33.         struct MinHeapNode** array;
  34. };
  35.  
  36. // A utility function allocate a new
  37. // min heap node with given character
  38. // and frequency of the character
  39. struct MinHeapNode* newNode(char data, unsigned freq)
  40. {
  41.         struct MinHeapNode* temp
  42.                 = (struct MinHeapNode*)malloc
  43. (sizeof(struct MinHeapNode));
  44.  
  45.         temp->left = temp->right = NULL;
  46.         temp->data = data;
  47.         temp->freq = freq;
  48.  
  49.         return temp;
  50. }
  51.  
  52. // A utility function to create
  53. // a min heap of given capacity
  54. struct MinHeap* createMinHeap(unsigned capacity)
  55.  
  56. {
  57.  
  58.         struct MinHeap* minHeap
  59.                 = (struct MinHeap*)malloc(sizeof(struct MinHeap));
  60.  
  61.         // current size is 0
  62.         minHeap->size = 0;
  63.  
  64.         minHeap->capacity = capacity;
  65.  
  66.         minHeap->array
  67.                 = (struct MinHeapNode**)malloc(minHeap->
  68. capacity * sizeof(struct MinHeapNode*));
  69.         return minHeap;
  70. }
  71.  
  72. // A utility function to
  73. // swap two min heap nodes
  74. void swapMinHeapNode(struct MinHeapNode** a,
  75.                                         struct MinHeapNode** b)
  76.  
  77. {
  78.  
  79.         struct MinHeapNode* t = *a;
  80.         *a = *b;
  81.         *b = t;
  82. }
  83.  
  84. // The standard minHeapify function.
  85. void minHeapify(struct MinHeap* minHeap, int idx)
  86.  
  87. {
  88.  
  89.         int smallest = idx;
  90.         int left = 2 * idx + 1;
  91.         int right = 2 * idx + 2;
  92.  
  93.         if (left < minHeap->size && minHeap->array[left]->
  94. freq < minHeap->array[smallest]->freq)
  95.                 smallest = left;
  96.  
  97.         if (right < minHeap->size && minHeap->array[right]->
  98. freq < minHeap->array[smallest]->freq)
  99.                 smallest = right;
  100.  
  101.         if (smallest != idx) {
  102.                 swapMinHeapNode(&minHeap->array[smallest],
  103.                                                 &minHeap->array[idx]);
  104.                 minHeapify(minHeap, smallest);
  105.         }
  106. }
  107.  
  108. // A utility function to check
  109. // if size of heap is 1 or not
  110. int isSizeOne(struct MinHeap* minHeap)
  111. {
  112.  
  113.         return (minHeap->size == 1);
  114. }
  115.  
  116. // A standard function to extract
  117. // minimum value node from heap
  118. struct MinHeapNode* extractMin(struct MinHeap* minHeap)
  119.  
  120. {
  121.  
  122.         struct MinHeapNode* temp = minHeap->array[0];
  123.         minHeap->array[0]
  124.                 = minHeap->array[minHeap->size - 1];
  125.  
  126.         --minHeap->size;
  127.         minHeapify(minHeap, 0);
  128.  
  129.         return temp;
  130. }
  131.  
  132. // A utility function to insert
  133. // a new node to Min Heap
  134. void insertMinHeap(struct MinHeap* minHeap,
  135.                                 struct MinHeapNode* minHeapNode)
  136.  
  137. {
  138.  
  139.         ++minHeap->size;
  140.         int i = minHeap->size - 1;
  141.  
  142.         while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
  143.  
  144.                 minHeap->array[i] = minHeap->array[(i - 1) / 2];
  145.                 i = (i - 1) / 2;
  146.         }
  147.  
  148.         minHeap->array[i] = minHeapNode;
  149. }
  150.  
  151. // A standard funvtion to build min heap
  152. void buildMinHeap(struct MinHeap* minHeap)
  153.  
  154. {
  155.  
  156.         int n = minHeap->size - 1;
  157.         int i;
  158.  
  159.         for (i = (n - 1) / 2; i >= 0; --i)
  160.                 minHeapify(minHeap, i);
  161. }
  162.  
  163. // A utility function to print an array of size n
  164. void printArr(int arr[], int n)
  165. {
  166.         int i;
  167.         for (i = 0; i < n; ++i)
  168.                 printf("%d", arr[i]);
  169.  
  170.         printf("\n");
  171. }
  172.  
  173. // Utility function to check if this node is leaf
  174. int isLeaf(struct MinHeapNode* root)
  175.  
  176. {
  177.  
  178.         return !(root->left) && !(root->right);
  179. }
  180.  
  181. // Creates a min heap of capacity
  182. // equal to size and inserts all character of
  183. // data[] in min heap. Initially size of
  184. // min heap is equal to capacity
  185. struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
  186.  
  187. {
  188.  
  189.         struct MinHeap* minHeap = createMinHeap(size);
  190.  
  191.         for (int i = 0; i < size; ++i)
  192.                 minHeap->array[i] = newNode(data[i], freq[i]);
  193.  
  194.         minHeap->size = size;
  195.         buildMinHeap(minHeap);
  196.  
  197.         return minHeap;
  198. }
  199.  
  200. // The main function that builds Huffman tree
  201. struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
  202.  
  203. {
  204.         struct MinHeapNode *left, *right, *top;
  205.  
  206.         // Step 1: Create a min heap of capacity
  207.         // equal to size. Initially, there are
  208.         // modes equal to size.
  209.         struct MinHeap* minHeap = createAndBuildMinHeap(data, freq, size);
  210.  
  211.         // Iterate while size of heap doesn't become 1
  212.         while (!isSizeOne(minHeap)) {
  213.  
  214.                 // Step 2: Extract the two minimum
  215.                 // freq items from min heap
  216.                 left = extractMin(minHeap);
  217.                 right = extractMin(minHeap);
  218.  
  219.                 // Step 3: Create a new internal
  220.                 // node with frequency equal to the
  221.                 // sum of the two nodes frequencies.
  222.                 // Make the two extracted node as
  223.                 // left and right children of this new node.
  224.                 // Add this node to the min heap
  225.                 // '$' is a special value for internal nodes, not used
  226.                 top = newNode('$', left->freq + right->freq);
  227.  
  228.                 top->left = left;
  229.                 top->right = right;
  230.  
  231.                 insertMinHeap(minHeap, top);
  232.         }
  233.  
  234.         // Step 4: The remaining node is the
  235.         // root node and the tree is complete.
  236.         return extractMin(minHeap);
  237. }
  238.  
  239. // Prints huffman codes from the root of Huffman Tree.
  240. // It uses arr[] to store codes
  241. void printCodes(struct MinHeapNode* root, int arr[], int top)
  242.  
  243. {
  244.  
  245.         // Assign 0 to left edge and recur
  246.         if (root->left) {
  247.  
  248.                 arr[top] = 0;
  249.                 printCodes(root->left, arr, top + 1);
  250.         }
  251.  
  252.         // Assign 1 to right edge and recur
  253.         if (root->right) {
  254.  
  255.                 arr[top] = 1;
  256.                 printCodes(root->right, arr, top + 1);
  257.         }
  258.  
  259.         // If this is a leaf node, then
  260.         // it contains one of the input
  261.         // characters, print the character
  262.         // and its code from arr[]
  263.         if (isLeaf(root)) {
  264.  
  265.                 printf("%c: ", root->data);
  266.                 printArr(arr, top);
  267.         }
  268. }
  269.  
  270. // The main function that builds a
  271. // Huffman Tree and print codes by traversing
  272. // the built Huffman Tree
  273. void HuffmanCodes(char data[], int freq[], int size)
  274.  
  275. {
  276.         // Construct Huffman Tree
  277.         struct MinHeapNode* root
  278.                 = buildHuffmanTree(data, freq, size);
  279.  
  280.         // Print Huffman codes using
  281.         // the Huffman tree built above
  282.         int arr[MAX_TREE_HT], top = 0;
  283.  
  284.         printCodes(root, arr, top);
  285. }
  286.  
  287. // Driver program to test above functions
  288. int main()
  289. {
  290.  
  291.         char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
  292.         int freq[] = { 5, 9, 12, 13, 16, 45 };
  293.  
  294.         int size = sizeof(arr) / sizeof(arr[0]);
  295.  
  296.         HuffmanCodes(arr, freq, size);
  297.  
  298.         return 0;
  299. }
View raw paste Reply