forked from shreya123-coder/competetivecoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueuq.c
More file actions
174 lines (145 loc) · 3.11 KB
/
Copy pathpriorityqueuq.c
File metadata and controls
174 lines (145 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// C++ Program for Huffman Coding
// using Priority Queue
#include <iostream>
#include <queue>
using namespace std;
// Maximum Height of Huffman Tree.
#define MAX_SIZE 100
class HuffmanTreeNode {
public:
// Stores character
char data;
// Stores frequency of
// the character
int freq;
// Left child of the
// current node
HuffmanTreeNode* left;
// Right child of the
// current node
HuffmanTreeNode* right;
// Initializing the
// current node
HuffmanTreeNode(char character,
int frequency)
{
data = character;
freq = frequency;
left = right = NULL;
}
};
// Custom comparator class
class Compare {
public:
bool operator()(HuffmanTreeNode* a,
HuffmanTreeNode* b)
{
// Defining priority on
// the basis of frequency
return a->freq > b->freq;
}
};
// Function to generate Huffman
// Encoding Tree
HuffmanTreeNode* generateTree(priority_queue<HuffmanTreeNode*,
vector<HuffmanTreeNode*>,
Compare> pq)
{
// We keep on looping till
// only one node remains in
// the Priority Queue
while (pq.size() != 1) {
// Node which has least
// frequency
HuffmanTreeNode* left = pq.top();
// Remove node from
// Priority Queue
pq.pop();
// Node which has least
// frequency
HuffmanTreeNode* right = pq.top();
// Remove node from
// Priority Queue
pq.pop();
// A new node is formed
// with frequency left->freq
// + right->freq
// We take data as '$'
// because we are only
// concerned with the
// frequency
HuffmanTreeNode* node = new HuffmanTreeNode('$',
left->freq + right->freq);
node->left = left;
node->right = right;
// Push back node
// created to the
// Priority Queue
pq.push(node);
}
return pq.top();
}
// Function to print the
// huffman code for each
// character.
// It uses arr to store the codes
void printCodes(HuffmanTreeNode* root,
int arr[], int top)
{
// Assign 0 to the left node
// and recur
if (root->left) {
arr[top] = 0;
printCodes(root->left,
arr, top + 1);
}
// Assign 1 to the right
// node and recur
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
// If this is a leaf node,
// then we print root->data
// We also print the code
// for this character from arr
if (!root->left && !root->right) {
cout << root->data << " ";
for (int i = 0; i < top; i++) {
cout << arr[i];
}
cout << endl;
}
}
void HuffmanCodes(char data[],
int freq[], int size)
{
// Declaring priority queue
// using custom comparator
priority_queue<HuffmanTreeNode*,
vector<HuffmanTreeNode*>,
Compare>
pq;
// Populating the priority
// queue
for (int i = 0; i < size; i++) {
HuffmanTreeNode* newNode
= new HuffmanTreeNode(data[i], freq[i]);
pq.push(newNode);
}
// Generate Huffman Encoding
// Tree and get the root node
HuffmanTreeNode* root = generateTree(pq);
// Print Huffman Codes
int arr[MAX_SIZE], top = 0;
printCodes(root, arr, top);
}
// Driver Code
int main()
{
char data[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int freq[] = { 5, 9, 12, 13, 16, 45 };
int size = sizeof(data) / sizeof(data[0]);
HuffmanCodes(data, freq, size);
return 0;
}