-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanCodes.cpp
More file actions
143 lines (119 loc) · 3.04 KB
/
Copy pathHuffmanCodes.cpp
File metadata and controls
143 lines (119 loc) · 3.04 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//% I collaborated with cristian ortiz
#include<iostream>
#include <deque>
#include<map>
#include<queue>
#include<string>
using namespace std;
map<char, string>C;
struct Node {
char letter;
double freq;
Node* left;
Node* right;
};
struct comparenodes {
// weak collaboration
// bool operator ()(const Node& left, const Node& right) {
bool operator()(Node* left, Node* right){
//help from http://www.cplusplus.com/reference/algorithm/sort/ for the bool operator
//discussed comparison type with cristian ortiz
return (left->freq > right->freq);
}
};
priority_queue<Node *, deque<Node *>, comparenodes> Q;
//priority queue was instructed by the book
//priority_queue <Type, vector<Type>, ComparisonType > min_heap;
// disscused using a priority queue with cristisn ortiz
Node* Getnew(char letter, double freq){
Node* newnode= new Node();
newnode->freq=freq;
newnode->letter=letter;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
Node* empty(){
Node* temp= new Node();
temp->left=NULL;
temp->right=NULL;
temp->letter=NULL;
temp->freq=NULL;
return temp;
}
static void encoding(Node *Q, string enc, int i) {
//weak collaboration
char letters[]={'A','B','C','D','E','F'};
if (Q==NULL) {
return;
}
if (Q->letter == letters[i]) {
C.insert(make_pair (letters[i],enc));
}
if (Q->letter!= letters[i]){
encoding(Q->left, enc + "0", i);
//adds on to the string
encoding(Q->right, enc + "1", i);
}
};
void huffmantree(){
Node *x= empty();
//establish new nodes
Node *y= empty();
//establish new nodes
Node *z=empty();
//establish new nodes
for(int p=6; p!=1;p--){
x = Q.top();
//x = extractmin(Q)
//gets new min
Q.pop();
//min element gets removed
y = Q.top();
Q.pop();
z = Getnew(' ', (x->freq + y->freq));
// gets our addition of both nodes being added based on the frequency
z->left = x;
//makes left child off of the x min
z->right = y;
//makes right child off of the y min
Q.push(z);
}
}
int main(){
double freq;
int x=0;
while (x!=6){
cin>>freq;
if (x==0){
Q.push(Getnew('A', freq));
}
if (x==1){
Q.push(Getnew('B', freq));
}
if (x==2){
Q.push(Getnew('C', freq));
}
if (x==3){
Q.push(Getnew('D', freq));
}
if (x==4){
Q.push(Getnew('E', freq));
}
if (x==5){
Q.push(Getnew('F', freq));
}
x++;
}
huffmantree();
for (int i = 0; i < 6; i++) {
encoding(Q.top(), "",i);
}
//prints out our encoded letters
map<char, string>::iterator it = C.begin();
while(it != C.end()){
//
cout<<it->first<<":"<<it->second<<endl;
it++;
}
}