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 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

第一种方法,比较笨,重建整棵树,然后判断是否时大根堆和小根堆,然后再遍历出后序遍历

 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 int n, m;
 7 vector<int>level, post;
 8 struct Node
 9 {
10     int val;
11     Node *l, *r;
12     Node(int a = 0) :val(a), l(nullptr), r(nullptr) {}
13 };
14 Node* creatTree(bool &flag, const bool isMax)
15 {
16     Node* root = new Node(level[0]);
17     int k = 1;
18     queue<Node*>q;
19     q.push(root);
20     while (k < m)
21     {
22         Node *p = q.front();
23         q.pop();
24         p->l = new Node(level[k++]);
25         if (isMax && p->val<p->l->val || !isMax && p->val>p->l->val)
26             flag = false;
27         q.push(p->l);
28         if (k >= m)break;
29         p->r = new Node(level[k++]);
30         if (isMax && p->val < p->r->val || !isMax && p->val > p->r->val)
31             flag = false;
32         q.push(p->r);
33     }
34     return root;
35 }
36 void postOrder(Node *root)
37 {
38     if (root == nullptr)
39         return;
40     postOrder(root->l);
41     postOrder(root->r);
42     post.push_back(root->val);
43 }
44 int main()
45 {
46     cin >> n >> m;
47     while (n--)
48     {
49         level.clear();
50         level.resize(m);
51         post.clear();
52         int minN = INT32_MAX, maxN = -1;
53         for (int i = 0; i < m; ++i)
54         {
55             cin >> level[i];
56             minN = minN < level[i] ? minN : level[i];
57             maxN = maxN > level[i] ? maxN : level[i];
58         }
59         bool flag = true, isMax = false;
60         Node *root = nullptr;
61         if (level[0] == minN)//小根堆
62         {
63             isMax = false;
64             root = creatTree(flag, isMax);
65         }
66         else if (level[0] == maxN)
67         {
68             isMax = true;
69             root = creatTree(flag, isMax);
70         }
71         else
72         {
73             flag = false;
74             root = creatTree(flag, isMax);
75         }
76         postOrder(root);
77         if (flag && isMax)
78             printf("Max Heap\n");
79         else if (flag && !isMax)
80             printf("Min Heap\n");
81         else
82             printf("Not Heap\n");
83         for (int i = 0; i < m; ++i)
84             cout << (i == 0 ? "" : " ") << post[i];
85         cout << endl;
86     }
87     return 0;
88 }

第二种方法,简单点,通过完全二叉树的性质,直接判断并得出后序遍历结果

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int n, m;
 5 vector<int>level, post;
 6 void postOrder(int index)
 7 {
 8     if (index >= m)return;
 9     postOrder(index * 2 + 1);
10     postOrder(index * 2 + 2);
11     post.push_back(level[index]);
12 }
13 int main()
14 {
15     cin >> n >> m;
16     while (n--)
17     {
18         level.resize(m);
19         for (int i = 0; i < m; ++i)
20             cin >> level[i];
21         bool isMaxHeap = level[0] >= level[1] ? true : false;
22         bool flag = true;
23         for (int i = 0; i < (m - 1) / 2 && flag; ++i)
24         {
25             int L = i * 2 + 1, R = i * 2 + 2;
26             if (isMaxHeap && (level[i] < level[L] || R < m && level[i] < level[R]))
27                 flag = false;
28             if (!isMaxHeap && (level[i] > level[L] || R<m && level[i] > level[R]))
29                 flag = false;
30         }
31         if (flag && isMaxHeap)
32             printf("Max Heap\n");
33         else if (flag && !isMaxHeap)
34             printf("Min Heap\n");
35         else
36             printf("Not Heap\n");
37         postOrder(0);
38         for (int i = 0; i < m; ++i)
39             cout << (i == 0 ? "" : " ") << post[i];
40         cout << endl;
41     }
42     return 0;
43 }

原文地址:https://www.cnblogs.com/zzw1024/p/11909255.html

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

PAT甲级——1147 Heaps【30】的相关文章

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

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No

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甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

PAT 1072. Gas Station (30)

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.  However it must guarantee that all the houses are in its service range. Now given the map o

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

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

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要