PAT甲级1147 Heaps (30分)(c++)
题目描述
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
这题题意很好理解,只有一个概念,学过数据结构的应该都知道,heap(堆),本质是一个完全二叉树,又分为大根堆和小根堆,大根堆就是所有根结点都比子结点大,小根堆就是所有根结点都比子结点小。此题就是给出一个二叉树(通过数组给出),让你判断这是一个大根堆还是小根堆,又或者都不是,然后返回一个二叉树的后序遍历。
分解一下这题的问题,其实核心有两个,一个是判断二叉数是否是大根堆或小根堆,第二个是二叉树的后序遍历。
首先解决一下二叉树的后序遍历,本文是通过c++实现的,先看一下后序遍历函数的代码:
void postorder(int i,int n,vector<int> &ans,vector<int> &vec){
if(i>=n) return ;
postorder(2*i+1,N,ans,vec);
postorder(2*i+2,N,ans,vec);
ans.push_back(vec[i]);
}
其中i是二叉树结点的下标,n是二叉树结点总数,ans是最后的后序遍历数组,vec是原给定的完全二叉树数组。通过一个简单的递归实现,当i下标大于总数n时,说明该结点不存在了,直接返回。否则再调用自身,去访问2*i+1结点,这个就是原结点的左孩子结点,一直访问到没有左孩子,然后再访问2*i+2也就是右孩子结点,最后,将该结点加入ans数组。
实现后序遍历之后,剩下就是判断大根堆和小根堆了。
通过两个flag实现,一个是flag_max,一个是flag_min,从二叉树0下标开始,到(n+1)/2-1,为什么是这个数,这个是最后一个父结点,因为最大的下标是n-1,根据简单的数学推导可以得到这个结论。遍历所有有子结点的结点,如果父结点大于等于子结点,就flag_max++,如果父结点小于等于子结点,则flag_min++。这里还要分两种情况,因为有子结点的父结点,一定有左子结点,因为这是完全二叉树,但是不一定有右子结点,如果强行比较两个子结点,则有可能右子结点下标越界。所以刚开始判断时就区分一下。
最后就是根据两个flag进行判断了,这个比较简单。
上AC代码:
#include<bits/stdc++.h>
using namespace std;
int M,N;
//后序遍历函数
void postorder(int i,int n,vector<int> &ans,vector<int> &vec){
if(i>=n) return ;
postorder(2*i+1,N,ans,vec);
postorder(2*i+2,N,ans,vec);
ans.push_back(vec[i]);
}
int main(){
cin>>M>>N;
for(int i = 0;i<M;i++){
vector<int> vec;
for(int j = 0;j<N;j++){
int a;
cin>>a;
vec.push_back(a);
}
int t = 0;
int flag_max = 0,flag_min = 0;
for(int j = 0;j<(N+1)/2;j++){
if(2*j+2<N){ //这里判断右子树是否存在
if(vec[j]>=vec[2*j+1]&&vec[j]>=vec[2*j+2]){
flag_max++;
}
if(vec[j]<=vec[2*j+1]&&vec[j]<=vec[2*j+2]){
flag_min++;
}
}
else{
if(vec[j]>=vec[2*j+1]){
flag_max++;
}
if(vec[j]<=vec[2*j+1]){
flag_min++;
}
}
}
if(flag_max==(N+1)/2&&flag_min<flag_max){
cout<<"Max Heap"<<endl;
}else if(flag_min==(N+1)/2&&flag_max<flag_min) cout<<"Min Heap"<<endl;
else cout<<"Not Heap"<<endl;
vector<int> ans;
int k = 0;
postorder(k,N,ans,vec);
for(int j = 0;j<N;j++){
cout<<ans[j];
if(j<N-1) cout<<" ";
}
cout<<endl;
}
return 0;
}