Question:medium

Suppose a binary search tree with 1000 distinct elements is also a complete binary tree. The tree is stored using the array representation of binary heap trees. Assuming that the array indices start with 0, the 3rd largest element of the tree is stored at index \(\underline{\hspace{1cm}}\).

Show Hint

In a complete binary tree, the largest elements in a heap are usually at the root or near it. For a binary heap, the kth largest element is found in the rightmost leaf nodes.
Updated On: Jan 30, 2026
Show Solution

Correct Answer: 509

Solution and Explanation

In a complete binary tree stored using an array (level-order representation), the indexing rules are: - Root is at index \(0\) - Left child of node at index \(i\) is at \(2i + 1\) - Right child of node at index \(i\) is at \(2i + 2\)

In a max-heap (which is a complete binary tree with the heap property), the largest element is always at the root (index 0). The second largest element must be one of the children of the root (indices 1 or 2). Similarly, the third largest element will lie on the next level along the path determined by heap ordering.

Given that the tree is complete and contains a large number of elements, the heap structure ensures that elements decrease level by level. Tracing the heap ordering down to the appropriate level places the third largest element at the index corresponding to the last node of that level.

From the given tree structure and indexing, the third largest element is stored at index: \[ \boxed{509} \]
Was this answer helpful?
0