PAT 1147 Heaps

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

#include<iostream> //水题
#include<vector>
using namespace std;
vector<int> v;
int m, n, flag=0;
void postorder(int r){
    if(r*2+1<n)
        postorder(r*2+1);
    if(2*r+2<n)
        postorder(2*r+2);
    flag++==0?cout<<v[r]:cout<<" "<<v[r];
}
int main(){
    cin>>m>>n;
    v.resize(n);
    for(int i=0; i<m; i++){
        int type=-1;
        flag=0;
        for(int j=0; j<n; j++)
            cin>>v[j];
        if(v[0]>v[1]) type=1;
        for(int i=0; i<=(n-1)/2; i++)
            if(type==1&&(v[i*2+1]>v[i]||(i*2+2<n&&v[i*2+2]>v[i]))){
                type=0;
                break;
            }else if(type==-1&&(v[i*2+1]<v[i]||(i*2+2<n&&v[i*2+2]<v[i]))){
                type=0;
                break;
            }
        if(type==1) cout<<"Max Heap"<<endl;
        else if(type==-1) cout<<"Min Heap"<<endl;
        else cout<<"Not Heap"<<endl;
        postorder(0);
        cout<<endl;
    }
}

原文地址:https://www.cnblogs.com/A-Little-Nut/p/9652316.html

时间: 2024-08-02 02:37:49

PAT 1147 Heaps的相关文章

PAT甲级——1147 Heaps【30】

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 h

PAT Advanced 1147 Heaps (30分)

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 h

1147. Heaps (30)

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 h

PAT甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

PAT 1009 说反话 C语言

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格. 输出格式:每个测试用例的输出占一行,输出倒序后的句子. 输入样例: Hello World Here I Come 输出样例: Come I Here World Hello 1 #include<stdio.h> 2 #

PAT 1006 换个格式输出 C语言

让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个"百".3个"十".以及个位的4. 输入格式:每个测试输入包含1个测试用例,给出正整数n(<1000). 输出格式:每个测试用例的输出占一行,用规定的格式输出n. 输入样例1: 234 输出样例1: BBSSS1

俺也晒晒pat代码!

最近在练习pat,很多题目不是很好做,一直找不到思路.通过google题目名称可以找到很多前辈的做题记录,这极大的方便了后来者.在此默默谢谢他们! 不过这同时也带了一些问题,有些前辈可能是急于出结果,所以使用了非常麻烦的方法把题目做出来了,有些后来者不经思考草草学习了就也这么做了...俺觉得这不应当是刷题所追求的目标,俺觉得既然练习旧的题目就应当追求超越前人,多想想有没有更巧妙,更简便的方法.否则科学技术也就停滞不前了... 不过俺又想了想或许很多“大牛”的确使用了简单的方法,只是他们没有时间或

pat 1068 动态规划/Fina More Conis

1068. Find More Coins (30) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special re