【2019暑期】【PAT甲级】1147 Heaps (30 分)

48 篇文章 0 订阅

注意点
1.大顶堆小顶堆不是左右子树比大小
大顶堆是父节点都大于子节点
小顶堆是父节点都小于子节点
2.存树的时候
如果下标从0开始的时候,左右点用i *2+1 i *2+2
如果下标从1开始的时候,左右点用i *2, i * 2+1
走所有的父节点时
从0开始 是到(size-1)/ 2
从1开始 是到size / 2
3. 联系后序输出

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cmath>
#include <map> 
#include <set>
using namespace std;

int n,m;
vector<int> tree;
void postorder(int f){
	if(f>m) return;
	postorder(f*2);
	postorder(f*2+1);
	if(f==1) cout << tree[f] << "\n";
	else cout << tree[f] << " ";
}

int main(){
	
	cin >> n >> m;
	for(int i=0; i<n; i++){
		tree.resize(m);
		for(int j=1; j<=m; j++){
			cin >> tree[j];
		}
		int min=1,max=1;
		for(int k=1; k<=m/2; k++){
			if(tree[k]<=tree[k*2]||((k*2+1)<=m&&tree[k]<=tree[k*2+1]))
				max = 0;
			if(tree[k]>=tree[k*2]||((k*2+1)<=m&&tree[k]>=tree[k*2+1]))
				min = 0;	
			
		}
		if(min==1) cout << "Min Heap" << endl;
		else if(max==1) cout << "Max Heap" << endl;
		else cout << "Not Heap" << endl;
		postorder(1);
	}
	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值