1147 Heaps (30 分) 堆的判断

小咪咪 2021-10-30 02:28 356阅读 0赞

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:

  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

题意:n次询问,m个结点,判断是否为大小堆,然后输出后序遍历。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <string>
  4. #include <map>
  5. #include <vector>
  6. #include <queue>
  7. #include <math.h>
  8. #include <algorithm>
  9. #include <iostream>
  10. #define INF 0x3f3f3f3f
  11. using namespace std;
  12. int a[1005],m;
  13. bool ismax()
  14. {
  15. for(int i=0;i<m/2;i++)
  16. {
  17. if(i*2+1<m)
  18. {
  19. if(a[i]<=a[i*2+1])
  20. return false;
  21. }
  22. if(i*2+2<m)
  23. {
  24. if(a[i]<=a[i*2+2])
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. bool ismin()
  31. {
  32. for(int i=0;i<m/2;i++)
  33. {
  34. if(i*2+1<m)
  35. {
  36. if(a[i]>=a[i*2+1])
  37. return false;
  38. }
  39. if(i*2+2<m)
  40. {
  41. if(a[i]>=a[i*2+2])
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. vector<int> v;
  48. void post(int t)
  49. {
  50. if(t>=m)return;
  51. if(t*2+1<m)
  52. post(t*2+1);
  53. if(t*2+2<m)
  54. post(t*2+2);
  55. v.push_back(t);
  56. }
  57. int main()
  58. {
  59. int n,i,j,k;
  60. scanf("%d%d",&n,&m);
  61. while(n--)
  62. {
  63. for(i=0;i<m;i++)
  64. scanf("%d",&a[i]);
  65. if(ismax())
  66. printf("Max Heap\n");
  67. else if(ismin())
  68. printf("Min Heap\n");
  69. else
  70. printf("Not Heap\n");
  71. v.clear();
  72. post(0);
  73. for(i=0;i<v.size();i++)
  74. {
  75. if(i)printf(" ");
  76. printf("%d",a[v[i]]);
  77. }
  78. printf("\n");
  79. }
  80. }

发表评论

表情:
评论列表 (有 0 条评论,356人围观)

还没有评论,来说两句吧...

相关阅读

    相关 (Heap)实现

    什么是堆? 优先队列(Opriority Queue) 特殊的“队列”,取出元素的顺序是依照元素的优先权(关键字)大小,而不是元素进入队列的先后顺序。 ![waterma