1147. Heaps (30)

叁歲伎倆 2022-05-27 20:40 304阅读 0赞

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 <= 1000), 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<stdio.h>
  2. int a[1010],m,b[1010],num;
  3. int MaxHeap()
  4. {
  5. int i,j,k;
  6. for(i=0;i<m/2;i++)
  7. {
  8. if(i*2+1<m&&i*2+2<m)
  9. {
  10. k=i*2+1;
  11. if(a[k]<a[i*2+2])
  12. {
  13. k=i*2+2;
  14. }
  15. }
  16. else if(i*2+1<m&&i*2+2>=m)
  17. {
  18. k=i*2+1;
  19. }
  20. if(a[i]<a[k])
  21. {
  22. return 0;
  23. }
  24. }
  25. return 1;
  26. }
  27. int MinHeap()
  28. {
  29. int i,j,k;
  30. for(i=0;i<m/2;i++)
  31. {
  32. if(i*2+1<m&&i*2+2<m)
  33. {
  34. k=i*2+1;
  35. if(a[k]>a[i*2+2])
  36. {
  37. k=i*2+2;
  38. }
  39. }
  40. else if(i*2+1<m&&i*2+2>=m)
  41. {
  42. k=i*2+1;
  43. }
  44. if(a[i]>a[k])
  45. {
  46. return 0;
  47. }
  48. }
  49. return 1;
  50. }
  51. void postorder(int root)
  52. {
  53. if(root<m)
  54. {
  55. postorder(root*2+1);
  56. postorder(root*2+2);
  57. b[num++]=a[root];
  58. }
  59. }
  60. int main()
  61. {
  62. int i,j,n,k,t;
  63. scanf("%d %d",&n,&m);
  64. for(i=0;i<n;i++)
  65. {
  66. for(j=0;j<m;j++)
  67. {
  68. scanf("%d",&a[j]);
  69. }
  70. num=0;
  71. postorder(0);
  72. int flag1=MaxHeap();
  73. if(flag1==1)
  74. {
  75. printf("Max Heap\n");
  76. }
  77. int flag2=MinHeap();
  78. if(flag2==1)
  79. {
  80. printf("Min Heap\n");
  81. }
  82. if(flag1==0&&flag2==0)
  83. {
  84. printf("Not Heap\n");
  85. }
  86. for(j=0;j<m;j++)
  87. {
  88. if(j==0)
  89. {
  90. printf("%d",b[j]);
  91. }
  92. else
  93. {
  94. printf(" %d",b[j]);
  95. }
  96. }
  97. printf("\n");
  98. }
  99. return 0;
  100. }

发表评论

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

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

相关阅读