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 ____.
GATE 2022 CS Q.28: solution and answer
Answer and solution
ANSWER
The 3rd largest element is stored at index 509.
Official answer: 509Source: Official answer key, GATE 2022 CS (IIT Kharagpur)
The array representation of a complete binary tree (root at index 0, children of index at and ) fixes only the tree's SHAPE. The node values still obey the binary-search-tree property, so ranking by value requires walking the shape with the in-order-predecessor rule, not reading heap order.
Locate the largest element: starting at the root (index 0), repeatedly move to the right child () as long as it exists. This traces .
Check whether node 510 has any children. With nodes (valid indices to ): right child (does not exist); left child (does not exist). So index 510 is a leaf and holds the 1st largest (maximum) element.
In-order predecessor rule: if a node has no left subtree, its predecessor is its nearest ancestor for which it is a right descendant — here simply its parent. Parent of index is , so the parent of 510 is . So index 254 holds the 2nd largest element.
For the 3rd largest, find the in-order predecessor of node 254. Its left child is , and , so this child exists — node 254 DOES have a left subtree.
When a node has a left subtree, its predecessor is the maximum (rightmost) element of that subtree, so it is enough to walk as far right as possible starting from node 509.
Check node 509's children: right child (does not exist); left child (does not exist). Node 509 is itself a leaf, so it is already the maximum of the subtree rooted at it.
Therefore the predecessor of 254 — and hence the 3rd largest element of the tree — is stored at index .
KEY IDEA
A complete binary tree's array shape (root at index 0, children of index at and ) determines only the SHAPE of the tree, not the BST order of values. So to find the -th largest value, walk the shape using the in-order-predecessor rule starting from the rightmost node (the maximum), completely independent of any heap-order property.
WATCH OUT FOR
Assuming this is a max-heap and reading off the "3rd largest" as if array position reflected value magnitude directly (e.g., picking index 1 or 2, the heap's near-root children). The array only encodes tree shape here; the values still obey the BST property, so ranking requires an in-order-predecessor walk, not a heap-order read.
From the GATE 2022 CS paper. Every question of this paper · Download the full paper · All 26 Trees & heaps questions, with notes
More Trees & heaps questions
- GATE 2020 CS Q.6 · MCQ · 1 markWhat is the worst case time complexity of inserting n² elements into an AVL-tree with n elements initially?
- GATE 2020 CS Q.41 · MCQ · 2 marksIn a balanced binary search tree with n elements, what is the worst case time complexity of reporting all…
- GATE 2020 CS Q.47 · NAT · 2 marksConsider the array representation of a binary min-heap containing 1023 elements.
- GATE 2018 CS Q.20 · NAT · 1 markThe postorder traversal of a binary tree is 8,9,6,7,4,5,2,3,1. The inorder traversal of the same tree is…
