diff --git a/CPP/algorithms/greedy_algorithms/huffman_algo.cpp b/CPP/algorithms/greedy_algorithms/huffman_algo.cpp new file mode 100644 index 00000000..6fc09c0f --- /dev/null +++ b/CPP/algorithms/greedy_algorithms/huffman_algo.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct MinHeapNode +{ + char data; + unsigned freq; + MinHeapNode *left, *right; + + MinHeapNode(char data, unsigned freq) + { + left = right = nullptr; + 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(string text) +{ + map freq; + for (char c : text) + { + freq[c]++; + } + + priority_queue, compare> minHeap; + + for (auto pair : freq) + { + minHeap.push(new MinHeapNode(pair.first, pair.second)); + } + + MinHeapNode *left, *right, *top; + + 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); + } + + cout << "Huffman Codes for the text:\n"; + printCodes(minHeap.top(), ""); +} + +int main() +{ + string str = "huffman coding is a greedy algorithm"; + HuffmanCodes(str); + return 0; +} \ No newline at end of file