Question Details

Consider a heap containing n elements, where n > 100 and n is odd. Assuming 1-based indexing, which of the following cannot be the index of a leaf node in the heap?

Options

A

(n−1)/2

B

(n−3)/2

C

n

D

(n+1)/2

Show Answer

Correct Answer :

Option A

(n−1)/2

Solution :

The correct answer is (n−1)/2.

To understand why this is correct, let us analyze the properties of a binary heap represented as an array with 1-based indexing. For any node at index i in a heap containing n elements:
- The left child of the node is located at index 2i.
- The right child of the node is located at index 2i+1.
- A node is a leaf node if it has no children, which means its left child index exceeds the total number of elements in the heap: 2i>n.
- Conversely, a node is an internal (non-leaf) node if it has at least one child, which means:

2in

Let us now test the index given in the correct option, which is:

i=n-12

To determine if this node can be a leaf, we calculate the index of its left child:

Left Child Index=2i=2n-12=n-1

Since we are given that n>100, it is clear that:

n-1n

This means the index of the left child, n-1, is a valid position within the heap. In fact, since n is odd, the node at index n-12 also has a right child at index n. Because the node at index n-12 has children, it must be an internal node and cannot be a leaf node in the heap.

Unlock Our Free Library

Access expert-curated educational resources and study materials—completely free.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...