-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorities.cpp
283 lines (191 loc) · 6.86 KB
/
priorities.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//QUESTION 1
#include "priorities.h"
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <string>
#include <map>
//QUESTION 1
Heap::Heap(const vector<string> & dataToAdd) {
for(unsigned int i = 0; i < dataToAdd.size(); i++){
//Node n = {i, dataToAdd.at(i)}; //Create node to push
this->push(dataToAdd.at(i), i);
//content.push_back(n); //Push node directly into vector<Node>
}
}
Heap::Heap(const Heap& h) { //COPY CONSTRUCTOR
//Loop for deep-copy
//Iterate through h, create new node, then push node into this->vector(Node)
for(unsigned int i = 0; i < h.content.size(); i++){ //Safer version
Node n = {h.content.at(i).priority, h.content.at(i).data};
this->content.push_back(n);
}
}
//Question 2
size_t Heap::lengthOfContent(unsigned long index) const {
size_t length = content.at(index).data.length(); //Store length for node @ index
//Recursive left and right trees
if((2*index+1) < content.size()){
length += lengthOfContent(2*index+1);
if((2*index+2) < content.size()){
length += lengthOfContent(2*index+2);
}
}
return length;
}
ostream &operator<<(ostream & out, const Heap& h) { //PrettyPrint()
string str="";
unsigned long numSpace=0;
int element2Do=1;
int elementDone=0;
for(int index=0;index<h.content.size();index++){
//print left child node space
if(2*index+1<h.content.size()){
numSpace=h.lengthOfContent(2*index+1);
for(int sp=0;sp<numSpace;sp++){
str+=" ";
}
}
//print data of node
str+=h.content.at(index).data;
//print right child node space
if(2*index+2<h.content.size()){
numSpace=h.lengthOfContent(2*index+2);
for(int sp=0;sp<numSpace;sp++){
str+=" ";
}
}
elementDone++;
//print next line or right child
if(element2Do==elementDone){
element2Do*=2;
elementDone=0;
str+="\n";
}else{
int kid=index;
while(kid%2==0){
kid=(kid-1)/2;//go to parent
}
unsigned long parent=(kid-1)/2;
numSpace=h.content.at(parent).data.length();
for(int sp=0;sp<numSpace;sp++){
str+=" ";
}
}
}
out<<str;
return out;
}
//QUESTION 3
vector<string> Heap::printLinear() const{
vector<string> v; //Create new vector of strings
for(int i = 0; i < content.size(); i++){ //loop through to create new strings
string item = "(" + content.at(i).data + ", " + to_string(content.at(i).priority) + ")";
v.push_back(item); //push string into vector<string>
}
return v;
}
//QUESTION 4
unsigned int Heap::operator[](string s) const{
//If string matches node data, then return priority. Cycle through elements of CONTENT.
for(int i = 0; i < content.size(); i++){
if (content.at(i).data == s){ //match check
return content.at(i).priority; //return priority
}
}
return 0; //return 0 if nothing found
}
//QUESTION 5
unsigned long getParentIndex(unsigned long i){ //getParentIndex CUSTOM HELPER FUNCTION
unsigned long parentIndex;
parentIndex = ((i - 1)/2);
return parentIndex;
}
void Heap::heapifyUp(unsigned long index) { //HeapifyUp ALGORITHM
unsigned long parentIndex;
if (index != 0) {
parentIndex = getParentIndex(index);
if (content.at(parentIndex).priority > content.at(index).priority) {
swap(content.at(parentIndex), content.at(index)); //use swap function given
heapifyUp(parentIndex);
}
}
}
void Heap::push(string data, unsigned int priority) { //INSERT METHOD
Node n = {priority, data}; //Make new node, n.
content.push_back(n); //push n into the heap's content vector
unsigned long nindex = content.size()-1; //get index of new pushed node, n
heapifyUp(nindex);
}
Heap& Heap::operator+=(const Heap& h) {
int duplicate = 0; //duplicate flag
for(unsigned int i = 0; i < h.content.size(); i++){ //loop for every element in input heap, h
for(unsigned int k = 0; k < this->content.size(); k++){ //inner loop to check duplicature
if(h.content.at(i).data == this->content.at(k).data){
duplicate = 1;
break;
}
duplicate = 0;
}
if(duplicate == 0){//duplicate check
push(h.content.at(i).data, h.content.at(i).priority);
}
}
return *this;
}
// QUESTION 6
void Heap::heapifyDown(unsigned long index) { //HeapifyDown ALGORITHM
unsigned long leftChildIndex, rightChildIndex, minIndex;
leftChildIndex = (2*index)+1;
rightChildIndex = (2*index)+2;
if (rightChildIndex >= content.size()) { //break if out of bounds
if (leftChildIndex >= content.size())
return;
else
minIndex = leftChildIndex;
} else {
if (content.at(leftChildIndex).priority <= content.at(rightChildIndex).priority)
minIndex = leftChildIndex;
else
minIndex = rightChildIndex;
}
if (content.at(index).priority > content.at(minIndex).priority) {
swap(content.at(index), content.at(minIndex));
heapifyDown(minIndex);
}
}
string Heap::pop() {
string top;
if (empty()){ // used content.size() == 0 before / Now using professor's empty() method.
return string("Heap is empty\n");
}
else {
top = content.at(0).data; //store data of to-be-popped node
swap(content.at(0), content.at(content.size() - 1));
if (content.size() > 0){
content.pop_back(); //reduction step (eliminate last node)
heapifyDown(0);
}
}
return top; //might have to be the old?
}
// QUESTION 7
Heap::Heap(istream &in) {
typedef map<string, unsigned int> StrIntMap; //Shortcut for map type
StrIntMap words; //create map
string s; //count frequency
while (in >> s){
++words[s];
}
unsigned int max = 1; //set max
for(StrIntMap::iterator i = words.begin(); i != words.end(); ++i){ //iterate through map
for(StrIntMap::iterator p = words.begin(); p != words.end(); ++p){ //loop to find max
if(p->second > max){
max = p->second; //set running max
}
}
this->push(i->first, (max - i->second)); //push into the heap; i->second is # of times.
//therefore, max subtracted by i->second equals the priority.
}
}