PAT甲级2018冬季7-4 1155 Heap Paths(30 分)

雨点打透心脏的1/2处 2023-02-24 08:46 155阅读 0赞

算法笔记总目录
关键英语单词解释
1155 Heap Paths(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))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line 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, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

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

Sample Input 1:

  1. 8
  2. 98 72 86 60 65 12 23 50

Sample Output 1:

  1. 98 86 23
  2. 98 86 12
  3. 98 72 65
  4. 98 72 60 50
  5. Max Heap

Sample Input 2:

  1. 8
  2. 8 38 25 58 52 82 70 60

Sample Output 2:

  1. 8 25 70
  2. 8 25 82
  3. 8 38 52
  4. 8 38 58 60
  5. Min Heap

Sample Input 3:

  1. 8
  2. 10 28 15 12 34 9 8 56

Sample Output 3:

  1. 10 15 8
  2. 10 15 9
  3. 10 28 34
  4. 10 28 12 56
  5. Not Heap

代码一来自柳诺
题目大意:给出一颗完全二叉树,打印出从根节点到所有叶节点的路径,打印顺序先右后左,即先序遍历的镜像。然后判断该树是大顶堆、小顶堆或者不是堆~

分析:1.深搜打印出所有路径(从右往左,即先序的镜像),vector保存一路上的节点,通过push和pop回溯,维护路径,index <= n是对只有左叶节点没有右叶节点的点特判
2.判断是否为堆:从第二个节点开始遍历,如果比父节点小,就不是小顶堆,如果比父节点大,就不是大顶堆~

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<int> v;
  5. int a[1009], n, isMin = 1, isMax = 1;
  6. void dfs(int index) {
  7. if (index * 2 > n && index * 2 + 1 > n) {
  8. if (index <= n) {
  9. for (int i = 0; i < v.size(); i++)
  10. printf("%d%s", v[i], i != v.size() - 1 ? " " : "\n");
  11. }
  12. } else {
  13. v.push_back(a[index * 2 + 1]);
  14. dfs(index * 2 + 1);
  15. v.pop_back();
  16. v.push_back(a[index * 2]);
  17. dfs(index * 2);
  18. v.pop_back();
  19. }
  20. }
  21. int main() {
  22. cin >> n;
  23. for (int i = 1; i <= n; i++)
  24. scanf("%d", &a[i]);
  25. v.push_back(a[1]);
  26. dfs(1);
  27. for (int i = 2; i <= n; i++) {
  28. if (a[i/2] > a[i]) isMin = 0;
  29. if (a[i/2] < a[i]) isMax = 0;
  30. }
  31. if (isMin == 1)
  32. printf("Min Heap");
  33. else
  34. printf("%s", isMax == 1 ? "Max Heap" : "Not Heap");
  35. return 0;
  36. }

代码二
完全层序建树(练习使用,考试第一种方法即可)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int val;
  5. node *lchild,*rchild;
  6. };
  7. int n,level[1010],x=0,a[1010],isMin = 1, isMax = 1;
  8. node *create(int x,node *root)//1号位开始
  9. {
  10. if(x>n) return NULL;
  11. root->val=level[x];
  12. root->lchild=create(x*2,new node);
  13. root->rchild=create(x*2+1,new node);
  14. return root;
  15. }
  16. void printTree(node *root){
  17. if(root->rchild== NULL && root->lchild== NULL){
  18. printf("%d",a[0]);
  19. for(int i=1;i<x;i++) printf(" %d",a[i]);
  20. printf(" %d\n",root->val);
  21. return;
  22. }
  23. a[x]=root->val;
  24. x++;
  25. if(root->rchild != NULL)printTree(root->rchild);
  26. if(root->lchild != NULL)printTree(root->lchild);
  27. x--;
  28. }
  29. int main(){
  30. scanf("%d",&n);
  31. node *root = new node;
  32. for (int i = 1; i <= n; i++)
  33. scanf("%d", &level[i]);
  34. printTree(create(1,root));
  35. for (int i = 2; i <= n; i++) {
  36. if (level[i/2] > level[i]) isMin = 0;
  37. if (level[i/2] < level[i]) isMax = 0;
  38. }
  39. if (isMin == 1)
  40. printf("Min Heap");
  41. else
  42. printf("%s", isMax == 1 ? "Max Heap" : "Not Heap");
  43. return 0;
  44. }

在这里插入图片描述

发表评论

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

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

相关阅读