-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_heap.cpp
109 lines (102 loc) · 1.8 KB
/
binary_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
// Priority Queue
// Binary Heap
#include <iostream>
using namespace std;
const int N = 1e6+21;
int n, m, a, b, size_n = 0;
int val[N];
int tree[N];
void insert(int v)
{
size_n++;
tree[size_n] = v;
int k = size_n;
while (k>1 && tree[k] > tree[k/2])
{
swap(tree[k], tree[k/2]);
k/=2;
}
}
int max_heap()
{
return tree[1];
}
void restore(int node)
{
int left = 2*node;
int right = 2*node + 1;
int maxi = node;
if (left <= size_n && tree[left] > tree[maxi])
{
maxi = left;
}
if (right <= size_n && tree[right] > tree[maxi])
{
maxi = right;
}
if (maxi > node)
{
swap(tree[node], tree[maxi]);
restore(maxi);
}
}
void delete_max()
{
tree[1] = tree[size_n];
size_n--;
restore(1);
}
void input()
{
cin>>n;
for (int i=1; i<=n; i++)
{
cin>>a;
tree[i] = a;
}
size_n = n;
for (int i=n/2; i>=1; i--)
restore(i);
}
void operations()
{
cin>>m;
for (int i=0; i<m; i++)
{
cin>>a;
if (a==0)
{
cout<<max_heap()<<endl;
}
if (a==1)
{
delete_max();
}
if (a==2)
{
cin>>b;
insert(b);
}
}
}
void print_heap()
{
for (int i=1; i<=size_n; i++)
{
cout<<tree[i]<<" ";
}
cout<<endl;
}
int main()
{
// input takes n (node count) and n node values
input();
print_heap();
// operations takes m (operation count) and m lines formatted as following:
// (operation type, optional: operation parameters)
// MAX: (0) - returns max value in the heap
// DELETE MAX: (1) - deletes max value in the heap
// ADD NODE: (2, v) - adds a node with value v to the heap
operations();
print_heap();
}