Let 𝑛 be an odd number greater than 100. Consider a binary minheap with n…

2026

Let 𝑛 be an odd number greater than 100. Consider a binary minheap with n elements stored in an array 𝑃 whose index starts from 1. Which of the following indices of 𝑃 do/does NOT correspond to any leaf node of the minheap?

  1. A.

    n+1/2

  2. B.

    n-1/2

  3. C.

    n-3/2

  4. D.

    n

Attempted by 14 students.

Show answer & explanation

Correct answer: B, C

In a binary heap with n elements using 1-based indexing, the parent of node i is floor(i/2). A node i has children if 2*i <= n. Therefore, nodes with indices from 1 to floor(n/2) are internal nodes (non-leaves), and nodes with indices from floor(n/2)+1 to n are leaf nodes. Since n is odd, floor(n/2) equals (n-1)/2. Thus, any index less than or equal to (n-1)/2 does not correspond to a leaf node.

Explore the full course: Gate Guidance By Sanchit Sir