-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
285 lines (258 loc) · 7.16 KB
/
main.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
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
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
template <typename T> class heap
{
private://property
T *values;
int size = 0;
int max_size;//static
int mode = 0;
public://processes
heap(int n, int mod);
int add_value(T n);
inline int get_parent(int pos);
inline int get_right_child(int pos);
inline int get_left_child(int pos);
bool is_leaf(int pos);
void swap(int a, int b);
T pop();
void heapify(int pos);
void display();
bool verify_heap();
~heap();
};
template <typename T> bool heap<T>::verify_heap()
{
for (int i = 1; i <= size; i++)
{
if (2*i + 1 <= size)
{
if (get_right_child(i) < max_size)
{
if (mode)
{
if ((values[i] < values[get_right_child(i)]) || (values[i] < values[get_left_child(i)]))
return false;
}
else
{
if ((values[i] > values[get_right_child(i)]) || (values[i] > values[get_left_child(i)]))
return false;
}
}
}
}
return true;
}
template <typename T> heap<T>::heap(int n, int mod)//constructor
{
values = new T[n + 1];
max_size = n;
mode = mod;
}
template <typename T> int heap<T>::add_value(T n)
{
if(size >= max_size)
return 1;
size++;
values[size] = n;
int temp = size;
if (!mode)
{
while(values[temp] < values[get_parent(temp)] && temp != 1)
{
swap(temp, get_parent(temp));
temp = get_parent(temp);
}
}
else
{
while(values[temp] > values[get_parent(temp)] && temp != 1)
{
swap(temp, get_parent(temp));
temp = get_parent(temp);
}
}
return 0;
}
template <typename T> inline int heap<T>::get_parent(int pos)
{
int n = pos/2;
return n;
}
template <typename T> inline int heap<T>::get_right_child(int pos)
{
int n = (pos * 2) +1;
return n;
}
template <typename T> inline int heap<T>::get_left_child(int pos)
{
int n = pos * 2;
return n;
}
template <typename T> bool heap<T>::is_leaf(int pos)//is it at the bottom
{
if((pos > size / 2) && (pos <= size))
return true;
return false;
}
template <typename T> void heap<T>::swap(int a, int b)//swaps any 2 values
{
T buf;
buf = values[a];
values[a] = values[b];
values[b] = buf;
}
template <typename T> T heap<T>::pop()
{
if (size > 0)
{
T temp = values[1];
values[1] = values[size];
size--;
heapify(1);
return temp;
}
return 0;
}
template <typename T> void heap<T>::heapify(int pos)
{
if(!is_leaf(pos))
{
if (!mode)
{
if ((get_left_child(pos) < max_size) && (get_right_child(pos) < max_size))
{
if(values[pos] > values[get_left_child(pos)] || values[pos] > values[get_right_child(pos)])
{
if(values[get_left_child(pos)] < values[get_right_child(pos)])
{
swap(pos, get_left_child(pos));
heapify(get_left_child(pos));
}
else
{
swap(pos, get_right_child(pos));
heapify(get_right_child(pos));
}
}
}
}
else
{
if ((get_left_child(pos) < max_size) && (get_right_child(pos) < max_size))
{
if(values[pos] < values[get_left_child(pos)] || values[pos] < values[get_right_child(pos)])
{
if(values[get_left_child(pos)] > values[get_right_child(pos)])
{
swap(pos, get_left_child(pos));
heapify(get_left_child(pos));
}
else
{
swap(pos, get_right_child(pos));
heapify(get_right_child(pos));
}
}
}
}
}
}
template <typename T> void heap<T>::display()
{
for (int i = 1; i < size + 1; i++)
cout << values[i] << ' '; // delete later
cout << endl;
for(int i = 1; i < (size/2) + 1; i++)
{
cout <<"Parent: " <<values[i] << " Left Child: " << values[2*i];
if (2*i + 1 <= size)
cout << " Right Child: "<< values[2*i + 1] << '-' << size << endl;
else
cout << endl;
}
}
template <typename T> heap<T>::~heap()
{
free(values);
}
template <typename T> T inp(string s)
{
cout << s << endl;
T v;
cin>>v;
return v;
}
int main()
{
int seed = time(NULL);
srandom(seed);
string msg;
int size, mode, lastPopped, whereAmI, v;
heap<int>* h;
cout << "// --------------------------------------------------------\n// Test #1 : heap interface works properly\n// --------------------------------------------------------\n";
for (int i = 0; i < 100; i++)
{
size = rand()%100 + 5;
mode = rand()%2;
h = new heap<int>(size, mode);
for (int j = 0; j < size; j++)
{
v = rand()%1000-500;
h->add_value(v);
}
if (mode == 1)
{
whereAmI = 1001;
cout << "size = " + to_string(size) + " mode = " + to_string(mode) << endl;
for (int i = 0 ; i < size ; i++)
{
lastPopped = h->pop();
assert(lastPopped <= whereAmI);
whereAmI = lastPopped;
cout << lastPopped << ' ';
}
cout << endl << endl;
}
else
{
whereAmI = -1000;
cout << "size = " + to_string(size) + " mode = " + to_string(mode) << endl;
for (int i = 0 ; i < size ; i++)
{
lastPopped = h->pop();
assert(lastPopped >= whereAmI);
whereAmI = lastPopped;
cout << lastPopped << ' ';
}
cout << endl << endl;
}
free(h);
}
cout << "Interface works propperly\n";
cout << "// --------------------------------------------------------\n// Test #2 : Limit cases\n// --------------------------------------------------------\n";
h = new heap<int>(10, 0);
h->pop();
cout << "Popping empty heap doesnt break the code\n";
for (int i = 0 ; i < 10001 ; i++)
h->add_value(rand() % 1000 );
free(h);
cout << "Adding too much values fails, but doesn't break\n";
cout << "// --------------------------------------------------------\n// Test #3 : Internal tests\n// --------------------------------------------------------\n";
h = new heap<int>(10, 1);
for (int i = 0 ; i < 10 ; i++)
{
h->add_value(rand() % 1000 );
assert(h->verify_heap());
}
for (int i = 0 ; i < 10 ; i++)
{
h->pop();
assert(h->verify_heap());
}
free(h);
cout << "Verified" << endl;
}