-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
181 lines (151 loc) · 4.67 KB
/
code.cpp
File metadata and controls
181 lines (151 loc) · 4.67 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
#include <iostream>
#include <queue>
#include <unordered_map>
#include <string>
#include <bitset>
struct TreeNode
{
char info;
int freq;
TreeNode* left;
TreeNode* right;
TreeNode(char info, int freq) : info(info), freq(freq), left(nullptr), right(nullptr) {}
};
class HuffmanCode
{
private:
TreeNode* root;
struct CompareNodes
{
bool operator()(const TreeNode* a, const TreeNode* b) const
{
return a->freq > b->freq; // For min heap, lower frequency comes first
}
};
void buildHuffmanTree(const std::unordered_map<char, int>& frequencies);
void buildCodes(TreeNode* node, const std::string& code, std::unordered_map<char, std::string>& codes);
void destroyTree(TreeNode* node);
public:
HuffmanCode(const std::string& str);
std::string encode(const std::string& str);
std::string decode(const std::string& code);
void displayCodeTable();
void displayBits(const std::string& inputString, const std::string& encodedString);
~HuffmanCode();
};
HuffmanCode::HuffmanCode(const std::string& str) : root(nullptr)
{
std::unordered_map<char, int> frequencies;
for (char ch : str)
{
frequencies[ch]++;
}
buildHuffmanTree(frequencies);
}
void HuffmanCode::buildHuffmanTree(const std::unordered_map<char, int>& frequencies)
{
std::priority_queue<TreeNode*, std::vector<TreeNode*>, CompareNodes> minHeap;
for (const auto& entry : frequencies)
{
minHeap.push(new TreeNode(entry.first, entry.second));
}
while (minHeap.size() > 1)
{
TreeNode* left = minHeap.top();
minHeap.pop();
TreeNode* right = minHeap.top();
minHeap.pop();
TreeNode* combined = new TreeNode('\0', left->freq + right->freq);
combined->left = left;
combined->right = right;
minHeap.push(combined);
}
root = minHeap.top();
}
void HuffmanCode::buildCodes(TreeNode* node, const std::string& code, std::unordered_map<char, std::string>& codes)
{
if (!node)
{
return;
}
if (node->info != '\0')
{
codes[node->info] = code;
}
buildCodes(node->left, code + "0", codes);
buildCodes(node->right, code + "1", codes);
}
std::string HuffmanCode::encode(const std::string& str)
{
std::unordered_map<char, std::string> codes;
buildCodes(root, "", codes);
std::string encodedString;
for (char ch : str)
{
encodedString += codes[ch];
}
return encodedString;
}
std::string HuffmanCode::decode(const std::string& code)
{
std::string decodedString;
TreeNode* currentNode = root;
for (char ch : code)
{
currentNode = (ch == '0') ? currentNode->left : currentNode->right;
if (currentNode->info != '\0')
{
decodedString += currentNode->info;
currentNode = root;
}
}
return decodedString;
}
void HuffmanCode::displayCodeTable()
{
std::unordered_map<char, std::string> codes;
buildCodes(root, "", codes);
std::cout << "Character\tHuffman Code" << std::endl;
for (const auto& entry : codes)
{
std::cout << entry.first << "\t\t" << entry.second << std::endl;
}
}
void HuffmanCode::displayBits(const std::string& inputString, const std::string& encodedString)
{
int originalBits = inputString.size() * 8; // Each character takes 8 bits (1 byte)
int encodedBits = encodedString.size();
std::cout << "Bits needed before Huffman Coding: " << originalBits << " bits" << std::endl;
std::cout << "Bits needed after Huffman Coding: " << encodedBits << " bits" << std::endl;
}
void HuffmanCode::destroyTree(TreeNode* node)
{
if (!node)
{
return;
}
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
HuffmanCode::~HuffmanCode()
{
destroyTree(root);
}
int main()
{
std::string inputString;
std::cout << "Enter the string to be encoded by Huffman Coding: ";
std::cin >> inputString;
HuffmanCode huffman(inputString);
std::cout << "\nCode Table:\n";
huffman.displayCodeTable();
std::string encodedString = huffman.encode(inputString);
std::cout << "\nEncoded code for the input string '" << inputString << "' is:\n";
std::cout << encodedString << std::endl;
huffman.displayBits(inputString, encodedString);
std::string decodedString = huffman.decode(encodedString);
std::cout << "\nDecoded string for the input code '" << encodedString << "' is:\n";
std::cout << decodedString << std::endl;
return 0;
}