-
Notifications
You must be signed in to change notification settings - Fork 0
/
HUFFMAN.h
68 lines (55 loc) · 1.55 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*************************************************************************
> File Name: HUFFMAN.h
> Author:
> Mail:
> Created Time: Mon 12 Jan 2015 11:20:02 PM CST
************************************************************************/
#include <queue>
#include <algorithm>
#include <fstream>
#include <map>
#include <bitset>
#include <cassert>
#include "BinaryStdIn.h"
#include "BinaryStdOut.h"
using namespace std;
#ifndef _HUFFMAN_H
#define _HUFFMAN_H
struct Htree {
Htree *left;
Htree *right;
int weight;
char ch;
Htree() {left = right = NULL; weight = 0;ch = 0;}
Htree(Htree *l, Htree *r, int v, char c) {left = l; right = r; weight = v; ch = c;}
};
// is the node a leaf node?
static inline bool isLeaf(Htree *node) {
assert ( (node->left == NULL && node->right == NULL) || (node->left != NULL && node->right != NULL) );
return (node->left == NULL && node->right == NULL);
}
class myComparision
{
public:
bool operator () (const Htree* t1, const Htree* t2) {
return t1->weight> t2->weight;
}
};
class HUFFMAN {
typedef map<char, vector<bool> > huffman_dict;
Htree *huffman_Tree;
huffman_dict dict;
BinaryStdOut *binaryStdOut;
BinaryStdIn *binaryStdIn;
void buildTree(string &data, string filename);
void buildDict(Htree *node, vector<bool> &code);
void destroy(Htree *node);
void writeTrie(Htree *node);
Htree *readTrie();
public:
HUFFMAN();
~HUFFMAN();
void encode(string &data, string filename);
string decode(string filename);
};
#endif