18年春季第四题 PAT甲级 1147 Heaps (30分) 可以不建树

汇总贴

2020年3月PAT甲级满分必备刷题技巧

题目

来源:https://pintia.cn/problem-sets/994805342720868352/problems/994805342821531648

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))

Your job is to tell if a given complete binary tree is a heap.

Input Specification:

<
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Intermediate heaps during heap sort algorithm: Step 1: Remove the root node (40) and replace it with the last node (20). The heap becomes [20,30,15,10,40]. ``` 40 / \ 30 15 / \ 10 20 20 / \ 30 15 / 10 \ 40 ``` Step 2: Heapify the heap to maintain the max heap property. The heap becomes [30,20,15,10,40]. ``` 20 / \ 30 15 / 10 \ 40 30 / \ 20 15 / 10 \ 40 ``` Step 3: Remove the root node (30) and replace it with the last node (10). The heap becomes [10,20,15,30,40]. ``` 30 / \ 20 15 / 10 \ 40 10 / \ 20 15 / 30 \ 40 ``` Step 4: Heapify the heap to maintain the max heap property. The heap becomes [20,10,15,30,40]. ``` 10 / \ 20 15 / 30 \ 40 20 / \ 10 15 / 30 \ 40 ``` Step 5: Remove the root node (20) and replace it with the last node (15). The heap becomes [15,10,20,30,40]. ``` 20 / \ 10 15 / 30 \ 40 15 / \ 10 20 / 30 \ 40 ``` Step 6: Heapify the heap to maintain the max heap property. The heap becomes [10,15,20,30,40]. ``` 15 / \ 10 20 / 30 \ 40 10 / \ 15 20 / 30 \ 40 ``` Step 7: Remove the root node (10) and replace it with the last node (40). The heap becomes [40,15,20,30]. ``` 10 / \ 15 20 / 30 \ 40 40 / \ 15 20 / 30 ``` Step 8: Heapify the heap to maintain the max heap property. The heap becomes [20,15,40,30]. ``` 15 / \ 40 20 / 30 20 / \ 15 40 / 30 ``` Step 9: Remove the root node (20) and replace it with the last node (30). The heap becomes [40,15,30]. ``` 20 / \ 15 40 / 30 30 / \ 15 40 ``` Step 10: Heapify the heap to maintain the max heap property. The heap becomes [40,15,30]. ``` 30 / \ 15 40 40 / \ 15 30 ``` Step 11: Remove the root node (40) and replace it with the last node (30). The heap becomes [30,15]. ``` 40 / \ 15 30 30 / 15 ``` Step 12: Heapify the heap to maintain the max heap property. The heap becomes [30,15]. ``` 15 / 30 ``` Step 13: Remove the root node (30) and replace it with the last node (15). The heap becomes [15]. ``` 30 / 15 15 ``` Step 14: Heapify the heap to maintain the max heap property. The heap becomes [15]. ``` 15 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值