Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
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.
  1. Start: [20, 12, 30, 25, 18, 8, 24]. Last internal node is index ⌊7/2⌋ − 1 = 2.
  2. Heapify(2): 30 vs children 8, 24 → no change.
  3. Heapify(1): 12 vs children 25, 18 → swap with 25 → [20, 25, 30, 12, 18, 8, 24].
  4. 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].
  5. 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.
Heap-sort engine checkpoints (first two rows answer this question)checked by code
Input20 12 30 25 18 8 24
Max-heap built30 25 24 12 18 8 20
After extraction 125 20 24 12 18 8 30
After extraction 224 20 8 12 18 25 30
After extraction 320 18 8 12 24 25 30
After extraction 418 12 8 20 24 25 30
After extraction 512 8 18 20 24 25 30
After extraction 68 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  8

Array 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