-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaps.cpp
77 lines (74 loc) · 1.51 KB
/
Heaps.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
#include <iostream>
int a[1010];
int count = 0;
int n;
int preorder(int v)
{
if(v*2+1<n)
preorder(v * 2 + 1);
if(v*2+2<n)
preorder(v * 2 + 2);
count++;
std::cout << a[v];
if(count==n)
{
std::cout << std::endl;
}
else
{
std::cout << " ";
}
return 0;
}
int main()
{
int k;
std::cin >> k >> n;
for (int i = 0; i < k; i++)
{
for (int j = 0; j < n; j++)
{
std::cin >> a[j];
}
int flag = 0;
for (int j = 0; j < n; j++)
{
if (j * 2 + 1 < n)
{
if (a[j] < a[j * 2 + 1])
flag = 1;
}
if (j * 2 + 2 < n)
{
if (a[j] < a[j * 2 + 2])
flag = 1;
}
}
if (flag == 0)
std::cout << "Max Heap" << std::endl;
else
{
flag = 0;
for (int j = 0; j < n; j++)
{
if (j * 2 + 1 < n)
{
if (a[j] > a[j * 2 + 1])
flag = 1;
}
if (j * 2 + 2 < n)
{
if (a[j] > a[j * 2 + 2])
flag = 1;
}
}
if (flag == 0)
std::cout << "Min Heap" << std::endl;
else
std::cout << "Not Heap" << std::endl;
}
count = 0;
preorder(0);
}
return 0;
}