B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part B · Q16b3 marksCO4Module 3
Question 16b
Create a Max-heap from 20, 12, 30, 25, 18, 8 and 24 using heapify. Then delete root element from the tree.
Transcription notes:
- Printed under Module 4; binary heap operations are a Module 3 topic, and heapify is shared with heap sort (Module 4).
Topics: Binary heaps and priority queues, Heap sort
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- “Using heapify” is read as the bottom-up build-heap procedure (heapify from the last internal node up to the root). Inserting the keys one by one gives the same heap for this input.
- Start: [20, 12, 30, 25, 18, 8, 24]. Last internal node is index ⌊7/2⌋ − 1 = 2.
- Heapify(2): 30 vs children 8, 24 → no change.
- Heapify(1): 12 vs children 25, 18 → swap with 25 → [20, 25, 30, 12, 18, 8, 24].
- Heapify(0): 20 vs children 25, 30 → swap with 30 → [30, 25, 20, 12, 18, 8, 24]; continue at index 2: 20 vs 8, 24 → swap with 24 → [30, 25, 24, 12, 18, 8, 20].
- Delete root: replace 30 by the last element 20 → [20, 25, 24, 12, 18, 8]; sift down: 20 vs 25, 24 → swap with 25 → [25, 20, 24, 12, 18, 8]; 20 vs children 12, 18 → stop.
| Input | 20 12 30 25 18 8 24 |
|---|---|
| Max-heap built | 30 25 24 12 18 8 20 |
| After extraction 1 | 25 20 24 12 18 8 30 |
| After extraction 2 | 24 20 8 12 18 25 30 |
| After extraction 3 | 20 18 8 12 24 25 30 |
| After extraction 4 | 18 12 8 20 24 25 30 |
| After extraction 5 | 12 8 18 20 24 25 30 |
| After extraction 6 | 8 12 18 20 24 25 30 |
Totals: comparisons = 20, swaps = 14
How to write it in the exam
text
Max-heap: 30
/ \
25 24
/ \ / \
12 18 8 20
After deleting 30: 25
/ \
20 24
/ \ /
12 18 8Array form: heap = [30, 25, 24, 12, 18, 8, 20]; after deletion = [25, 20, 24, 12, 18, 8].
How this answer was verified
- Hand trace
- tests/pyq-sorting.test.ts
- heap_sort.c differential test