-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanTree.h
61 lines (42 loc) · 1.22 KB
/
HuffmanTree.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
// HuffmanTree.h
#ifndef __huffman__HuffmanTree__
#define __huffman__HuffmanTree__
#include <iostream>
#include <string>
#include <queue>
#include <fstream>
#include <bitset>
#include "BST.h"
using namespace std;
class CharFreq
{
public:
CharFreq() :letter('\0'), frequency(0), encoding("") { }
CharFreq(unsigned char l, float f) :letter(l), frequency(f), encoding("") { }
unsigned char letter;
float frequency;
string encoding;
bool operator>(const CharFreq &rhs) const
{
return frequency > rhs.frequency;
}
};
class HuffmanTree : BST<CharFreq>
{
public:
HuffmanTree() {}
bool createCodeTree(string filename);
bool encodeFile(string originalFilename, string outputFilename);
bool decodeFile(string filename);
void displayCode(ostream &out);
private:
inline int traverseAndPrint(string &bits, int i)
{
return traverseAndPrint(bits, i, root);
}
int traverseAndPrint(string &bits, int i, BSTNode *cur);
void findEncoding(string prev, BST<CharFreq>::BSTNode* node, char letter);
void findCode(BST<CharFreq>::BSTNode* node);
};
ostream& operator<<(ostream &out, HuffmanTree &code);
#endif /* defined(__huffman__HuffmanTree__) */