-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
209 lines (124 loc) · 3.63 KB
/
heap.cpp
File metadata and controls
209 lines (124 loc) · 3.63 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
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
// Ana Taylor
// heap.cpp
// function implementation
#include "heap.h"
#include <string>
#include <iostream>
using namespace std;
Heap::Heap(int size) {
heap_size = 0;
array_size = size;
heap = new HeapNode[array_size];
for (int i = 0; i < array_size; i++) {
heap[i].currentPriority = 0;
heap[i].previousPriority = 0;
}
}
Heap::Heap(const Heap &h) {
heap_size = h.heap_size;
array_size = h.array_size;
heap = new HeapNode[array_size];
for (int i = 0; i < array_size; i++) {
heap[i].currentPriority = h.heap[i].currentPriority;
heap[i].previousPriority = h.heap[i].previousPriority;
}
}
Heap& Heap::operator=(const Heap &rhs) {
if (this != &rhs) {
array_size = rhs.array_size;
heap_size = rhs.heap_size;
delete[] heap;
heap = new HeapNode[heap_size];
for (int i = 0; i < array_size; i++) {
heap[i].currentPriority = rhs.heap[i].currentPriority;
heap[i].previousPriority = rhs.heap[i].previousPriority;
}
}
return *this;
}
Heap::~Heap() {
cout << "Destructor now running..." << endl;
delete[] heap;
}
bool Heap::isEmpty() {
return heap_size == 0;
}
void Heap::insert(int key) {
if (heap_size == array_size - 1) {
resize();
}
bubbleUp(key);
}
void Heap::bubbleUp(int key) {
int hole = ++heap_size;
while (hole > 1 && key < heap[hole/2].currentPriority) {
heap[hole] = heap[hole/ 2];
hole = hole/ 2;
}
heap[hole].currentPriority = key;
heap[hole].previousPriority = key;
}
void Heap::resize() {
HeapNode *temp;
int larger = array_size * 2;
temp = new HeapNode[larger];
for (int i = 0; i < larger; i++) {
temp[i].currentPriority = heap[i].currentPriority;
temp[i].previousPriority = heap[i].previousPriority;
}
delete[] heap;
heap = temp;
array_size = larger;
}
int Heap::remove() {
if (heap_size == 0) {
cout << "EMPTY!" << endl;
return 0;
} else {
int head = heap[1].currentPriority;
heap[1].currentPriority = heap[heap_size].currentPriority;
heap_size--;
bubbleDown(1);
return head;
}
}
void Heap::bubbleDown(int hole) {
HeapNode temp = heap[hole];
while ( (hole * 2 <= heap_size && temp.currentPriority > heap[hole * 2].currentPriority)
||
(hole * 2 + 1 <= heap_size && temp.currentPriority > heap[hole * 2 + 1].currentPriority)) {
if ( hole * 2 + 1 <= heap_size && heap[hole * 2 + 1].currentPriority < heap[hole * 2].currentPriority ) {
heap[hole].currentPriority = heap[hole * 2 + 1].currentPriority;
hole = hole * 2 + 1;
} else {
heap[hole].currentPriority = heap[hole * 2].currentPriority;
hole = hole * 2;
}
}
heap[hole] = temp;
}
void Heap::resetPriority(int key) {
int index;
for (int i = 1; i <= heap_size; i++) {
if(heap[i].previousPriority == key) {
index = i;
}
}
//heap[index].previousPriority = heap[index].currentPriority;
heap[index].currentPriority = heap[1].currentPriority - 1;
while (index != 1 && heap[index/2].currentPriority > heap[index].currentPriority) {
HeapNode temp = heap[index];
heap[index] = heap[index/2];
heap[index/2] = temp;
index = index/ 2;
}
}
int Heap::getHeapCapacity() {
return heap_size;
}
int Heap::getPreviousPriority(int i) {
return heap[i].previousPriority;
}
int Heap::getCurrentPriority(int i) {
return heap[i].currentPriority;
}