GateAI
Binary Search Trees

GATE CSE 2022 · Q28

NAT1 markMedium

Time

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 3rd3^{\text{rd}} largest element of the tree is stored at index ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 509

Binary Search Trees

In a 00-indexed array representation of a complete binary tree:

  • The root is at index 00.

  • The left child of a node at index ii is at 2i+12i + 1.

  • The right child of a node at index ii is at 2i+22i + 2.

  • The parent of a node at index ii is ⌊(i−1)/2⌋\lfloor (i-1)/2 \rfloor.

In a Binary Search Tree (BST), larger elements lie in the right subtree. Following the right child links from the root gives the path to the largest elements:

  • Level 0: Index 00

  • Level 1: Index 2(0)+2=22(0) + 2 = 2

  • Level 2: Index 2(2)+2=62(2) + 2 = 6

  • Level 3: Index 2(6)+2=142(6) + 2 = 14

  • Level 4: Index 2(14)+2=302(14) + 2 = 30

  • Level 5: Index 2(30)+2=622(30) + 2 = 62

  • Level 6: Index 2(62)+2=1262(62) + 2 = 126

  • Level 7: Index 2(126)+2=2542(126) + 2 = 254

  • Level 8: Index 2(254)+2=5102(254) + 2 = 510

The right child of index 510510 would be at 2(510)+2=1022>9992(510) + 2 = 1022 > 999, so index 510510 has no right child. Thus:

  • 1st1^{\text{st}} largest element is at index 510510 (the maximum element in the BST).

  • Since index 510510 has no left child (2×510+1=1021>9992 \times 510 + 1 = 1021 > 999), the 2nd2^{\text{nd}} largest element is its parent, index 254254.

  • The 3rd3^{\text{rd}} largest element is the maximum element in the left subtree of index 254254, which is its left child: 2(254)+1=2(254) + 1 = 509509.

Therefore, the 3rd3^{\text{rd}} largest element is stored at index 509509.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Binary Search Trees previous year question is Q28 of the GATE CSE 2022 paper. Find more GATE CSE PYQs on Binary Search Trees in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Binary Search Trees PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Binary Search Trees in GATE CSE

Binary Search Trees has 21 GATE CSE PYQs across 17 papers from 2011 to 2026. 2 of them are 1-mark NAT questions like this one, and it is the only one in GATE CSE 2022.

How GATE tests Binary Search Trees

More from GATE CSE 2022

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2022 · Q28 · NAT

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 …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.