zoukankan      html  css  js  c++  java
  • 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
    ");
    79         else if (flag && !isMax)
    80             printf("Min Heap
    ");
    81         else
    82             printf("Not Heap
    ");
    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
    ");
    33         else if (flag && !isMaxHeap)
    34             printf("Min Heap
    ");
    35         else
    36             printf("Not Heap
    ");
    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 }
  • 相关阅读:
    HDU5586 最大连续和 xingxing在努力
    HDU5587 递推式+二分 xingxing在努力
    HDU2639 第k小01背包 xingxing在努力
    Java面向对象之继承
    Java面向对象之封装
    Java面向对象之封装
    Java流程控制语句
    Java流程控制语句
    Java运算符
    Java运算符
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11909255.html
Copyright © 2011-2022 走看看