-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.cpp
139 lines (117 loc) · 3.96 KB
/
12.cpp
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
/*Write a c++ program to implement Huffman coding text compression algorithm.
Build the huffman Tree using characters and their frequencies input from
user. Encode a given string by using codes generated from huffman tree and
decode the bit sequence entered by the user. */
#include <iostream>
#include <queue>
#include <unordered_map>
#include <string>
#include <sstream>
using namespace std;
// Huffman tree node
struct Node {
char data;
int frequency;
Node *left, *right;
Node(char data, int frequency) {
this->data = data;
this->frequency = frequency;
left = right = nullptr;
}
};
// Comparator for priority queue
struct compare {
bool operator()(Node* l, Node* r) {
return l->frequency > r->frequency;
}
};
// Function to build Huffman Tree and return the root
Node* buildHuffmanTree(unordered_map<char, int>& freqmap) {
priority_queue<Node*, vector<Node*>, compare> minHeap;
// Create a leaf node for each character and add it to the min heap
for (auto it : freqmap) {
minHeap.push(new Node(it.first, it.second));
}
// Build the Huffman Tree
while (minHeap.size() != 1) {
// Extract the two minimum frequency nodes from the min heap
Node* left = minHeap.top();
minHeap.pop();
Node* right = minHeap.top();
minHeap.pop();
// Create a new internal node with these two nodes as children and with a frequency equal to the sum of their frequencies
Node* newNode = new Node('$', left->frequency + right->frequency);
newNode->left = left;
newNode->right = right;
// Add the new node to the min heap
minHeap.push(newNode);
}
// The root of the Huffman Tree is the only node left in the min heap
return minHeap.top();
}
// Function to traverse the Huffman Tree and generate Huffman Codes
void generateHuffmanCodes(Node* root, string code, unordered_map<char, string>& huffmanCodes) {
if (root == nullptr)
return;
// If the node is a leaf node, then it contains a character and its code
if (!root->left && !root->right) {
huffmanCodes[root->data] = code;
}
// Traverse left
generateHuffmanCodes(root->left, code + "0", huffmanCodes);
// Traverse right
generateHuffmanCodes(root->right, code + "1", huffmanCodes);
}
// Function to encode a given string using Huffman Codes
string encode(string text, unordered_map<char, string>& huffmanCodes) {
string encodedText = "";
for (char c : text) {
encodedText += huffmanCodes[c];
}
return encodedText;
}
// Function to decode the bit sequence using Huffman Tree
string decode(Node* root, string encodedText) {
string decodedText = "";
Node* current = root;
for (char bit : encodedText) {
if (bit == '0')
current = current->left;
else
current = current->right;
// If it's a leaf node, append the character to the decoded text and reset to root
if (!current->left && !current->right) {
decodedText += current->data;
current = root;
}
}
return decodedText;
}
int main() {
// Frequency map of characters
unordered_map<char, int> freqmap = {
{'A', 12},
{'B', 15},
{'C', 7},
{'D', 13},
{'E', 9}
};
// Build the Huffman Tree
Node* root = buildHuffmanTree(freqmap);
// Generate Huffman Codes
unordered_map<char, string> huffmanCodes;
generateHuffmanCodes(root, "", huffmanCodes);
// Print Huffman Codes
cout << "Huffman Codes:\n";
for (auto it : huffmanCodes) {
cout << it.first << " : " << it.second << endl;
}
// Encode a given string
string text = "ABCDE";
string encodedText = encode(text, huffmanCodes);
cout << "\nEncoded Text: " << encodedText << endl;
// Decode the encoded text
string decodedText = decode(root, encodedText);
cout << "Decoded Text: " << decodedText << endl;
return 0;
}