|
| 1 | +// C++ program for Huffman Coding with STL |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// A Huffman tree node |
| 6 | +struct MinHeapNode { |
| 7 | + |
| 8 | + // One of the input characters |
| 9 | + char data; |
| 10 | + |
| 11 | + // Frequency of the character |
| 12 | + unsigned freq; |
| 13 | + |
| 14 | + // Left and right child |
| 15 | + MinHeapNode *left, *right; |
| 16 | + |
| 17 | + MinHeapNode(char data, unsigned freq) |
| 18 | + |
| 19 | + { |
| 20 | + |
| 21 | + left = right = NULL; |
| 22 | + this->data = data; |
| 23 | + this->freq = freq; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +// For comparison of |
| 28 | +// two heap nodes (needed in min heap) |
| 29 | +struct compare { |
| 30 | + |
| 31 | + bool operator()(MinHeapNode* l, MinHeapNode* r) |
| 32 | + |
| 33 | + { |
| 34 | + return (l->freq > r->freq); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +// Prints huffman codes from |
| 39 | +// the root of Huffman Tree. |
| 40 | +void printCodes(struct MinHeapNode* root, string str) |
| 41 | +{ |
| 42 | + |
| 43 | + if (!root) |
| 44 | + return; |
| 45 | + |
| 46 | + if (root->data != '$') |
| 47 | + cout << root->data << ": " << str << "\n"; |
| 48 | + |
| 49 | + printCodes(root->left, str + "0"); |
| 50 | + printCodes(root->right, str + "1"); |
| 51 | +} |
| 52 | + |
| 53 | +// The main function that builds a Huffman Tree and |
| 54 | +// print codes by traversing the built Huffman Tree |
| 55 | +void HuffmanCodes(char data[], int freq[], int size) |
| 56 | +{ |
| 57 | + struct MinHeapNode *left, *right, *top; |
| 58 | + |
| 59 | + // Create a min heap & inserts all characters of data[] |
| 60 | + priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap; |
| 61 | + |
| 62 | + for (int i = 0; i < size; ++i) |
| 63 | + minHeap.push(new MinHeapNode(data[i], freq[i])); |
| 64 | + |
| 65 | + // Iterate while size of heap doesn't become 1 |
| 66 | + while (minHeap.size() != 1) { |
| 67 | + |
| 68 | + // Extract the two minimum |
| 69 | + // freq items from min heap |
| 70 | + left = minHeap.top(); |
| 71 | + minHeap.pop(); |
| 72 | + |
| 73 | + right = minHeap.top(); |
| 74 | + minHeap.pop(); |
| 75 | + |
| 76 | + // Create a new internal node with |
| 77 | + // frequency equal to the sum of the |
| 78 | + // two nodes frequencies. Make the |
| 79 | + // two extracted node as left and right children |
| 80 | + // of this new node. Add this node |
| 81 | + // to the min heap '$' is a special value |
| 82 | + // for internal nodes, not used |
| 83 | + top = new MinHeapNode('$', left->freq + right->freq); |
| 84 | + |
| 85 | + top->left = left; |
| 86 | + top->right = right; |
| 87 | + |
| 88 | + minHeap.push(top); |
| 89 | + } |
| 90 | + |
| 91 | + // Print Huffman codes using |
| 92 | + // the Huffman tree built above |
| 93 | + printCodes(minHeap.top(), ""); |
| 94 | +} |
| 95 | + |
| 96 | +// Driver Code |
| 97 | +int main() |
| 98 | +{ |
| 99 | + |
| 100 | + char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 101 | + int freq[] = { 5, 9, 12, 13, 16, 45 }; |
| 102 | + |
| 103 | + int size = sizeof(arr) / sizeof(arr[0]); |
| 104 | + |
| 105 | + HuffmanCodes(arr, freq, size); |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +// This code is contributed by Aditya Goel |
0 commit comments