-
Notifications
You must be signed in to change notification settings - Fork 0
/
program6.h
107 lines (90 loc) · 2.24 KB
/
program6.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef PROGRAM4_H
#define PROGRAM4_H
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
typedef struct char_freq{
char c;
double freq;
char_freq(char c, double freq)
:c(c),
freq(freq)
{}
} CFreq;
typedef struct char_code{
char c;
std::string code;
char_code(char c, std::string code)
:c(c),
code(code)
{}
} CCode;
struct HuffTreeNode{
char data;
double freq;
HuffTreeNode* left;
HuffTreeNode* right;
HuffTreeNode(){
data = '\0';
freq = 0;
}
HuffTreeNode(char data, double freq):data(data),
freq(freq),
left(nullptr),
right(nullptr)
{}
~HuffTreeNode()
{
delete left;
delete right;
}
};
struct compare{
bool operator()(HuffTreeNode* l, HuffTreeNode* r){
return (l->freq > r->freq);
}
};
HuffTreeNode* top;
std::vector<CCode> finalCodes;
std::string codes;
std::string genCodeVector2(HuffTreeNode* root, std::string str){
//std::vector<std::string> codes;
if(root == nullptr){
return "";
}
if(root->data != '$'){
finalCodes.push_back( CCode(root->data, str));
codes = codes + root->data+" : "+str+"\n";
}
genCodeVector2(root->left, str+"0");
genCodeVector2(root->right, str+"1");
return codes;
}
//input: vector of CFreqs
//returns: vector of CCodes
std::vector<CCode> getHuffCodes(std::vector<CFreq > cfs){
//can define in separate .cpp file (make this into declaration)
//or define everything here (nothing in targetgtest.cpp)
//following is for compilation purposes
HuffTreeNode* left;
HuffTreeNode* right;
std::priority_queue<HuffTreeNode*, std::vector<HuffTreeNode*>, compare> huffHeap;
for(int i = 0; i < cfs.size(); ++i){
huffHeap.push(new HuffTreeNode(cfs[i].c, cfs[i].freq));
}
while(huffHeap.size() != 1){
left = huffHeap.top();
huffHeap.pop();
right=huffHeap.top();
huffHeap.pop();
top = new HuffTreeNode('$', left->freq + left->freq);
top->left = left;
top->right = right;
huffHeap.push(top);
}
genCodeVector2(huffHeap.top(), "");
return finalCodes;
}
#endif //PROGRAM4_H