• A1147 Heaps(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【最大最小堆】


    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

    题意分析:

    参考了柳神的题解

    注意在层序遍历中,如果根节点的标号为i,则它的两个儿子的标号分别为2i和2i+1

    反过来由两个儿子来索引父亲节点,如果儿子的标号为i,则他们的父节点就为i/2

    代码如下:

    1. #include
    2. using namespace std;
    3. int m, n;
    4. vector<int> vec;
    5. void postOrder(int root)
    6. {
    7. if (root > n)return;
    8. postOrder(root * 2);
    9. postOrder(root * 2 + 1);
    10. cout << vec[root];
    11. if (root != 1)cout << " ";
    12. else cout << endl;
    13. }
    14. int main()
    15. {
    16. cin >> m >> n;
    17. vec.resize(n + 1);
    18. while (m--) {
    19. bool minHeap = true, maxHeap = true;
    20. for (int i = 1; i <= n; i++)
    21. cin >> vec[i];
    22. for (int i = 2; i <= n; i++) {
    23. if (vec[i] > vec[i / 2]) {
    24. maxHeap = false;
    25. }
    26. if (vec[i] < vec[i / 2]) {
    27. minHeap = false;
    28. }
    29. }
    30. if (maxHeap)cout << "Max Heap" << endl;
    31. else if (minHeap)cout << "Min Heap" << endl;
    32. else cout << "Not Heap" << endl;
    33. postOrder(1);
    34. }
    35. }

    运行结果如下:

  • 相关阅读:
    ECharts快速入门
    Web学习笔记-中期项目(拳皇)
    Apache Jmeter BeanShell 实现跨文件跨线程全自动获取Token并写入CSV文件
    【JavaEE】JavaScript webAPI的基本知识
    【0907作业】写一个shell脚本,将以下内容放到脚本中
    补充 | gihub渲染失败
    网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
    MySql分区
    Vue---监听div元素宽高改变时echart图表重新resize
    useReducer的用法
  • 原文地址:https://blog.csdn.net/qq_47677800/article/details/126289018