TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
huffman.cpp
1// C++ program for Huffman Coding
2#include <iostream>
3#include <queue>
4using namespace std;
5
6// A Huffman tree node
7struct MinHeapNode {
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 left = right = NULL;
21 this->data = data;
22 this->freq = freq;
23 }
24};
25
26void deleteAll(const MinHeapNode* const root) {
27 if (root) {
28 deleteAll(root->left);
29 deleteAll(root->right);
30 delete root;
31 }
32}
33
34// For comparison of
35// two heap nodes (needed in min heap)
36struct compare {
37 bool operator()(const MinHeapNode* const l,
38 const MinHeapNode* const r) const {
39 return l->freq > r->freq;
40 }
41};
42
43// Prints huffman codes from
44// the root of Huffman Tree.
45void printCodes(struct MinHeapNode* root, const string& str) {
46 if (!root)
47 return;
48
49 if (root->data != '$')
50 cout << root->data << ": " << str << "\n";
51
52 printCodes(root->left, str + "0");
53 printCodes(root->right, str + "1");
54}
55
56// The main function that builds a Huffman Tree and
57// print codes by traversing the built Huffman Tree
58void HuffmanCodes(const char data[], const int freq[], int size) {
59 struct MinHeapNode *left, *right;
60
61 // Create a min heap & inserts all characters of data[]
62 priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap;
63
64 for (int i = 0; i < size; ++i)
65 minHeap.push(new MinHeapNode(data[i], freq[i]));
66
67 // Iterate while size of heap doesn't become 1
68 while (minHeap.size() != 1) {
69 // Extract the two minimum
70 // freq items from min heap
71 left = minHeap.top();
72 minHeap.pop();
73
74 right = minHeap.top();
75 minHeap.pop();
76
77 // Create a new internal node with
78 // frequency equal to the sum of the
79 // two nodes frequencies. Make the
80 // two extracted node as left and right children
81 // of this new node. Add this node
82 // to the min heap '$' is a special value
83 // for internal nodes, not used
84 auto* const top = new MinHeapNode('$', left->freq + right->freq);
85
86 top->left = left;
87 top->right = right;
88
89 minHeap.push(top);
90 }
91
92 // Print Huffman codes using
93 // the Huffman tree built above
94 printCodes(minHeap.top(), "");
95 deleteAll(minHeap.top());
96}
97
98// Driver program to test above functions
99int main() {
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}
int main()
Main function.
int data[MAX]
test data