GATE / CS ARCHIVE

GATE CSE 2022 / QUESTION 28

GATE CSE 2022 Q28

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2022 paper

GATE 2022, Q28 1 mark · NAT
Report issue

Binary Search & Balanced Trees

Question 28, GATE 2022: A BST with 1000 distinct keys is a complete binary tree, stored in zero-based heap-array positions 0…999. At what index is its third-largest key?
Tap to enlarge
Answer & solution

Official answer: 509

The maximum key lies on the right spine, with zero-based array indices 0, 2, 6, 14, 30, 62, 126, 254, 510. Index 510 has no children because their indices exceed 999. The predecessor is its parent 254. The next predecessor is the maximum in 254’s left subtree, rooted at 509; 509 has no children either. Hence the third largest key is at index 509.

The heap-array formulas are left(i)=2i+1, right(i)=2i+2; they locate nodes, not key ordering. BST ordering implies reverse inorder visits largest first. At 254, the right child is 510 and left child 509. Both are leaves, so the first three reverse-inorder positions are 510,254,509.

Original paper, page 22 · Answer key