Consider the array A=<4, 1, 3, 2, 16, 9, 10, 14, 8, 7>. After building heap…

2018

Consider the array A=<4, 1, 3, 2, 16, 9, 10, 14, 8, 7>. After building heap from the array A, the depth of the heap and the right child of max-heap are _________ and _________ respectively. (Root is at level 0).

  1. A.

    3, 14

  2. B.

    3, 10

  3. C.

    4, 14

  4. D.

    4, 10

Attempted by 118 students.

Show answer & explanation

Correct answer: B

Key idea: depth = floor(log2(n)) where n is the number of elements in the array.

  • Step 1: Count the elements in the given array A. Here n = 10.

  • Step 2: Compute the depth: depth = floor(log2(10)) = 3. So the heap depth (root at level 0) is 3.

  • Step 3: Find the right child of the root in the array representation. Using 0-based indexing, the root is at index 0, the left child is at index 1, and the right child is at index 2.

  • Step 4: Build the max-heap from A. Starting from the last non-leaf node (index 4), apply heapify. After heapification, the element at index 2 (right child of root) is 10.

  • Conclusion: The heap depth is 3 and the right child of the root in the max-heap is 10.

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

Explore the full course: Coding For Placement