From 81561e6a74710233553a2cc8c91d2623ec9a85a7 Mon Sep 17 00:00:00 2001 From: yashmunoth Date: Mon, 15 Oct 2018 23:04:45 +0530 Subject: [PATCH 1/2] Program for Huffman Coding in C++ --- Greedy Algorithms/huffman.cpp | 108 ++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Greedy Algorithms/huffman.cpp diff --git a/Greedy Algorithms/huffman.cpp b/Greedy Algorithms/huffman.cpp new file mode 100644 index 000000000..be3ed4880 --- /dev/null +++ b/Greedy Algorithms/huffman.cpp @@ -0,0 +1,108 @@ +// C++ program for Huffman Coding +#include +using namespace std; + +// A Huffman tree node +struct MinHeapNode { + + // One of the input characters + char data; + + // Frequency of the character + unsigned freq; + + // Left and right child + MinHeapNode *left, *right; + + MinHeapNode(char data, unsigned freq) + + { + + left = right = NULL; + this->data = data; + this->freq = freq; + } +}; + +// For comparison of +// two heap nodes (needed in min heap) +struct compare { + + bool operator()(MinHeapNode* l, MinHeapNode* r) + + { + return (l->freq > r->freq); + } +}; + +// Prints huffman codes from +// the root of Huffman Tree. +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"); +} + +// The main function that builds a Huffman Tree and +// print codes by traversing the built Huffman Tree +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])); + + // Iterate while size of heap doesn't become 1 + while (minHeap.size() != 1) { + + // Extract the two minimum + // freq items from min heap + left = minHeap.top(); + minHeap.pop(); + + right = minHeap.top(); + minHeap.pop(); + + // Create a new internal node with + // frequency equal to the sum of the + // two nodes frequencies. Make the + // two extracted node as left and right children + // of this new node. Add this node + // to the min heap '$' is a special value + // for internal nodes, not used + top = new MinHeapNode('$', left->freq + right->freq); + + top->left = left; + top->right = right; + + minHeap.push(top); + } + + // Print Huffman codes using + // the Huffman tree built above + printCodes(minHeap.top(), ""); +} + +// Driver program to test above functions +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; +} From 85c3d5aaad5e5f691b28b3a663501a9199ee7217 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Sun, 10 Nov 2019 19:02:54 +0530 Subject: [PATCH 2/2] change header files --- Greedy Algorithms/huffman.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Greedy Algorithms/huffman.cpp b/Greedy Algorithms/huffman.cpp index be3ed4880..253d8d0b5 100644 --- a/Greedy Algorithms/huffman.cpp +++ b/Greedy Algorithms/huffman.cpp @@ -1,5 +1,6 @@ // C++ program for Huffman Coding -#include +#include +#include using namespace std; // A Huffman tree node