-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.hpp
More file actions
49 lines (40 loc) · 1.58 KB
/
HuffmanTree.hpp
File metadata and controls
49 lines (40 loc) · 1.58 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
/*Daud Ahmad Nisar (U37366522) & Adesh Kessani (U69434322)
This code implements Huffman coding for lossless data compression and decompression,
including functions for building a Huffman tree, generating compressed binary output,
serializing the tree, and reconstructing the tree for decompression.*/
#ifndef HUFFMANTREE_HPP
#define HUFFMANTREE_HPP
#include "HuffmanBase.hpp"
#include "HeapQueue.hpp"
#include <map>
class HuffmanTree : public HuffmanTreeBase {
private:
size_t n;
HuffmanNode* root;
public:
std::string compress(const std::string inputStr) override;
std::string serializeTree() const override;
std::string decompress(const std::string inputCode, const std::string serializedTree) override;
//helper method to find the Huffman code for each character
void findCode(const HuffmanNode* node, std::map<char, std::string>& codeMap, std::string code) {
if (node == nullptr) return;
if(node->isLeaf()) {
codeMap[node->getCharacter()] = code;
}
findCode(node->left, codeMap, code + "0");
findCode(node->right, codeMap, code + "1");
}
//post order traversal recursive function to serialize the huffman tree
void serialize(const HuffmanNode* node, std::string& outString) const {
if (node == nullptr) return;
serialize(node->left, outString);
serialize(node->right, outString);
if (node->isLeaf()) {
outString = outString + "L" + node->getCharacter();
}
if (node->isBranch()) {
outString += "B";
}
}
};
#endif