-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (128 loc) · 4.23 KB
/
main.cpp
File metadata and controls
165 lines (128 loc) · 4.23 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
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <queue>
using namespace std;
struct Node {
char data;
int freq;
Node* left;
Node* right;
Node(char data, int freq) : data(data), freq(freq), left(nullptr), right(nullptr) {}
};
struct Compare {
bool operator()(Node* a, Node* b) {
return a->freq > b->freq;
}
};
void countFrequencies(const string& input, unordered_map<char, int>& frequencies) {
for (char c : input)
frequencies[c]++;
}
Node* buildHuffmanTree(unordered_map<char, int>& frequencies) {
priority_queue<Node*, vector<Node*>, Compare> pq;
for (auto& pair : frequencies) {
Node* node = new Node(pair.first, pair.second);
pq.push(node);
}
while (pq.size() > 1) {
Node* left = pq.top();
pq.pop();
Node* right = pq.top();
pq.pop();
Node* parent = new Node('\0', left->freq + right->freq);
parent->left = left;
parent->right = right;
pq.push(parent);
}
return pq.top();
}
void buildHuffmanCodes(Node* root, string code, unordered_map<char, string>& huffmanCodes) {
if (root == nullptr)
return;
if (root->data != '\0')
huffmanCodes[root->data] = code;
buildHuffmanCodes(root->left, code + '0', huffmanCodes);
buildHuffmanCodes(root->right, code + '1', huffmanCodes);
}
string compress(const string& input, unordered_map<char, string>& huffmanCodes) {
string compressed;
for (char c : input)
compressed += huffmanCodes[c];
return compressed;
}
string decompress(const string& compressed, Node* root) {
string decompressed;
Node* current = root;
for (char c : compressed) {
if (c == '0')
current = current->left;
else if (c == '1')
current = current->right;
if (current->left == nullptr && current->right == nullptr) {
decompressed += current->data;
current = root;
}
}
return decompressed;
}
void writeCompressedFile(const string& compressed, const string& outputFilename) {
ofstream outfile(outputFilename, ios::binary);
if (outfile) {
int compressedSize = compressed.size();
outfile.write(reinterpret_cast<const char*>(&compressedSize), sizeof(compressedSize));
outfile.write(compressed.c_str(), compressed.size());
cout << "Compressed file written successfully!" << endl;
} else {
cerr << "Error writing compressed file: " << outputFilename << endl;
}
}
string readCompressedFile(const string& inputFilename) {
ifstream infile(inputFilename, ios::binary);
if (infile) {
int compressedSize;
infile.read(reinterpret_cast<char*>(&compressedSize), sizeof(compressedSize));
string compressed;
compressed.resize(compressedSize);
infile.read(&compressed[0], compressedSize);
cout << "Compressed file read successfully!" << endl;
return compressed;
} else {
cerr << "Error reading compressed file: " << inputFilename << endl;
return "";
}
}
int main() {
string inputFilename = "input.txt";
string outputFilename = "compressed.bin";
// Read input file
ifstream infile(inputFilename);
if (!infile) {
cerr << "Error reading input file: " << inputFilename << endl;
return 1;
}
string input((istreambuf_iterator<char>(infile)), (istreambuf_iterator<char>()));
infile.close();
// Count character frequencies
unordered_map<char, int> frequencies;
countFrequencies(input, frequencies);
// Build Huffman tree
Node* root = buildHuffmanTree(frequencies);
// Build Huffman codes
unordered_map<char, string> huffmanCodes;
buildHuffmanCodes(root, "", huffmanCodes);
// Compress input
string compressed = compress(input, huffmanCodes);
// Write compressed file
writeCompressedFile(compressed, outputFilename);
// Read compressed file
string compressedRead = readCompressedFile(outputFilename);
// Decompress
string decompressed = decompress(compressedRead, root);
// Output decompressed data
cout << "Decompressed data:" << endl;
cout << decompressed << endl;
// Clean up memory
delete root;
return 0;
}