Given a binary-max heap. The elements are stored in an arrays as 25, 14, 16,…

2018

Given a binary-max heap. The elements are stored in an arrays as 25, 14, 16, 13, 10, 8, 12. What is the content of the array after two delete operations?

  1. A.

    14,13,8,12,10

  2. B.

    14,12,13,10,8

  3. C.

    14,13,12,8,10

  4. D.

    14,13,12,10,8

Attempted by 200 students.

Show answer & explanation

Correct answer: C

The initial max heap is represented by the array [25, 14, 16, 13, 10, 8, 12] where 25 is the root.

First deletion: Remove the root (25). Replace it with the last element (12) and heapify down. Compare 12 with its children (14, 16). Swap 12 with the larger child (16). The array becomes [16, 14, 12, 13, 10, 8].

Second deletion: Remove the new root (16). Replace it with the last element (8) and heapify down. Compare 8 with its children (14, 12). Swap 8 with the larger child (14). Then compare 8 at index 1 with its children (13, 10). Swap 8 with the larger child (13). The final array is [14, 13, 12, 8, 10].

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Isro