-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman_coding_code.cpp
More file actions
280 lines (234 loc) · 7.44 KB
/
Copy pathhuffman_coding_code.cpp
File metadata and controls
280 lines (234 loc) · 7.44 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <string>
using namespace std;
const int MAX_CHARS = 256;
const int MAX_CODE = 256;
struct Node {
char ch;
int freq;
int left;
int right;
bool isLeaf;
};
struct MinHeap {
int data[MAX_CHARS * 2];
int size;
void init() { size = 0; }
void swapAt(int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
void heapifyUp(int i, Node pool[]) {
while (i > 0) {
int parent = (i - 1) / 2;
if (pool[data[parent]].freq > pool[data[i]].freq) {
swapAt(parent, i);
i = parent;
} else {
break;
}
}
}
void heapifyDown(int i, Node pool[]) {
while (true) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int smallest = i;
if (left < size &&
pool[data[left]].freq < pool[data[smallest]].freq)
smallest = left;
if (right < size &&
pool[data[right]].freq < pool[data[smallest]].freq)
smallest = right;
if (smallest != i) {
swapAt(i, smallest);
i = smallest;
} else {
break;
}
}
}
void insert(int nodeIdx, Node pool[]) {
data[size] = nodeIdx;
size++;
heapifyUp(size - 1, pool);
}
int extractMin(Node pool[]) {
int minIdx = data[0];
size--;
data[0] = data[size];
heapifyDown(0, pool);
return minIdx;
}
};
int buildHuffmanTree(Node pool[], int &poolSize,
char chars[], int freqs[], int n) {
MinHeap heap;
heap.init();
for (int i = 0; i < n; i++) {
pool[i].ch = chars[i];
pool[i].freq = freqs[i];
pool[i].left = -1;
pool[i].right = -1;
pool[i].isLeaf = true;
heap.insert(i, pool);
}
poolSize = n;
while (heap.size > 1) {
int leftIdx = heap.extractMin(pool);
int rightIdx = heap.extractMin(pool);
int newIdx = poolSize;
poolSize++;
pool[newIdx].ch = '\0';
pool[newIdx].freq = pool[leftIdx].freq + pool[rightIdx].freq;
pool[newIdx].left = leftIdx;
pool[newIdx].right = rightIdx;
pool[newIdx].isLeaf = false;
heap.insert(newIdx, pool);
}
return heap.extractMin(pool);
}
void generateCodes(Node pool[], int nodeIdx,
int code[], int depth,
int codeTable[][MAX_CODE],
int codeLengths[]) {
if (nodeIdx == -1) return;
if (pool[nodeIdx].isLeaf) {
unsigned char c = pool[nodeIdx].ch;
codeLengths[c] = depth;
for (int i = 0; i < depth; i++) {
codeTable[c][i] = code[i];
}
if (depth == 0) {
codeTable[c][0] = 0;
codeLengths[c] = 1;
}
return;
}
code[depth] = 0;
generateCodes(pool, pool[nodeIdx].left,
code, depth + 1,
codeTable, codeLengths);
code[depth] = 1;
generateCodes(pool, pool[nodeIdx].right,
code, depth + 1,
codeTable, codeLengths);
}
void encodeString(const string &text,
int codeTable[][MAX_CODE],
int codeLengths[]) {
cout << "\nEncoded bitstream:\n ";
for (int i = 0; i < (int)text.size(); i++) {
unsigned char c = text[i];
for (int b = 0; b < codeLengths[c]; b++) {
cout << codeTable[c][b];
}
cout << " ";
}
cout << "\n";
}
int countFrequencies(const string &text,
char chars[], int freqs[]) {
int freq[MAX_CHARS] = {0};
for (int i = 0; i < (int)text.size(); i++) {
freq[(unsigned char)text[i]]++;
}
int n = 0;
for (int i = 0; i < MAX_CHARS; i++) {
if (freq[i] > 0) {
chars[n] = (char)i;
freqs[n] = freq[i];
n++;
}
}
return n;
}
void printFrequencyTable(char chars[], int freqs[], int n) {
cout << "\n------------------------------------------\n";
cout << " Char Frequency\n";
cout << "------------------------------------------\n";
for (int i = 0; i < n; i++) {
if (chars[i] == ' ')
cout << " ' ' " << freqs[i] << "\n";
else
cout << " " << chars[i] << " " << freqs[i] << "\n";
}
cout << "------------------------------------------\n";
}
void printCodeTable(char chars[], int n,
int codeTable[][MAX_CODE],
int codeLengths[]) {
int totalBitsFixed = 0;
int totalBitsHuffman = 0;
cout << "\n------------------------------------------\n";
cout << " Char Code\n";
cout << "------------------------------------------\n";
for (int i = 0; i < n; i++) {
unsigned char c = chars[i];
if (c == ' ')
cout << " ' ' ";
else
cout << " " << (char)c << " ";
for (int b = 0; b < codeLengths[c]; b++) {
cout << codeTable[c][b];
}
cout << " (length " << codeLengths[c] << ")\n";
}
cout << "------------------------------------------\n";
}
void printCompressionStats(const string &text,
char chars[], int freqs[], int n,
int codeLengths[]) {
int totalBitsFixed = (int)text.size() * 8;
int totalBitsHuffman = 0;
for (int i = 0; i < n; i++) {
unsigned char c = chars[i];
totalBitsHuffman += freqs[i] * codeLengths[c];
}
cout << "\n------------------------------------------\n";
cout << " Compression Statistics\n";
cout << "------------------------------------------\n";
cout << " Original size (8-bit ASCII): "
<< totalBitsFixed << " bits\n";
cout << " Compressed size (Huffman) : "
<< totalBitsHuffman << " bits\n";
if (totalBitsFixed > 0) {
float ratio = 100.0f - (totalBitsHuffman * 100.0f / totalBitsFixed);
cout << " Space saved : "
<< ratio << "%\n";
}
cout << "------------------------------------------\n";
}
int main() {
cout << "======================================\n";
cout << " Huffman Coding - Greedy\n";
cout << "======================================\n\n";
string text;
cout << "Enter the text to encode:\n ";
getline(cin, text);
if (text.empty()) {
cout << "No text entered. Exiting.\n";
return 0;
}
char chars[MAX_CHARS];
int freqs[MAX_CHARS];
int n = countFrequencies(text, chars, freqs);
cout << "\nFrequency table:";
printFrequencyTable(chars, freqs, n);
Node pool[MAX_CHARS * 2];
int poolSize = 0;
int root = buildHuffmanTree(pool, poolSize, chars, freqs, n);
int codeTable[MAX_CHARS][MAX_CODE] = {};
int codeLengths[MAX_CHARS] = {};
int tempCode[MAX_CODE] = {};
generateCodes(pool, root, tempCode, 0, codeTable, codeLengths);
cout << "\nHuffman code table:";
printCodeTable(chars, n, codeTable, codeLengths);
encodeString(text, codeTable, codeLengths);
printCompressionStats(text, chars, freqs, n, codeLengths);
cout << "\n======================================\n";
cout << " Complexity: O(n log n) [heap ops]\n";
cout << "======================================\n";
return 0;
}