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

    1. 3 8
    2. 98 72 86 60 65 12 23 50
    3. 8 38 25 58 52 82 70 60
    4. 10 28 15 12 34 9 8 56

    Sample Output:

    1. Max Heap
    2. 50 60 65 72 12 23 86 98
    3. Min Heap
    4. 60 58 52 38 82 70 25 8
    5. Not Heap
    6. 56 12 34 28 9 8 15 10
    1. #include
    2. using namespace std;
    3. int m, n, cnt, flag;
    4. int a[1010], post[1010];
    5. void postorder(int x) {
    6. if (x > n) {
    7. return;
    8. }
    9. postorder(2 * x);
    10. postorder(2 * x + 1);
    11. post[cnt++] = a[x];
    12. }
    13. int main() {
    14. cin >> m >> n;
    15. while (m--) {
    16. cnt = 0;
    17. flag = 0;
    18. for (int i = 1; i <= n; i++) {
    19. cin >> a[i];
    20. }
    21. postorder(1);
    22. for (int i = 1; i <= n; i++) {
    23. if (2 * i <= n && flag != 3) {
    24. if (a[i] < a[i * 2] && flag != 2) {
    25. flag = 1;
    26. } else if (a[i] > a[i * 2] && flag != 1) {
    27. flag = 2;
    28. } else {
    29. flag = 3;
    30. }
    31. }
    32. if (2 * i + 1 <= n && flag != 3) {
    33. if (a[i] < a[i * 2 + 1] && flag != 2) {
    34. flag = 1;
    35. } else if (a[i] > a[i * 2 + 1] && flag != 1) {
    36. flag = 2;
    37. } else {
    38. flag = 3;
    39. }
    40. }
    41. }
    42. if (flag == 1) {
    43. cout << "Min Heap" << endl;
    44. } else if (flag == 2) {
    45. cout << "Max Heap" << endl;
    46. } else if (flag == 3) {
    47. cout << "Not Heap" << endl;
    48. }
    49. for (int i = 0; i < cnt; i++) {
    50. cout << post[i];
    51. if (i != cnt - 1) {
    52. cout << ' ';
    53. }
    54. }
    55. if (m) {
    56. cout << endl;
    57. }
    58. }
    59. return 0;
    60. }
  • 相关阅读:
    Iptables防火墙基础知识介绍
    初识Redis
    记录一次 vue2 前端项目整合过程
    炫酷HTML蜘蛛侠登录页面
    markdown语法大全_Markdown超详细介绍
    【代码随想录】二刷-链表
    [NSSCTF]prize_p1~p5五道题学习
    新闻发稿多少钱一篇?轻松发布新闻一站式发稿服务平台
    ElastaticSearch ---es客户端 TransportClient
    STM32F4XX之串口
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/128127729