英语
specialized adj 专业的
A common implementation(实施,实现) of a heap is the binary heap
堆的一个常见实现是二进制堆
思路
完全二叉树的性质
判断是否是大顶堆,小顶堆, 只需要判断父节点和子结点是否满足堆的性质
需要注意的是 if(-1) 可以输出元素, if(0)此时非
// 本题主要是 表示if(!) 只有 0 才能表示 if(-1) 同样会输出元素
#include<bits/stdc++.h>
using namespace std;
const int maxsize = 1005;
int tree[maxsize], maxHeap = 1, isHeap = 1, cnt = 0, m, n;
void postOrder(int index) {
if(index >= n) return;
postOrder(index * 2 + 1);
postOrder(index * 2 + 2);
printf("%s%d", cnt++ == 0 ? "" : " ", tree[index]);
}
int main()
{
scanf("%d%d", &m, &n);
while(m--) {
for(int i = 0; i < n; i++) {
scanf("%d", &tree[i]);
}
isHeap = 1;
maxHeap = tree[0] >= tree[1] ? 1 : 0; //
for(int i = 0; i * 2 + 1 < n && isHeap; i++) {
int left = i * 2 + 1, right = i * 2 + 2;
if(maxHeap == 1) {
if(tree[left] > tree[i]) isHeap = -1;
if(right < n && tree[right] > tree[i]) isHeap = -1;
} else {
if(tree[left] < tree[i]) isHeap = -1;
if(right < n && tree[right] < tree[i]) isHeap = -1;
}
}
if(isHeap == -1) printf("Not Heap
");
else {
if(maxHeap) printf("Max Heap
");
else printf("Min Heap
");
}
postOrder(0);
cnt = 0;
printf("
");
}
return 0;
}