GATE Computer ScienceVery hardGATE CS
A max-heap is stored in A[1..13] = [100, 60, 90, 50, 40, 85, 80, 20, 10, 30, 35, 70, 75]. The key at position 8 (the value 20) is deleted by moving the last element into position 8, and then the heap property is restored by sifting up or down as needed. What is the heap afterwards (positions 1 to 12)?
- A. [100, 60, 90, 75, 40, 85, 80, 50, 10, 30, 35, 70]
- B. [100, 60, 90, 50, 40, 85, 80, 75, 10, 30, 35, 70]
- C. [100, 60, 90, 50, 40, 85, 80, 10, 30, 35, 70, 75]
- D. [100, 75, 90, 60, 40, 85, 80, 50, 10, 30, 35, 70]
Correct answer: D. [100, 75, 90, 60, 40, 85, 80, 50, 10, 30, 35, 70]
The last element 75 goes to position 8; its parent (position 4) holds 50 < 75, so it must sift up: 75 swaps with 50 and then with 60 (position 2), stopping below 100. The value 60 ends at position 4 and 50 at position 8. Sifting down only would leave the heap invalid.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Binary search (lo = 0, hi = n − 1, mid = ⌊(lo + hi)/2⌋; each loop iteration compares a[mid] once and ends the search on equality) is run…
- Evaluate the postfix expression 8 2 3 ^ / 2 3 + 5 3 - - where ^ is exponentiation (x ^ y = xʸ), / is integer division and the operators…
- How many structurally distinct binary search trees can be built from 7 distinct keys?
- An AVL tree of height 6 (a single node has height 0) must contain at least how many nodes?
- Keys 12, 7, 19, 3, 25, 9, 14 are inserted one by one into an initially empty max-heap stored in an array (index 0 is the root), each…
- An 11-slot hash table with h(k) = k mod 11 and linear probing receives keys 23, 34, 45, 12, 56, 67, 78 in that order. In which slot (0–10)…