-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
142 lines (114 loc) · 3.48 KB
/
heap.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
#include "heap.h"
int heap::leftChild(int currentIdx) const
{
return 2 * currentIdx;
}
int heap::rightChild(int currentIdx) const
{
// @TODO Update to return the index of the right child.
return 2 * currentIdx + 1;
}
int heap::parent(int currentIdx) const
{
// @TODO Update to return the index of the parent.
return (int) (currentIdx / 2);
}
bool heap::hasAChild(int currentIdx) const
{
// @TODO Update to return whether the given node has a child
return (int) leftChild(currentIdx) < (int) _elems.size();
}
int heap::maxPriorityChild(int currentIdx) const
{
// @TODO Update to return the index of the child with highest priority
/// as defined by higherPriority()
if (rightChild(currentIdx) >= (int) _elems.size()) {
return leftChild(currentIdx);
}
if (_elems[leftChild(currentIdx)].second < _elems[rightChild(currentIdx)].second) {
return leftChild(currentIdx);
} else {
return rightChild(currentIdx);
}
}
void heap::heapifyDown(int currentIdx)
{
// @TODO Implement the heapifyDown algorithm.
if (hasAChild(currentIdx)) {
int min_child = maxPriorityChild(currentIdx);
if (_elems[min_child].second < _elems[currentIdx].second) {
std::swap(_elems[currentIdx], _elems[min_child]);
heapifyDown(min_child);
}
}
}
void heap::heapifyUp(int currentIdx)
{
if (currentIdx == root())
return;
int parentIdx = parent(currentIdx);
if (_elems[currentIdx].second < _elems[parentIdx].second) {
std::swap(_elems[currentIdx], _elems[parentIdx]);
heapifyUp(parentIdx);
}
}
heap::heap()
{
// @TODO Depending on your implementation, this function may or may
/// not need modifying
_elems.push_back(pair<int, double>(1,1.0));
}
heap::heap(const std::vector<pair<int, double>>& elems)
{
// @TODO Construct a heap using the buildHeap algorithm
_elems.push_back(pair<int, double>(1, 1.0));
for (int i = 0; i < (int) elems.size(); i++) {
_elems.push_back(elems[i]);
}
for (int i = (int) _elems.size() - 1; i > 0; i--) {
heapifyDown(i);
}
}
pair<int, double> heap::pop()
{
// @TODO Remove, and return, the element with highest priority
std::swap(_elems[1], _elems[_elems.size() - 1]);
pair<int, double> result = _elems[_elems.size() - 1];
_elems.pop_back();
heapifyDown(root());
return result;
}
pair<int, double> heap::peek() const
{
// @TODO Return, but do not remove, the element with highest priority
return _elems[root()];
}
void heap::push(const pair<int, double>& elem)
{
// @TODO Add elem to the heap
_elems.push_back(elem);
heapifyUp(_elems.size() - 1);
}
void heap::updateElem(const int & idx, const pair<int, double>& elem)
{
// @TODO In-place updates the value stored in the heap array at idx
// Corrects the heap to remain as a valid heap even after update
pair<int, double> old = _elems[idx];
_elems[idx] = elem;
if (_elems[idx].second < old.second) {
heapifyUp(idx);
} else {
heapifyDown(idx);
}
}
bool heap::empty() const
{
// @TODO Determine if the heap is empty
return _elems.size() <= 1;
}
void heap::getElems(std::vector<pair<int, double>> & heaped) const
{
for (int i = root(); i < (int) _elems.size(); i++) {
heaped.push_back(_elems[i]);
}
}