zoukankan      html  css  js  c++  java
  • 1155 Heap Paths

    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), 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:

    8
    98 72 86 60 65 12 23 50
    
     

    Sample Output 1:

    98 86 23
    98 86 12
    98 72 65
    98 72 60 50
    Max Heap
    
     

    Sample Input 2:

    8
    8 38 25 58 52 82 70 60
    
     

    Sample Output 2:

    8 25 70
    8 25 82
    8 38 52
    8 38 58 60
    Min Heap
    
     

    Sample Input 3:

    8
    10 28 15 12 34 9 8 56
    
     

    Sample Output 3:

    10 15 8
    10 15 9
    10 28 34
    10 28 12 56
    Not Heap

    题意:

    给出一组由完全二叉树按照层序遍历的序列,从右到左输出由根节点到各个叶子结点的序列,并判断是大顶堆还是小顶堆。

    思路:

    根据序列先构建这颗完全二叉树,然后在遍历从根节点到叶子结(先右后左),遍历的过程中用一个stack存放路径上的数字,遍历的时候入栈,遍历完右,左结点之后再出栈。判断是大顶堆还是小顶堆的时候,可以用反证的方法,即:如果存在一个根节点小于子节点则一定不是大顶堆,如果存在一个子节点大于根节点则一定不是大顶堆。

    Code:

    #include<iostream>
    #include<queue>
    #include<stack>
    
    using namespace std;
    
    queue<int> que;
    stack<int> path;
    queue<int> temp;
    bool isMaxHeap = true;
    bool isMinHeap = true;
    
    typedef struct Node* node;
    
    struct Node {
        int value;
        node left;
        node right;
        Node() {
            value = 0;
            left = NULL;
            right = NULL;
        }
    };
    
    node root = new Node();
    queue<node> nodeQue;
    
    void buildTree() {
        root->value = que.front();
        que.pop();
        nodeQue.push(root);
        while(!que.empty()) {
            node parent = nodeQue.front(); 
            nodeQue.pop();
            node left = new Node();
            node right = new Node();
            if (!que.empty()) {
                left->value = que.front();
                parent->left = left;
                nodeQue.push(left);
                que.pop();
            }
            if (!que.empty()) {
                right->value = que.front();
                parent->right = right;
                nodeQue.push(right);
                que.pop();
            }
        }
    }
    
    void print(stack<int> path) {
        stack<int> temp;
        bool first = true;
        while (!path.empty()) {
            temp.push(path.top());
            path.pop();
        }
        int last = temp.top(); temp.pop();
        int next;
        while (!temp.empty()) {
            next = temp.top();
            if (last < next) isMaxHeap = false;
            if (last > next) isMinHeap = false;
            if (first) {
                first = false;
                cout << last;
            } else
                cout << " " << last;
            temp.pop();
            last = next;
        }
        cout << " " << last << endl;
    }
    
    void travel(node root) {
        path.push(root->value);
        if (root->left == NULL && root->right == NULL) {
            print(path);
            path.pop();
            return;
        }
        if (root->right) travel(root->right);
        if (root->left) travel(root->left);
        path.pop();
    }
    
    int main() {
        int num, value;
        cin >> num;
        for (int i = 0; i < num; ++i) {
            cin >> value;
            que.push(value);
        }
        buildTree();
        travel(root);
        if (isMaxHeap) cout << "Max Heap" << endl;
        else if (isMinHeap) cout << "Min Heap" << endl;
        else cout << "Not Heap" << endl;
    
        return 0;
    }
    

      

    当我声明一个全局变量queue<node> nodeQue时因为结构体是在main()函数中构建的,所以在main()函数外使用queue<node> nodeQue时,会提示没有node这种数据类型。 


    2020-07-13 18:22:21

    一种不用建树的写法:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int n;
     6 bool isIncreasing = false;
     7 bool isDecreasing = false;
     8 vector<int> a(10005);
     9 vector<int> v;
    10 void DFS(int index) {
    11     int i;
    12     if (index * 2 > n && index * 2 + 1 > n) {
    13         if (index <= n) {
    14             cout << v[0];
    15             for (i = 1; i < v.size(); ++i) {
    16                 cout << " " << v[i];
    17                 if (v[i] > v[i - 1])
    18                     isIncreasing = true;
    19                 else if (v[i] < v[i - 1])
    20                     isDecreasing = true;
    21             }
    22             cout << endl;
    23         }
    24     } else {
    25         v.push_back(a[index * 2 + 1]);
    26         DFS(index * 2 + 1);
    27         v.pop_back();
    28         v.push_back(a[index * 2]);
    29         DFS(index * 2);
    30         v.pop_back();
    31     }
    32 }
    33 
    34 int main() {
    35     cin >> n;
    36     for (int i = 1; i <= n; ++i) cin >> a[i];
    37     v.push_back(a[1]);
    38     DFS(1);
    39     if (isIncreasing && isDecreasing)
    40         cout << "Not Heap" << endl;
    41     else if (isIncreasing)
    42         cout << "Min Heap" << endl;
    43     else if (isDecreasing)
    44         cout << "Max Heap" << endl;
    45     return 0;
    46 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    JS 创建对象的几种方式
    JS跨域笔记
    HTML5随笔
    css3随笔
    CSS3最简洁的轮播图
    canvas createRadialGradient 用法
    git 初级
    Oracle数据库字符集与国家字符集
    连接Oracle 19c出现ORA-28040:没有匹配的验证协议
    Vmware workstation虚拟机导入到esxi虚拟机
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12652668.html
Copyright © 2011-2022 走看看