PAT A 2021年春 7-3 Structure of Max-Heap (25 分) AC代码(堆的插入,向上调整)

该博客主要讨论了计算机科学中最大堆(max-heap)的数据结构,特别是其特性——父节点的值大于等于子节点的值。文章通过插入一系列整数到初始为空的最大堆中,并对堆的结构进行向上调整。之后,文章给出一系列关于堆结构的描述,判断这些描述是否正确。代码示例展示了如何实现插入和验证过程,涉及了根节点、兄弟节点、父节点和子节点的关系判断。
摘要由CSDN通过智能技术生成

7-3 Structure of Max-Heap (25 分)

In computer science, a max-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 greater than or equal to the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree.

Your job is to first insert a given sequence of integers into an initially empty max-heap, then to judge if a given description of the resulting heap structure is correct or not. There are 5 different kinds of description statements:

  • x is the root
  • x and y are siblings
  • x is the parent of y
  • x is the left child of y
  • x is the right child of y

Input Specification:

Each input file contains one test case. For each case, the first line gives 2 positive integers: N (≤1,000), the number of keys to be inserted, and M (≤20), the number of statements to be judged. Then the next line contains N distinct integer keys in [−104,104] which are supposed to be inserted into an initially empty max-heap. Finally there are M lines of statements, each occupies a line.

Output Specification:

For each statement, print 1 if it is true, or 0 if not. All the answers must be print in one line, without any space.

Sample Input:

5 6
23 46 26 35 88
35 is the root
46 and 26 are siblings
88 is the parent of 46
35 is the left child of 26
35 is the right child of 46
-1 is the root

结尾无空行

Sample Output:

011010

结尾无空行

堆的插入,用向上调整来做。类似的输入读取在2020年春的7-4也有,我个人认为我这样还比较好写

AC代码:

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int N,M;
unordered_map<int,int> num_index;
int heap[1001];
void upadjust(int index){
	if(index==1) return;
	if(heap[index]>heap[index/2]){
		int temp=heap[index];
		heap[index]=heap[index/2];
		heap[index/2]=temp;
		num_index[heap[index]]=index;
		num_index[heap[index/2]]=index/2;
	}
	upadjust(index/2);
}
int main(){
	cin>>N>>M;
	for(int i=1;i<=N;i++){
		scanf("%d",&heap[i]);
		num_index[heap[i]]=i;
		upadjust(i);
	}
	for(int i=0;i<M;i++){
		string s;
		int x;
		cin>>x;cin>>s;
		if(s=="and"){
			int y;
			cin>>y;cin>>s;cin>>s;
			if(num_index[x]/2==num_index[y]/2){
				printf("1");
			}else{
				printf("0");
			}
		}else{
			cin>>s;cin>>s;
			if(s=="root"){
				if(heap[1]==x){
					printf("1");
				}else{
					printf("0");
				}
			}else if(s=="parent"){
				cin>>s;int y;
				cin>>y;
				if(num_index[x]==num_index[y]/2){
					printf("1");
				}else{
					printf("0");
				}
			}else if(s=="left"){
				cin>>s;cin>>s;int y;cin>>y;
				if(num_index[y]*2==num_index[x]){
					printf("1");
				}else{
					printf("0");
				}
			}else{
				cin>>s;cin>>s;int y;cin>>y;
				if(num_index[y]*2+1==num_index[x]){
					printf("1");
				}else{
					printf("0");
				}
			}
		}
	}
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值