-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeap.cpp
189 lines (130 loc) · 4.09 KB
/
MinHeap.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
#include "MinHeap.h"
/*********************************************************************/
MinHeap::MinHeap(vector<int> min) {
build(min);
}
/*********************************************************************/
/* In this bulild function we will use Floyd Algorithm to build the Heap
*/
void MinHeap::build(vector<int> min) {
heap.reserve(min.capacity());
vertexToPlace.reserve(min.capacity());
for (int i = 0; i < min.size(); i++) {
// weight of vertex i = "infinity" or 0 in prim's algorithms
heapNode tmp = { i, min[i] };
// ver Key
heap.push_back(tmp);
vertexToPlace.push_back(i);
}
for (int i = min.size() / 2 - 1; i >= 0; i--) {
fixHeap(i);
}
}
/*********************************************************************/
/* fixHeap is a Recursive function
*/
void MinHeap::fixHeap(int index) {
int indexToCheck = index;
int left = getLeftChild(index);
int right = getRightChild(index);
heapNode tmp;
if (rightChildExists(index) && leftChildExists(index))
{
if (heap[left].weight < heap[right].weight)
indexToCheck = left;
else
indexToCheck = right;
if (heap[index].weight > heap[indexToCheck].weight)
{ //vertex
vertexToPlace[heap[index].data] = indexToCheck;
vertexToPlace[heap[indexToCheck].data] = index;
tmp = heap[index];
heap[index] = heap[indexToCheck];
heap[indexToCheck] = tmp;
fixHeap(indexToCheck);
}
}
else if (!rightChildExists(index) && leftChildExists(index)) {
if (heap[index].weight > heap[left].weight)
{
// vertex
vertexToPlace[heap[index].data] = left;
vertexToPlace[heap[left].data] = index;
tmp = heap[index];
heap[index] = heap[left];
heap[left] = tmp;
fixHeap(left);
}
}
// else, the heap is fixed. No more switches required.
}
/*********************************************************************/
heapNode MinHeap::deleteMin() {
if (heap.size() < 1) {
std::cout << "Heap was empty and deleteMin occurred" << endl;
return {-1, -1};
}
heapNode min = heap[0];
heap[0] = heap[heap.size() - 1];
vertexToPlace[heap[0].data] = 0;
heap.pop_back(); // heapSize--
fixHeap(0);
vertexToPlace[min.data] = NOTINHEAP;
return min;
}
/*********************************************************************/
bool MinHeap::isEmpty() {
if (heap.size() == 0)
return true;
return false;
}
/*********************************************************************/
/* deceaseKey changes the weight of heap[place] to newKey and fixes the heap, if necessary.
* ASSUMPTION: newKey <= heap[place].weight
*/
void MinHeap::deceaseKey(int v, int newKey) {
int place = vertexToPlace[v];
if (place >= heap.size()) {
// ASSUMPTION: vertexToPlace is updated and doesn't conatain a value that is not in the heap range
}
if (newKey > heap[place].weight) {
// During Prim's Algorithm we call this function, with each time with a new key that is lower than the value before.
// Therefore we won't enter this case.
}
heapNode temp = heap[place];
while (place > 0 && heap[getParent(place)].weight > newKey) {
// Fix Upwards:
heap[place] = heap[getParent(place)];
vertexToPlace[heap[getParent(place)].data] = place;
place = getParent(place);
}
heap[place] = temp;
heap[place].weight = newKey;
vertexToPlace[heap[place].data] = place;
}
/*********************************************************************/
bool MinHeap::leftChildExists(int index) {
return (getLeftChild(index) < heap.size());
}
bool MinHeap::rightChildExists(int index) {
return (getRightChild(index) < heap.size());
}
/*********************************************************************/
// Getters:
/*********************************************************************/
int MinHeap::getParent(int index) {
return (index - 1) / 2;
}
int MinHeap::getLeftChild(int index) {
return index * 2 + 1;
}
int MinHeap::getRightChild(int index) {
return index * 2 + 2;
}
/*********************************************************************/
/* ASSUMPION: The heap is not Empty
*/
heapNode MinHeap::getMin() const {
return heap[0];
}
/*********************************************************************/