-
Notifications
You must be signed in to change notification settings - Fork 1
/
HCTree.h
73 lines (54 loc) · 1.92 KB
/
HCTree.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
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
/**
@author Rajdeep Konwar (A53225609)
@date Nov 10 2017
@section OVERVIEW
Declaration of class HCTree's member functions and variables.
@section ASSIGNMENT NUMBER
PA3
*/
#ifndef HCTREE_H
#define HCTREE_H
#include <vector>
#include "HCNode.h"
#include "BitInputStream.h"
#include "BitOutputStream.h"
//! A 'function class' for use as the Compare class in the HCNode priority_queue
class HCNodePtrComp {
public:
bool operator()( HCNode* &lhs, HCNode* &rhs ) const {
return *lhs < *rhs;
}
};
//! Huffman Code Tree class.
class HCTree {
private:
HCNode *root; //! Root of the Huffman tree
vector< HCNode* > leaves; //! Leaves of the tree
//! Does a postorder traversal, deleting nodes
static void deleteAll( HCNode *n );
public:
//! Explicit keyword is used to avoid accidental implicit conversions
explicit HCTree() : root(nullptr) {
leaves = vector< HCNode* >( 256, (HCNode*) nullptr );
}
//! Default destructor
~HCTree();
//! Build Huffman Code Tree for compressor
void buildTreeComp( const vector< int > &freqs, unsigned int length,
BitOutputStream &bout );
//! Write header info to compressed file
void writeHeader( HCNode *node, BitOutputStream &bitOut );
//! Write character count to compressed file
void writeCharCount( unsigned int length, BitOutputStream &bitOut );
//! Build Huffman Code Tree for uncompressor
unsigned int buildTreeUncomp( BitInputStream &in );
//! Read header info from compressed file
void readHeader( HCNode *par, HCNode* &node, BitInputStream &bitIn );
//! Read character count from compressed file
unsigned int readCharCount( BitInputStream &bitIn );
//! Encode symbol to compressed file
void encode( byte symbol, BitOutputStream &out ) const;
//! Decode symbol from compressed file
int decode( BitInputStream &in ) const;
};
#endif // HCTREE_H