-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathHuffmanCoding.cpp
More file actions
127 lines (90 loc) · 3.01 KB
/
Copy pathHuffmanCoding.cpp
File metadata and controls
127 lines (90 loc) · 3.01 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
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
struct HuffmanNode {
char character;
int frequency;
HuffmanNode* left;
HuffmanNode* right;
HuffmanNode(char ch, int freq) : character(ch), frequency(freq), left(nullptr), right(nullptr) {}
};
struct CompareNodes {
bool operator()(HuffmanNode* lhs, HuffmanNode* rhs) {
return lhs->frequency > rhs->frequency;
}
};
HuffmanNode* buildHuffmanTree(const string& message) {
map<char, int> frequencyMap;
for (char ch : message) {
if (ch != ' ') {
frequencyMap[ch]++;
}
}
priority_queue<HuffmanNode*, vector<HuffmanNode*>, CompareNodes> pq;
for (const auto& pair : frequencyMap) {
pq.push(new HuffmanNode(pair.first, pair.second));
}
while (pq.size() > 1) {
HuffmanNode* left = pq.top();
pq.pop();
HuffmanNode* right = pq.top();
pq.pop();
HuffmanNode* newNode = new HuffmanNode('\0', left->frequency + right->frequency);
newNode->left = left;
newNode->right = right;
pq.push(newNode);
}
return pq.top();
}
void generateHuffmanCodes(HuffmanNode* root, const string& code, map<char, string>& huffmanCodes) {
if (root->left == nullptr && root->right == nullptr) {
huffmanCodes[root->character] = code;
return;
}
generateHuffmanCodes(root->left, code + "0", huffmanCodes);
generateHuffmanCodes(root->right, code + "1", huffmanCodes);
}
string encodeMessage(const string& message, const map<char, string>& huffmanCodes) {
string encodedMessage = "";
for (char ch : message) {
if (ch != ' ') {
encodedMessage += huffmanCodes.at(ch);
}
}
return encodedMessage;
}
string decodeMessage(const string& encodedMessage, const HuffmanNode* root) {
string decodedMessage = "";
const HuffmanNode* currentNode = root;
for (char bit : encodedMessage) {
if (bit == '0') {
currentNode = currentNode->left;
} else {
currentNode = currentNode->right;
}
if (currentNode->left == nullptr && currentNode->right == nullptr) {
decodedMessage += currentNode->character;
currentNode = root;
}
}
return decodedMessage;
}
int main() {
string message;
cout << "Enter a message to encode using Huffman coding: ";
getline(cin, message);
HuffmanNode* huffmanTreeRoot = buildHuffmanTree(message);
map<char, string> huffmanCodes;
generateHuffmanCodes(huffmanTreeRoot, "", huffmanCodes);
cout << "Huffman Codes:" << endl;
for (const auto& pair : huffmanCodes) {
cout << pair.first << ": " << pair.second << endl;
}
string encodedMessage = encodeMessage(message, huffmanCodes);
cout << "Encoded Message: " << encodedMessage << endl;
string decodedMessage = decodeMessage(encodedMessage, huffmanTreeRoot);
cout << "Decoded Message: " << decodedMessage << endl;
return 0;
}