1147 Heaps

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:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56
 

Sample Output:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

题意:

给出一棵树的顺序遍历序列,判断这棵树是Max Heap或者是 Min Heap 还是Not Heap。

思路:

首先,构造出这棵树,然后通过递归判断这棵树,当时写递归的时候我都有些迷,不知道自己写的对不对,提交了之后都通过了(说明我写的是对的,哈哈哈),最后再后序遍历一下这棵树就行了。以为最后一个数后面不能有空格,又因为后序遍历的最后一个数是根节点,所以在构造的节点中加入isroot来判断是不是根节点。

Code:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef struct Node* node;

struct Node {
    int val;
    node left;
    node right;
    bool isroot;
    Node(int v) {
        val = v;
        left = NULL;
        right = NULL;
        isroot = false;
    }
};

node buildTree(queue<int>& val) {
    node root = new Node(val.front());
    root->isroot = true;
    val.pop();
    queue<node> que;
    que.push(root);
    while (!que.empty() && !val.empty()) {
        node father = que.front();
        que.pop();
        node l = new Node(val.front());
        val.pop();
        father->left = l;
        que.push(l);
        if (!val.empty()) {
            node r = new Node(val.front());
            val.pop();
            father->right = r;
            que.push(r);
        }
    }
    return root;
}

void postOrderTravel(node root) {
    if (root == NULL) return;
    postOrderTravel(root->left);
    postOrderTravel(root->right);
    if (root->isroot)
        cout << root->val << endl;
    else
        cout << root->val << " ";
}

bool isMaxHeap(node root) {
    bool left = true, right = true;
    if (root->left) {
        left = isMaxHeap(root->left);
        if (root->val < root->left->val) return false;
    }
    if (root->right) {
        right = isMaxHeap(root->right);
        if (root->val < root->right->val) return false;
    }
    return left && right;
}

bool isMinHeap(node root) {
    bool left = true, right = true;
    if (root->left) {
        left = isMinHeap(root->left);
        if (root->val > root->left->val) return false;
    }
    if (root->right) {
        right = isMinHeap(root->right);
        if (root->val > root->right->val) return false;
    }
    return left && right;
}

int main() {
    int m, n;
    cin >> m >> n;

    for (int i = 0; i < m; ++i) {
        int temp;
        queue<int> val;
        for (int j = 0; j < n; ++j) {
            cin >> temp;
            val.push(temp);
        }
        node root = buildTree(val);
        if (isMaxHeap(root)) {
            cout << "Max Heap" << endl;
        } else if (isMinHeap(root)) {
            cout << "Min Heap" << endl;
        } else {
            cout << "Not Heap" << endl;
        }
        postOrderTravel(root);
    }

    return 0;
}
永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/12688562.html