In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))
Your job is to tell if a given complete binary tree is a heap.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 100), the number of trees to be tested; and N (1 < N ≤ 1,000), the number of keys in each tree, respectively. Then M lines follow, each contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all. Then in the next line print the tree’s postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.
Sample Input:
3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56
Sample Output:
Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10
本来想用层序遍历来建完全二叉树的,居然建不来,真是汗颜。
因为是满二叉树,设当前结点为i,所以满足左子结点为2i,右为2I+1,对层序遍历数组进行for循环,记得从1号位开始存,在没有越界的情况下,也就是2i<=n或者2i+1<=n,分别判断,如果左子结点大于该结点,证明不是小顶堆,如果大于,证明不是大顶堆,右子结点也是。最后如果不是大不是小,就输出not
最后后序遍历输出,第一号结点单独输出,因为一号结点是根结点在后序遍历最后输出,单独处理就可以了
//先根据层序遍历来建完全二叉树,然后判断是否为堆,再判断是大顶堆还是小顶堆,最后输出后序
#include<iostream>
#include<queue>
using namespace std;
int levelOrder[1005];
int m,n;
struct node{
int val;
node* lchild, *rchild;
};
void heap(){
bool maxHeap = true,minHeap = true;
for(int i = 1; i <= n; i++){
if(2*i <= n){
if(levelOrder[2*i] > levelOrder[i]) maxHeap = false;
if(levelOrder[2*i] < levelOrder[i]) minHeap = false;
}
if(2*i+1<=n){
if(levelOrder[2*i+1] > levelOrder[i]) maxHeap = false;
if(levelOrder[2*i+1] < levelOrder[i]) minHeap = false;
}
}
if(maxHeap == true) cout<<"Max Heap\n";
else if(minHeap == true) cout<<"Min Heap\n";
else cout<<"Not Heap\n";
return;
}
void postTravese(int index){
if(index > n) return;
postTravese(2*index);
postTravese(2*index+1);
if(index!=1) cout<<levelOrder[index]<<" ";
return;
}
int main(){
int x;
cin>>m>>n;
for(int i = 0; i < m; i++){
for(int j = 1; j <= n; j++){
cin>>levelOrder[j];
}
heap();
postTravese(1);
cout<<levelOrder[1]<<endl;
}
return 0;
}
因为是完全二叉树,所以可以通过下标就解决了左右子结点的寻找问题,连静态建树都不用,在testHeap里面,从1号结点遍历到n号结点,每个结点都要访问其左右子树!不用递归,遍历每个结点即可,如果子结点大于自身,那么maxHeap就为false,不要想着把啥设为true,只要设false就行了
//根据层序遍历来建树,然后判断这是否是一个堆
//不用建树了,直接根据完全二叉树的性质来判断
#include<iostream>
#include<vector>
using namespace std;
int m,n;
vector<int> levelOrder;
void testHeap(){
//在左右结点没有越界的情况下,判断左右结点是否均大于or小于根结点
bool maxHeap = true, minHeap = true;
for(int i = 1; i <= n; i++){
if(i*2 <= n){
if(levelOrder[i*2] < levelOrder[i]){
minHeap = false;
}
else if(levelOrder[i*2] > levelOrder[i]){
maxHeap = false;
}
}
if(i*2+1<=n){
if(levelOrder[2*i+1] < levelOrder[i]){
minHeap = false;
}
else if(levelOrder[2*i+1] > levelOrder[i]){
maxHeap = false;
}
}
}
if(!maxHeap && !minHeap) cout<<"Not Heap\n";
else if(maxHeap) cout<<"Max Heap\n";
else cout<<"Min Heap\n";
}
bool flag = true;
void postOrderTrave(int index){
if(index > n) return;
postOrderTrave(index*2);
postOrderTrave(index*2+1);
if(flag){
cout<<levelOrder[index];
flag =false;
}
else{
cout<<" "<<levelOrder[index];
}
}
int main(){
cin>>m>>n;
levelOrder.resize(n+2);
for(int i = 0; i < m; i++){
levelOrder.clear();
flag = true;
for(int j = 1; j <= n; j++){
cin>>levelOrder[j];
}
testHeap();
postOrderTrave(1);
cout<<endl;
}
return 0;
}