|
| 1 | +--- |
| 2 | +extension: cpp |
| 3 | +author: AnkurSalunkhe11 |
| 4 | +category: Algorithms |
| 5 | +layout: '../../layouts/SubmissionLayout.astro' |
| 6 | +title: Huffman Compression Algorithm |
| 7 | +--- |
| 8 | +```cpp |
| 9 | +#include <iostream> |
| 10 | +#include <string> |
| 11 | +#include <queue> |
| 12 | +#include <unordered_map> |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +#define EMPTY_STRING "" |
| 16 | + |
| 17 | +// A Tree node |
| 18 | +struct Node |
| 19 | +{ |
| 20 | + char ch; |
| 21 | + int freq; |
| 22 | + Node *left, *right; |
| 23 | +}; |
| 24 | + |
| 25 | +// Function to allocate a new tree node |
| 26 | +Node* getNode(char ch, int freq, Node* left, Node* right) |
| 27 | +{ |
| 28 | + Node* node = new Node(); |
| 29 | + |
| 30 | + node->ch = ch; |
| 31 | + node->freq = freq; |
| 32 | + node->left = left; |
| 33 | + node->right = right; |
| 34 | + |
| 35 | + return node; |
| 36 | +} |
| 37 | + |
| 38 | +// Comparison object to be used to order the heap |
| 39 | +struct comp |
| 40 | +{ |
| 41 | + bool operator()(const Node* l, const Node* r) const |
| 42 | + { |
| 43 | + // the highest priority item has the lowest frequency |
| 44 | + return l->freq > r->freq; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +// Utility function to check if Huffman Tree contains only a single node |
| 49 | +bool isLeaf(Node* root) { |
| 50 | + return root->left == nullptr && root->right == nullptr; |
| 51 | +} |
| 52 | + |
| 53 | +// Traverse the Huffman Tree and store Huffman Codes in a map. |
| 54 | +void encode(Node* root, string str, unordered_map<char, string> &huffmanCode) |
| 55 | +{ |
| 56 | + if (root == nullptr) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // found a leaf node |
| 61 | + if (isLeaf(root)) { |
| 62 | + huffmanCode[root->ch] = (str != EMPTY_STRING) ? str : "1"; |
| 63 | + } |
| 64 | + |
| 65 | + encode(root->left, str + "0", huffmanCode); |
| 66 | + encode(root->right, str + "1", huffmanCode); |
| 67 | +} |
| 68 | + |
| 69 | +// Traverse the Huffman Tree and decode the encoded string |
| 70 | +void decode(Node* root, int &index, string str) |
| 71 | +{ |
| 72 | + if (root == nullptr) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + // found a leaf node |
| 77 | + if (isLeaf(root)) |
| 78 | + { |
| 79 | + cout << root->ch; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + index++; |
| 84 | + |
| 85 | + if (str[index] == '0') { |
| 86 | + decode(root->left, index, str); |
| 87 | + } |
| 88 | + else { |
| 89 | + decode(root->right, index, str); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Builds Huffman Tree and decodes the given input text |
| 94 | +void buildHuffmanTree(string text) |
| 95 | +{ |
| 96 | + // base case: empty string |
| 97 | + if (text == EMPTY_STRING) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // count the frequency of appearance of each character |
| 102 | + // and store it in a map |
| 103 | + unordered_map<char, int> freq; |
| 104 | + for (char ch: text) { |
| 105 | + freq[ch]++; |
| 106 | + } |
| 107 | + |
| 108 | + // Create a priority queue to store live nodes of the Huffman tree |
| 109 | + priority_queue<Node*, vector<Node*>, comp> pq; |
| 110 | + |
| 111 | + // Create a leaf node for each character and add it |
| 112 | + // to the priority queue. |
| 113 | + for (auto pair: freq) { |
| 114 | + pq.push(getNode(pair.first, pair.second, nullptr, nullptr)); |
| 115 | + } |
| 116 | + |
| 117 | + // do till there is more than one node in the queue |
| 118 | + while (pq.size() != 1) |
| 119 | + { |
| 120 | + // Remove the two nodes of the highest priority |
| 121 | + // (the lowest frequency) from the queue |
| 122 | + |
| 123 | + Node* left = pq.top(); pq.pop(); |
| 124 | + Node* right = pq.top(); pq.pop(); |
| 125 | + |
| 126 | + // create a new internal node with these two nodes as children and |
| 127 | + // with a frequency equal to the sum of the two nodes' frequencies. |
| 128 | + // Add the new node to the priority queue. |
| 129 | + |
| 130 | + int sum = left->freq + right->freq; |
| 131 | + pq.push(getNode('\0', sum, left, right)); |
| 132 | + } |
| 133 | + |
| 134 | + // `root` stores pointer to the root of Huffman Tree |
| 135 | + Node* root = pq.top(); |
| 136 | + |
| 137 | + // Traverse the Huffman Tree and store Huffman Codes |
| 138 | + // in a map. Also, print them |
| 139 | + unordered_map<char, string> huffmanCode; |
| 140 | + encode(root, EMPTY_STRING, huffmanCode); |
| 141 | + |
| 142 | + cout << "Huffman Codes are:\n" << endl; |
| 143 | + for (auto pair: huffmanCode) { |
| 144 | + cout << pair.first << " " << pair.second << endl; |
| 145 | + } |
| 146 | + |
| 147 | + cout << "\nThe original string is:\n" << text << endl; |
| 148 | + |
| 149 | + // Print encoded string |
| 150 | + string str; |
| 151 | + for (char ch: text) { |
| 152 | + str += huffmanCode[ch]; |
| 153 | + } |
| 154 | + |
| 155 | + cout << "\nThe encoded string is:\n" << str << endl; |
| 156 | + cout << "\nThe decoded string is:\n"; |
| 157 | + |
| 158 | + if (isLeaf(root)) |
| 159 | + { |
| 160 | + // Special case: For input like a, aa, aaa, etc. |
| 161 | + while (root->freq--) { |
| 162 | + cout << root->ch; |
| 163 | + } |
| 164 | + } |
| 165 | + else { |
| 166 | + // Traverse the Huffman Tree again and this time, |
| 167 | + // decode the encoded string |
| 168 | + int index = -1; |
| 169 | + while (index < (int)str.size() - 1) { |
| 170 | + decode(root, index, str); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// Huffman coding algorithm implementation in C++ |
| 176 | +int main() |
| 177 | +{ |
| 178 | + string text = "Huffman coding is a data compression algorithm."; |
| 179 | + buildHuffmanTree(text); |
| 180 | + |
| 181 | + return 0; |
| 182 | +}``` |
0 commit comments