# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6929044,"math_prob":0.9955983,"size":2898,"snap":"2020-24-2020-29","text_gpt3_token_len":921,"char_repetition_ratio":0.12646855,"word_repetition_ratio":0.17297298,"special_character_ratio":0.35852313,"punctuation_ratio":0.14376996,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99719495,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-10T09:53:58Z\",\"WARC-Record-ID\":\"<urn:uuid:6ea313e2-5663-4db9-99a5-a8284834f6a7>\",\"Content-Length\":\"9328\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7a5df899-89b8-4d42-9467-f8d4f2e3b72c>\",\"WARC-Concurrent-To\":\"<urn:uuid:6c70fd26-115d-4996-b12c-7165960d34ff>\",\"WARC-IP-Address\":\"174.137.48.86\",\"WARC-Target-URI\":\"https://programmer.group/pat-a1155-heap-paths-30-reactor.html\",\"WARC-Payload-Digest\":\"sha1:PFMS5H7KSEG4H336X45MESWHKLBHXRV7\",\"WARC-Block-Digest\":\"sha1:3KTGT5ZJFCHWT5FLIQCHAFKYMAJDVDKV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655906934.51_warc_CC-MAIN-20200710082212-20200710112212-00237.warc.gz\"}"}