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<N≤1,000), 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
题目大意:给出一个层序遍历的完全二叉树,要你打印所有到叶子节点的路径(先右后左)+判断这棵树是大顶堆还是小顶堆还是根本就不是堆。
首先dfs打印路径:可以用vector存储路径,也可以用一维数组path[]存储。
1.vector存储路径(不用维护表示层数的变量level,但是要在dfs返回的时候弹出一个元素,也就是父结点)
void dfs(int root){
if(root>n)return;
path.push_back(heap[root]);
//如果是叶子节点就打印路径
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
//先右后左
dfs(root*2+1,level+1);
dfs(root*2,level+1);
//弹出父结点
path.pop_back();
}
2.path数组存储路径
void dfs(int root,int level){
if(root>n)return;
path[level] = heap[root];
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
dfs(root*2+1,level+1);
dfs(root*2,level+1);
}
然后是堆排序时调整的函数downAdjust:其实这题可以直接用i/2表示父结点,与孩子结点i对比,就可以看出是什么堆。但是为了复习堆调整,还是把调整函数写一遍,体会一下堆是怎么调整的。
本题采用的思路是从n/2号结点开始向树的上层遍历(因为从n/2号结点开始才有孩子结点),每次调用downAdjusth函数,检查该结点是否符合大顶堆or小顶堆的要求
//检查是否是大顶堆
bool downAdjustMax(int low,int high){
//i一开始为该结点编号,j为其左孩子编号
//high为总结点数
int i = low;
int j = i*2;
while(j<=high){
//如果右孩子比较大,就把右孩子编号赋值给j
if(j+1<=high&&heap[j]<heap[j+1]){
++j;
}
//如果不符合大顶堆要求,就return
if(heap[i]<heap[j])return false;
//否则接着向下检查
else {
i = j;
j = i*2;
}
}
return true;
}
完整代码:
#include <iostream>
#include <vector>
using namespace std;
int heap[1001],n;
vector<int> path;
//打印路径
void dfs(int root,int level){
if(root>n)return;
path.push_back(heap[root]);
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
dfs(root*2+1,level+1);
dfs(root*2,level+1);
path.pop_back();
}
//检查是否是大顶堆
bool downAdjustMax(int low,int high){
int i = low;
int j = i*2;
while(j<=high){
if(j+1<=high&&heap[j]<heap[j+1]){
++j;
}
if(heap[i]<heap[j])return false;
else {
i = j;
j = i*2;
}
}
return true;
}
//检查是否是小顶堆
bool downAdjustMin(int low,int high){
int i = low;
int j = i*2;
while(j<=high){
if(j+1<=high&&heap[j]>heap[j+1]){
++j;
}
if(heap[i]>heap[j])return false;
else {
i = j;
j = i*2;
}
}
return true;
}
int main(){
cin>>n;
for(int i = 1;i<=n;i++){
cin>>heap[i];
}
dfs(1,0);
bool isMax= true,isMin = true;
//从n/2号结点到根节点遍历检查
for(int i = n/2;i>=1;i--){
if(downAdjustMax(i,n)==false)isMax = false;
}
if(isMax==false){
for(int i = n/2;i>=1;i--){
if(downAdjustMin(i,n)==false)isMin = false;
}
}
if(isMax)cout<<"Max Heap";
else if(isMin)cout<<"Min Heap";
else cout<<"Not Heap";
return 0;
}