-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.h
More file actions
286 lines (230 loc) · 7.76 KB
/
Heap.h
File metadata and controls
286 lines (230 loc) · 7.76 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
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
283
284
285
286
//
// Created by Kamil Bonkowski on 30/03/2021.
//
#ifndef DATASTRUCTURES_HEAP_H
#define DATASTRUCTURES_HEAP_H
#include <iostream>
#include <cmath>
class Heap {
int size = 0;
int *items = nullptr;
int *items_new = nullptr;
Timer *timer = timer->getInstance();
public:
Heap(const string &file_name) {
string amount;
string element;
fstream file;
file.open(file_name, ios::in);
if (!file.good()) {
cout << "File "<<file_name<<" does not exist !" << endl;
}else {
getline(file, amount);
items = new int[(atoi(amount.c_str()))];
size = atoi(amount.c_str());
for (int i = 0; i < size; i++) {
getline(file, element);
items[i] = atoi(element.c_str());
}
create_max_heap(size);
}
}
public:
int get_parent_index(int child_index) {
return (int) ceil((child_index - 2) / 2.0);
}
public:
int get_left_child_index(int index) {
return (index * 2) + 1;
}
public:
int get_right_child_index(int index) {
return (index * 2) + 2;
}
public:
bool has_parent(int index) {
return get_parent_index(index) >= 0;
}
public:
bool has_left_child(int index) {
return get_left_child_index(index) < size;
}
public:
int parent(int index) {
return items[get_parent_index(index)];
}
public:
int left_child(int index) {
return items[get_left_child_index(index)];
}
public:
int right_child(int index) {
return items[get_right_child_index(index)];
}
public:
void swap(int parent_index, int child_index) {
int temp = 0;
temp = items[parent_index];
items[parent_index] = items[child_index];
items[child_index] = temp;
cout << "Swaped parent index " << parent_index << " with child index " << child_index << endl;
}
public:
void heapify_up(int index) {
while ((parent(index) < items[index]) && get_parent_index(index) >= 0) {
swap(get_parent_index(index), index);
index = get_parent_index(index);
}
}
public:
void create_max_heap(int size) {
for (int i = 0; i < size; i++) {
heapify_up(i);
}
}
public:
void heapify_down(int index) {
bool terminate = false;
while (has_left_child(index) && !terminate) {
if (left_child(index) > right_child(index)) {
swap(index, get_left_child_index(index));
index = get_left_child_index(index);
} else if (left_child(index) < right_child(index)) {
swap(index, get_right_child_index(index));
index = get_right_child_index(index);
} else if (left_child(index) == right_child(index)) {
swap(index, get_left_child_index(index));
index = get_left_child_index(index);
} else {
terminate = true;
}
}
}
public:
int *create_arr(int size) {
int *arr = new int[size];
return arr;
}
public:
int get_size() {
this->size = size;
return size;
}
public:
void remove(int index) {
int times = 0;
if (index < size && index >= 0) {
if (size > 0) {
size--;
items_new = create_arr(size);
cout << "How many times should time be measured : ";
cin >> times;
for (int i = 0; i < times; i++) {
auto start = std::chrono::steady_clock::now();// START [HEAP REMOVE]
for (int j = 0; j < index; j++) {
items_new[j] = items[j];
}
items_new[index] = items[size];
for (int k = index + 1; k < size; k++) {
items_new[k] = items[k];
}
delete[]items;
items = items_new;
heapify_down(index);
auto end = std::chrono::steady_clock::now();
double elapsed_time = double(
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
timer->calculate_average_elapsed_time(elapsed_time, "REMOVE_FROM_HEAP");
timer->showAvgTime("REMOVE_FROM_HEAP");
}
cout << "heap[" << index << "] has been removed !" << endl;
} else {
cout << "Heap is empty !" << endl;
}
} else {
cout << "No index found !" << endl;
}
}
public:
void add() {
int times = 0;
int value = 0;
size++;
items_new = create_arr(size);
cout << "heap.add(" << size - 1 << ") = ";
cin >> value;
cout << "How many times should time be measured : ";
cin >> times;
for (int i = 0; i < times; i++) {
auto start = std::chrono::steady_clock::now();// START [HEAP ADD]
for (int i = 0; i < size - 1; i++) {
items_new[i] = items[i];
}
items_new[size - 1] = value;
delete[]items;
items = items_new;
heapify_up(size - 1);
auto end = std::chrono::steady_clock::now();
double elapsed_time = double(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
timer->calculate_average_elapsed_time(elapsed_time, "ADD_TO_HEAP");
timer->showAvgTime("ADD_TO_HEAP");
}
}
public:
void find() {
int times = 0;
int value = 0;
int present = 0;
cout << "Number you are looking for :" << endl;
cin >> value;
cout << "How many times should time be measured : ";
cin >> times;
for (int i = 0; i < times; i++) {
auto start = std::chrono::steady_clock::now();// START [HEAP FIND BY VALUE]
for (int i = 0; i < size; i++) {
if (items[i] == value) {
present++;
}
}
if (present > 0) {
cout << "Number " << value << " is in the heap" << endl;
} else {
cout << "Number was not found" << endl;
}
auto end = std::chrono::steady_clock::now();
double elapsed_time = double(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
timer->calculate_average_elapsed_time(elapsed_time, "FIND_IN_HEAP_BY_VALUE");
timer->showAvgTime("FIND_IN_HEAP_BY_VALUE");
}
}
public:
void find(int index) {
int times = 0;
cout << "How many times should time be measured : ";
cin >> times;
for (int i = 0; i < times; i++) {
auto start = std::chrono::steady_clock::now();// START [HEAP FIND BY INDEX]
if (index >= 0 && index < size) {
cout << "Number at index " << index << " : " << items[index] << endl;
} else {
cout << "There is no such index" << endl;
}
auto end = std::chrono::steady_clock::now();
double elapsed_time = double(std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count());
timer->calculate_average_elapsed_time(elapsed_time, "FIND_IN_HEAP_BY_INDEX");
timer->showAvgTime("FIND_IN_HEAP_BY_INDEX");
}
}
public:
void show() {
if (size > 0) {
for (int i = 0; i < size; i++) {
cout << "heap [" << i << "] : " << items[i] << endl;
}
cout << endl;
}else{
cout<<"Heap is empty !"<<endl;
}
}
};
#endif //DATASTRUCTURES_HEAP_H