-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPQ.hpp
executable file
·225 lines (176 loc) · 5.74 KB
/
MPQ.hpp
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
//
// MPQ.hpp
// skyline
//
// Created by Bahadır on 17.12.2017.
// Copyright © 2017 Bahadır. All rights reserved.
//
#ifndef MPQ_hpp
#define MPQ_hpp
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
template <typename comparable>
class priorityQueue {
public:
explicit priorityQueue(int capacity): heapArr(capacity+1), location(500), size(0) {}
// location i da doldurmayi unutma
void insert(comparable value, int label){
heapEntry obj;
obj.value = value;
obj.label = label;
heapArr[++size] = obj;
location[label] = size;
bubble_up(size);
heapify();
}
// Pops the head
comparable pop(){
if (size > 0)
return Remove(heapArr[1].label);
else
throw "Array is empty";
}
// removes and returns the head (max)
comparable Remove(int label){
int loc_on_heap = location[label];
if (loc_on_heap == -1)
throw "No item exists with that label!\n" ;
comparable output = heapArr[loc_on_heap].value;
swap(loc_on_heap, size);
location[label] = -1;
--size;
bubble_down(loc_on_heap);
heapify();
return output;
}
// returns the max value
comparable GetMax(){
if (IsEmpty())
throw "IsEmpty!\n";
else
return heapArr[1].value;
}
// Get the max of label
int GetMaxLabel(){
if (IsEmpty())
throw "IsEmpty!\n";
else
return heapArr[1].label;
}
comparable GetByLabel(int label){
int loc = location[label];
if (loc == -1)
throw "No item exists with that label!\n" ;
return heapArr[loc].value;
}
bool IsEmpty(){
if (size == 0)
return 1;
return 0;
}
void print(){
cout << "heapArr: ";
for (int i = 1 ; i <= size ; i++){
cout << heapArr[i].value << " ";
}
cout << endl;
/*
cout << "location: " << endl;
for (int i = 0 ; i < location.size(); i++){
cout << "(" << i << ")" << location[i] << endl;
}
cout << endl;
*/
}
private:
struct heapEntry{
comparable value;
int label;
heapEntry(const comparable & val = comparable(), int lab = 0)
: value(val), label(lab)
{}
};
vector<heapEntry> heapArr;
vector<int> location;
int size;
// useful functions
int getLeftChildIndex(int parentIndex) { return parentIndex*2; }
int getRightChildIndex(int parentIndex) { return parentIndex*2 + 1; }
int getParentIndex(int childIndex) { return childIndex / 2; }
bool hasLeftChild(int index) { return getLeftChildIndex(index) < size; }
bool hasRightChild(int index) { return getRightChildIndex(index) < size; }
bool hasParent(int index) { return getParentIndex(index) > 0; }
// Swaps the given indexes in heapArr. Also organize the location array
void swap(int indexOne, int indexTwo){
auto temp = heapArr[indexOne];
int labelOne = heapArr[indexOne].label;
int labelTwo = heapArr[indexTwo].label;
location[labelOne] = indexTwo;
location[labelTwo] = indexOne;
heapArr[indexOne] = heapArr[indexTwo];
heapArr[indexTwo] = temp;
}
// Percolate up
void bubble_up(int index){
while (hasParent(index) && heapArr[getParentIndex(index)].value < heapArr[index].value){
swap(getParentIndex(index), index);
index = getParentIndex(index);
}
}
// Percolate down
void bubble_down(int index){
if (hasLeftChild(index) && hasRightChild(index)){
int rightChildIndex = getRightChildIndex(index);
int leftChildIndex = getLeftChildIndex(index);
if (heapArr[rightChildIndex].value > heapArr[leftChildIndex].value){
if (heapArr[rightChildIndex].value > heapArr[index].value)
swap(rightChildIndex, index);
}
else{
if (heapArr[leftChildIndex].value > heapArr[index].value)
swap(leftChildIndex, index);
}
}
else if (hasRightChild(index)){
int rightChildIndex = getRightChildIndex(index);
if (heapArr[rightChildIndex].value > heapArr[index].value)
swap(rightChildIndex, index);
}
else if (hasLeftChild(index)){
int leftChildIndex = getLeftChildIndex(index);
if (heapArr[leftChildIndex].value > heapArr[index].value)
swap(leftChildIndex, index);
}
}
/* Starts with size/2 and goes up one by one.
for each index check if the index is bigger than the
child node(s). if not swap
*/
void heapify(){
int index = size/2;
if (size > 3){
for (; index > 0; index--)
bubble_down(index);
}
// if size is 3 or less the algorithm above does not work.
// So do it manually :)
else if (size == 3){
heapEntry obj1 = heapArr[1];
heapEntry obj2 = heapArr[2];
heapEntry obj3 = heapArr[3];
if (obj2.value > obj3.value && obj1.value < obj2.value)
swap(1, 2);
else if (obj3.value > obj2.value && obj1.value < obj3.value)
swap(1,3);
}
else if (size == 2){
heapEntry obj1 = heapArr[1];
heapEntry obj2 = heapArr[2];
if (obj1.value < obj2.value)
swap(1,2);
}
}
};
#endif /* MPQ_hpp */