-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
145 lines (134 loc) · 3.55 KB
/
decode.cpp
File metadata and controls
145 lines (134 loc) · 3.55 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
#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;
}
int main()
{
string characters, freq, enc, total1, total2;
getline(cin, characters);
getline(cin, freq);
getline(cin, enc);
getline(cin, total1);
getline(cin, total2);
vector<pi>count;
for(int i = 0; i < characters.length(); i+=2)
{
int charint = freq[i] - '0';
count.push_back(make_pair(charint, characters[i]));
}
//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;
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));
}
TreeNode*tree = treeQueue.top().second;
TreeNode*tempTree = tree;
string message;
for(int it = 0; it < enc.length(); it++)
{
char t = enc[it];
if(tempTree->left == nullptr && tempTree->right == nullptr)
{
message = message + tempTree->value;
tempTree = tree;
}
else
{
if(t == '1')
{
tempTree = tempTree->right;
}
else if(t == '0')
{
tempTree = tempTree->left;
}
}
}
cout << message << endl;
return 0;
}