-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsixtySecond.cpp
78 lines (78 loc) · 1.45 KB
/
sixtySecond.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
#include <iostream>
using namespace std;
struct heap {
int *array;
int count;
int capacity;
int heap_type;
};
heap* create(int cap, int type)
{
heap* temp = new heap();
temp->heap_type = type;
temp->count = 0;
temp->capacity = cap;
temp->array = new int[cap];
return temp;
}
heap* h = create(3,1);
int parent(int i)
{
return (i-1)/2;
}
int left(int i)
{
return (2*i) + 1;
}
int right(int i)
{
return (2*i) + 2;
}
int Maximum(heap* temp)
{
return temp->array[0];
}
void resize(heap* temp) {
int *oldArray = temp->array;
temp->array = new int[temp->capacity * 2];
for (int i = 0; i < temp->capacity; i++) {
temp->array[i] = oldArray[i];
}
temp->capacity *= 2;
delete oldArray;
}
void insert(heap* temp, int x) {
int i;
if (temp->count == temp->capacity) {
resize(temp);
}
i = temp->count;
temp->count++;
while (i >= 0 && x > temp->array[(i-1)/2]) {
temp->array[i] = temp->array[(i-1)/2];
i = (i-1)/2;
}
temp->array[i] = x;
}
void display(heap* temp)
{
for (int i = 0; i < temp->count; i++) {
std::cout << temp->array[i] << ' ';
}
}
int main(int argc, char const *argv[]) {
insert(h, 16);
insert(h, 14);
insert(h, 9);
insert(h, 12);
insert(h, 8);
insert(h, 10);
insert(h, 31);
insert(h, 3);
insert(h, 1);
insert(h, 5);
insert(h, 7);
insert(h, 19);
display(h);
return 0;
}