-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.hpp
88 lines (68 loc) · 2.28 KB
/
huffman.hpp
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
//Header Guards to prevent header files from being included multiple times
#ifndef HUFFMAN_HPP
#define HUFFMAN_HPP
#include <string>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
//Defining Huffman Tree Node
struct Node {
char data;
unsigned freq;
string code;
Node *left, *right;
Node() {
left = right = NULL;
}
};
class huffman {
private:
vector <Node*> arr;
fstream inFile, outFile;
string inFileName, outFileName;
Node *root;
class Compare {
public:
bool operator() (Node* l, Node* r)
{
return l->freq > r->freq;
}
};
priority_queue <Node*, vector<Node*>, Compare> minHeap;
//Initializing a vector of tree nodes representing character's ascii value and initializing its frequency with 0
void createArr();
//Traversing the constructed tree to generate huffman codes of each present character
void traverse(Node*, string);
//Function to convert binary string to its equivalent decimal value
int binToDec(string);
//Function to convert a decimal number to its equivalent binary string
string decToBin(int);
//Reconstructing the Huffman tree while Decoding the file
void buildTree(char, string&);
//Creating Min Heap of Nodes by frequency of characters in the input file
void createMinHeap();
//Constructing the Huffman tree
void createTree();
//Generating Huffman codes
void createCodes();
//Saving Huffman Encoded File
void saveEncodedFile();
//Saving Decoded File to obtain the original File
void saveDecodedFile();
//Reading the file to reconstruct the Huffman tree
void getTree();
public:
//Constructor
huffman(string inFileName, string outFileName)
{
this->inFileName = inFileName;
this->outFileName = outFileName;
createArr();
}
//Compressing input file
void compress();
//Decrompressing input file
void decompress();
};
#endif