-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.h
47 lines (40 loc) · 1.69 KB
/
Huffman.h
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
#ifndef _HUFFMAN_H
#define _HUFFMAN_H
#include <algorithm>
#include <vector>
#include <queue>
using std::make_pair;
using std::pair;
using std::vector;
using std::priority_queue;
class Node{
friend class HuffmanTree;
//Necessary for the priority queue
public:
bool operator<(const Node *other){ return this->character < other->character; }
//Only HuffmanTree class can access the nodes data
private:
Node(const char character_): character(character_),
left(NULL), right(NULL), parent(NULL){}; //Default Constructor
Node *left; //Pointer to left child
Node *right; //Pointer to right child
Node *parent; //Pointer to parent
unsigned char character; //Character of the node
};
class HuffmanTree{
public:
HuffmanTree(const int freq[256]); //Default Constructor
HuffmanTree(const HuffmanTree &); //Copy Constructor
const HuffmanTree & operator=(const HuffmanTree &); //Assignment Operator
~HuffmanTree(); //Destructor
void compress(vector<bool> &, const vector <char> &) const; //Encodes a string into a compressed binary representation
void descompress(vector <char> &, const vector<bool> &) const; //Decodes a compressed binary representation into a string
private:
Node *root; //Root of the Tree
int freq[256]; //Frequency of each character
vector<bool> code[256]; //Codes of each character
void buildCodes(Node *, vector<bool> &); //Build the codes based on the tree
void build(const int freq[256]); //Build the tree based on the frequency array and returns the root
void destroy(Node *); //Destroy the tree starting from the root
};
#endif