-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
221 lines (204 loc) · 5.29 KB
/
encode.cpp
File metadata and controls
221 lines (204 loc) · 5.29 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <stack>
using namespace std;
typedef pair<int, char> pi;
class TreeNode{
public:
TreeNode *left = nullptr;
TreeNode *right = nullptr;
char value = '*';
int frequency = -1;
bool operator<(const TreeNode &other){
if(frequency < other.frequency){
return true;
}else if(frequency > other.frequency){
return false;
}else if(value < other.value){
return true;
}else {
return false;
}
}
void preorder(){
cout << value << "[" << frequency << "] ";
if(left) left->preorder();
if(right) right->preorder();
}
};
typedef pair<int, TreeNode*> ti;
TreeNode *merge(TreeNode *t1, TreeNode* t2){
// Return a new node with t1 and t2 as children
TreeNode *output = new TreeNode();
if(*t1 < *t2)
{
output->left = t1;
output->right = t2;
}
else
{
output->left = t2;
output->right = t1;
}
output->frequency = t1->frequency + t2->frequency;
return output;
}
void inOrderTrav(TreeNode*root)
{
if(root->right != nullptr)
{
inOrderTrav(root->right);
}
cout<<root->frequency << " " << root->value << endl;
if(root->left != nullptr)
{
inOrderTrav(root->left);
}
}
vector<pair<char, string>> code(TreeNode*root)
{
string code = "";
vector <pair<char, string>> ret;
stack<pair<string, TreeNode*>> stk;
stk.push(make_pair(code, root));
while(stk.size()>0)
{
pair<string, TreeNode*> temp = stk.top();
stk.pop();
if(temp.second->value != '*')
{
ret.push_back(make_pair(temp.second->value, temp.first));
}
if(temp.second->left != nullptr)
{
string tmpcode = temp.first + "0";
stk.push(make_pair(tmpcode, temp.second->left));
}
if(temp.second->right != nullptr)
{
string tmpcode = temp.first + "1";
stk.push(make_pair(tmpcode, temp.second->right));
}
}
return ret;
}
string find(vector<pair<char, string>> vec, char t)
{
for(int i = 0; i<vec.size(); i++)
{
if(vec[i].first == t)
{
return vec[i].second;
}
}
}
int main()
{
// This is how to use the tree
// TreeNode *l1 = new TreeNode();
// TreeNode *l2 = new TreeNode();
// l1->value = 'i';
// l1->frequency = 4;
// l2->value = 'h';
// l2->frequency = 4;
// l1->preorder(); cout << endl;
// if(*l1 < *l2){
// cout << "L1 < L2" << endl;
// }else{
// cout << "L1 >= L2" << endl;
// }
string line;
getline(cin, line);
// Build frequency table
vector<pi>count;
//Building frequencies
for(int i = 0; i < line.size(); i++)
{
char temp = line[i];
bool tmpfound = false;
for(int j = 0; j<count.size(); j++)
{
if(count[j].second == temp)
{
count[j].first += 1;
tmpfound = true;
}
}
if(!tmpfound)
{
count.push_back(make_pair(1,temp));
}
tmpfound = false;
}
//Declaring and inserting frequencies into priority queue. Sorted in ascending order (pi == pair<int, char>)
priority_queue<pi, vector<pi>, greater<pi>>pq;
int size = count.size();
for(int c = 0; c<count.size(); c++)
{
pq.emplace(count[c]);
}
//Printing Items
priority_queue<pi, vector<pi>, greater<pi>> temp = pq;
for(int i = 0; i < pq.size(); i++)
{
cout<<temp.top().second << " ";
temp.pop();
}
cout<<endl;
temp = pq;
for(int j = 0; j<pq.size(); j++)
{
cout<<temp.top().first<<" ";
temp.pop();
}
cout<<endl;
// Create the tree
//Converting each pair(pi) into a treenode
priority_queue<ti, vector<ti>, greater<ti>> treeQueue;
int tsize = size;
while(pq.size() > 0)
{
TreeNode *tmp = new TreeNode;
tmp->frequency = pq.top().first;
tmp->value = pq.top().second;
pq.pop();
treeQueue.emplace(make_pair(tmp->frequency, tmp));
}
while(treeQueue.size()>1)
{
//cout<<treeQueue.top().first << " " << treeQueue.top().second << endl;
TreeNode*tmpNode1 = new TreeNode;
TreeNode*tmpNode2 = new TreeNode;
TreeNode*mergeNode = new TreeNode;
tmpNode1 = treeQueue.top().second;
treeQueue.pop();
tmpNode2 = treeQueue.top().second;
treeQueue.pop();
mergeNode = merge(tmpNode1, tmpNode2);
treeQueue.emplace(make_pair(mergeNode->frequency, mergeNode));
}
// Encode the string
TreeNode*root = treeQueue.top().second;
vector<pair<char, string>>codes = code(root);
// Output encoding
string tempStr = "";
for(int i = 0; i<line.size(); i++)
{
tempStr = tempStr + find(codes, line[i]);
}
cout<<tempStr << endl;
int countP = 1;
int counter = 0;
while(countP < codes.size())
{
countP *= 2;
counter++;
}
int totalBitsCod = tempStr.length();
int totalBitsUnc = 8*line.length();
cout << "Total Bits (Original):" << totalBitsUnc << endl << "Total Bits (Coded):" << totalBitsCod << endl;
return 0;
}