From ac93bbe2335f7c18631db527f8b4750c6bbef13b Mon Sep 17 00:00:00 2001 From: Adit Mehta <35133994+aditmehta9@users.noreply.github.com> Date: Sun, 7 Oct 2018 01:17:37 +0530 Subject: [PATCH] Huffman code in C++ --- huffmancode.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 huffmancode.cpp diff --git a/huffmancode.cpp b/huffmancode.cpp new file mode 100644 index 00000000..280baefe --- /dev/null +++ b/huffmancode.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +struct MinHeapNode { + + char data; + + unsigned freq; + + MinHeapNode *left, *right; + + MinHeapNode(char data, unsigned freq) + + { + + left = right = NULL; + this->data = data; + this->freq = freq; + } +}; + + +struct compare { + + bool operator()(MinHeapNode* l, MinHeapNode* r) + + { + return (l->freq > r->freq); + } +}; + +void 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"); +} + +void HuffmanCodes(char data[], int freq[], int size) +{ + struct MinHeapNode *left, *right, *top; + + // Create a min heap & inserts all characters of data[] + priority_queue, compare> minHeap; + + for (int i = 0; i < size; ++i) + minHeap.push(new MinHeapNode(data[i], freq[i])); + + while (minHeap.size() != 1) { + + + left = minHeap.top(); + minHeap.pop(); + + right = minHeap.top(); + minHeap.pop(); + + top = new MinHeapNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + minHeap.push(top); + } + + printCodes(minHeap.top(), ""); +} + +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; +}