Data Structures | Heap | Question 6
What is the content of the array after two delete operations on the correct answer to the previous question?
(A) 14,13,12,10,8
(B) 14,12,13,8,10
(C) 14,13,8,12,10
(D) 14,13,12,8,10
Answer: (D)
Explanation: For Heap trees, deletion of a node includes following two operations.
1) Replace the root with last element on the last level.
2) Starting from root, heapify the complete tree from top to bottom..
Let us delete the two nodes one by one:
1) Deletion of 25:
Replace 25 with 12
12 / \ / \ 14 16 / \ / / \ / 13 10 8
Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.
16 / \ / \ 14 12 / \ / / \ / 13 10 8
2) Deletion of 16:
Replace 16 with 8
8 / \ / \ 14 12 / \ / \ 13 10
Heapify from root to bottom.
14 / \ / \ 8 12 / \ / \ 13 10 14 / \ / \ 13 12 / \ / \ 8 10
Please Login to comment...