PAT A1155 Heap Paths (30) Reactor

Keywords: less

Main idea: Give a complete binary tree to determine whether it is the largest heap or the smallest heap. The path from the root node to all the leaf nodes is output. Leaf nodes in the left subtree are required to output first than those in the right subtree.

Because it is a complete binary tree, the root node subscript is 0, the left subtree subscript is 2*i+1, and the right subtree subscript is 2*i+2. The subscript of the last non-leaf node is (N-2)/2, and N is the number of nodes. The output path of a leaf node is easy to implement, and it is recursive forward until the root node. The main problem is how to ensure that the leaf nodes of the right cottree output first. There are two situations to consider here:

Note that the leftmost node subscript is mostLeft, which is the last subscript less than N obtained by looping 2* I + 1 from root node 0. The rightmost node subscript is mostRight, which is the last subscript less than N obtained by looping 2 * i + 2 from root node 0. In two cases, is most right equal to N-1?

(1) Most Right == N-1, then all leaf nodes are on the same layer, just start from the most right, traverse to (N-2)/2+1, then output the path in turn.

(2) Most Right < N-1, then there are leaf nodes in the next layer, as in the case of node number 8 given in the title, so the path from most right to (N-2) / 2+1 needs to be output first, and then the path from N-1 to mostLeft needs to be output.

AC Code:

#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

void printPath(int node, vector<int> &tree)
{
    vector<int> path;
    path.push_back(node);
    while(node > 0)
    {
        node = (node - 1) / 2;
        path.push_back(node);
    }
    for (int i = path.size() - 1; i >= 0; --i)
    {
        printf("%d", tree[path[i]]);
        if(i > 0) printf(" ");
        else printf("\n");
    }
}

bool judgeMaxHeap(vector<int> &tree)
{
    for (int i = 0; i <= (tree.size() - 1 - 1) / 2; ++i)
    {
        if(tree[i] < tree[2 * i + 1] || tree[i] < tree[min(2 * i + 2, (int)tree.size() - 1)])
            return false;
    }
    return true;
}

bool judgeMinHeap(vector<int> &tree)
{
    for (int i = 0; i <= (tree.size() - 1 - 1) / 2; ++i)
    {
        if(tree[i] > tree[2 * i + 1] || tree[i] > tree[min(2 * i + 2, (int)tree.size() - 1)])
            return false;
    }
    return true;
}

int main()
{
    int N;
    scanf("%d", &N);
    vector<int> tree(N);
    for (int i = 0; i < N; ++i)
    {
        scanf("%d", &tree[i]);
    }
    int mostLeft = 0;
    while(mostLeft < N)
    {
        if(mostLeft * 2 + 1 >= N) break;
        mostLeft = mostLeft * 2 + 1;
    }
    int mostRight = 0;
    while(mostRight < N)
    {
        if(mostRight * 2 + 2 >= N) break;
        mostRight = mostRight * 2 + 2;
    }
    if(mostRight == N - 1)
    {
        for (int i = mostRight; i > (N - 1) / 2; --i)
            printPath(i, tree);
    }
    else
    {
        for (int i = mostRight; i > (N - 1) / 2; --i)
            printPath(i, tree);
        for (int i = N - 1; i >= mostLeft; --i)
            printPath(i, tree);
    }
    if(judgeMaxHeap(tree)) printf("Max Heap\n");
    else if(judgeMinHeap(tree)) printf("Min Heap\n");
    else printf("Not Heap\n");
    return 0;
}


 

Posted by DeeDee2010 on Tue, 08 Oct 2019 22:32:19 -0700