Heaps & Priority Queues
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../assets/questions/CS-1991-Q64-1.webp)
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../assets/questions/CS-1991-Q64-2.webp)
![Question 14(b), GATE 1991: The complete binary tree has level-order array [1,5,18,7,13,25,20,11,9,15,17,27]. Its edges are 1→(5,18), 5→(7,13), 18→(25,20), 7→(11,9), 13→(15,17), and 25→(27,left only). Show the steps deleting key 5 while retaining the structure.](../../assets/questions/CS-1991-Q64-3.webp)
Answer & solution
Answer: Replace 5 with last key 27, remove the last leaf, then swap 27 with 7 and with 9. Final array [1,7,18,9,13,25,20,11,27,15,17]. (no official key was published)
Use one-based array indexing. Key 5 is at index 2 and the last node, key 27, is at index 12. Delete the last node and place its value at index 2. Taking the last leaf preserves the complete-tree shape; simply removing node 5 would leave a hole.
After replacement:
[1,27,18,7,13,25,20,11,9,15,17]
The new key is greater than both children 7 and 13. Swap with the smaller child 7 (index 4), not an arbitrary child:
[1,7,18,27,13,25,20,11,9,15,17]
At index 4, children are 11 and 9. Swap 27 with the smaller, 9 (index 9):
[1,7,18,9,13,25,20,11,27,15,17]
Index 9 is a leaf, so stop. Each swap makes the parent of the affected subtree its minimum, and only the downward path can remain out of order. The final tree has levels [1], [7,18], [9,13,25,20], [11,27,15,17]; every parent-child inequality and the complete shape are preserved.