ACompressed recap
🧾
Insert puts the key in the next free index and sifts it up (swap with the parent while larger): ≤ h comparisons. Deleting the root moves the last key to the root and sifts it down (swap with the larger child while smaller): ≤ 2h comparisons. Both O(log n); changing a key uses sift-up or sift-down accordingly.
BMerged rule card
5.2 Heap insertion (sift-up) and deleting the root (sift-down)
- Insert: place at index n; sift-up (swap with parent while larger); ≤ h comparisons.
- Delete max: root ← last; n−−; sift-down (swap with larger child while smaller); ≤ 2h comparisons.
- Both O(log n).
- Increase key → sift-up; decrease key → sift-down (max-heap).
CInterleaved GATE-style questions
Q1MCQ2 marksInsert
Insert 10, 20, 15, 30, 40 one by one into an empty max-heap. Final array:
- (A)
40 30 15 20 10 - (B)
40 20 30 10 15 - (C)
40 30 20 15 10 - (D)
40 30 15 10 20
Q2NAT1 markComparisons
Maximum comparisons to delete the root of a max-heap of height 5?
Numerical answer: ________
Q3MCQ1 markSift-down
In a min-heap, sift-down swaps the moved key with:
- (A) its larger child
- (B) its parent
- (C) the last element
- (D) its smaller child
Q4MCQ2 marksOutput
What is the last line printed?
int h[20], n = 0; /* max-heap in h[0..n-1] */
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
void insert(int x) /* sift-up */
{
int i = n++;
h[i] = x;
while (i > 0 && h[(i - 1) / 2] < h[i]) {
swap(&h[i], &h[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void siftDown(int i)
{
int l, r, big;
for (;;) {
l = 2 * i + 1;
r = l + 1;
big = i;
if (l < n && h[l] > h[big]) big = l;
if (r < n && h[r] > h[big]) big = r;
if (big == i) return;
swap(&h[i], &h[big]);
i = big;
}
}
int deleteMax(void)
{
int top = h[0];
h[0] = h[--n]; /* last element to the root */
siftDown(0);
return top;
}
void show(void) { int i; for (i = 0; i < n; i++) printf("%d ", h[i]); printf("\n"); }
int main(void)
{
int a[] = {40, 80, 35, 90, 45, 50, 70}, i;
for (i = 0; i < 7; i++) insert(a[i]);
show();
printf("removed %d\n", deleteMax());
show();
return 0;
}
- (A)
80 50 70 40 45 35 - (B)
70 45 50 40 35 - (C)
80 45 70 40 50 35 - (D)
80 70 50 40 45 35
Q5NAT2 marksDelete
Delete the maximum from the max-heap 25 15 16 14 10 8 12 13. What key is at index 2 afterwards?
Numerical answer: ________
Q6MSQ1 markCosts
Which are O(log n) in a heap of n keys?
- (A) insert
- (B) search for a key
- (C) delete the root
- (D) increase a key at a known index
Q7NAT2 marksSwaps
Into the max-heap 50 30 40 10 20 the key 45 is inserted. How many swaps occur?
Numerical answer: ________
Q8MCQ1 markWhere
A newly inserted key in a heap of n keys starts at 0-based index:
- (A) n
- (B) n − 1
- (C) 0
- (D) ⌊n/2⌋
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (D)2 marks
Trace each sift-up.
- (A) is wrong: 20 is pushed down by 40 to index 4: 10 ends at index 3.
- (B) is wrong: 30 rises above 20.
- (C) is wrong: Inserts do not sort.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q2 Answer: 101 mark
2 per level: 10.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q3 Answer: (D)1 mark
The smaller child becomes the parent of the other.
- (A) is wrong: That is a max-heap.
- (B) is wrong: That is sift-up.
- (C) is wrong: Only at the start.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q4 Answer: (A)2 marks
50 goes to the root, swaps with 80, then stops above 40 and 45.
Output
90 80 70 40 45 35 50
removed 90
80 50 70 40 45 35
- (B) is wrong: Only one key was removed.
- (C) is wrong: 45 stays at index 4; 50 stops at index 1.
- (D) is wrong: Sift-down swaps with the larger child 80, not 70.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q5 Answer: 132 marks
Result 16 15 13 14 10 8 12: index 2 holds 13.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q6 Answer: (A) (C) (D)1 mark
Search is O(n).
- (B) is wrong: No ordering helps search.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q7 Answer: 12 marks
45 goes to index 5 (parent 40) and swaps once; 45 < 50 stops it: 1.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)
Q8 Answer: (A)1 mark
The next free slot.
- (B) is wrong: That is the current last element.
- (C) is wrong: The root is occupied.
- (D) is wrong: Not the next free slot.
Re-read: Lesson 5.2 — Heap insertion (sift-up) and deleting the root (sift-down)