-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.cpp
More file actions
104 lines (89 loc) · 3.26 KB
/
HuffmanTree.cpp
File metadata and controls
104 lines (89 loc) · 3.26 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
#include "HuffmanTree.hpp"
// Compress the input string
std::string HuffmanTree::compress(const std::string inputStr) {
std::map<char, size_t> charMap;
std::map<char, size_t>::iterator i;
size_t count[256] = {0};
// Read characters, count frequency, and store in array
for (int i = 0; inputStr[i] != '\0'; i++) {
count[inputStr[i]]++;
}
for(int i = 0; i < 256; i++) {
if(count[i] != 0){
charMap.insert(std::pair<char,size_t>((char)i, count[i]));
}
}
// Create Heap Queue and populate it
HeapQueue<HuffmanNode*, HuffmanNode::Compare> queue;
for (i = charMap.begin(); i != charMap.end(); ++i) {
HuffmanNode* node = new HuffmanNode(i->first, i->second);
n++;
queue.insert(node);
}
// Build the Huffman Tree using the Heap Queue
while (queue.size() != 1){
HuffmanNode* left = queue.min();
queue.removeMin();
HuffmanNode* right = queue.min();
queue.removeMin();
size_t totalFrequency = (left->getFrequency() + right->getFrequency());
HuffmanNode* parent = new HuffmanNode('\0', totalFrequency, nullptr, left, right);
left->parent = parent;
right->parent = parent;
queue.insert(parent);
}
root = queue.min();
// Encoding each character in the string using a map
std::map<char, std::string> codeMap;
std::string code = "";
std::string output = "";
for (i = charMap.begin(); i != charMap.end(); ++i) {
findCode(queue.min(), codeMap, code);
}
for (auto i : inputStr) {
output += codeMap[i];
}
return output;
}
// Serialize the tree using recursive helper function
std::string HuffmanTree::serializeTree() const {
if (root == nullptr) return "";
std::string output = "";
serialize(root, output);
return output;
}
// Decompress the compressed string into the original input string
std::string HuffmanTree::decompress(const std::string inputCode, const std::string serializedTree) {
std::string output = "";
std::stack<HuffmanNode*> treeStack;
// Reconstruct the tree using the serializedTree
for(std::string::const_iterator i = serializedTree.begin(); i != serializedTree.end(); i++){
if(*i == 'L'){
i++;
HuffmanNode *n = new HuffmanNode(*i, 0);
treeStack.push(n);
} else {
HuffmanNode* right = treeStack.top();
treeStack.pop();
HuffmanNode* left = treeStack.top();
treeStack.pop();
HuffmanNode* branch = new HuffmanNode('\0', 0, nullptr, left, right);
treeStack.push(branch);
}
}
// Copy whatever remains in the stack to the root of the newly constructed Huffman Tree and clean up
HuffmanNode* root = treeStack.top();
treeStack.pop();
// Reconstruct the text from the code using the Huffman Tree
HuffmanNode* curr = root;
for (auto character : inputCode) {
if (character == '0') curr = curr->left;
else curr = curr->right;
// Once a leaf is reached, add character to output and start again from root
if (curr->isLeaf()) {
output += curr->getCharacter();
curr = root;
}
}
return output;
}