Skip to content
Official PYQ · 2022

GATE CS 2022 Q18 — Binary Search Trees

Explain with AI
Suppose a binary search tree with 10001000 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,0, the 3rd3^{\text{rd}} largest element of the tree is stored at index ______________ .
View worked solution

Answer: 509

In the array representation of a complete binary tree with indices starting at 00:

  • The root is at index 00.
  • For any node at index ii, its left child is at 2i+12i + 1 and its right child is at 2i+22i + 2.

Since the tree is a Binary Search Tree (BST), the largest elements are located along the rightmost path. Let's trace the rightmost path starting from the root:

Root=0→2→6→14→30→62→126→254→510\text{Root} = 0 \to 2 \to 6 \to 14 \to 30 \to 62 \to 126 \to 254 \to 510

The next right child would be at index 2(510)+2=10222(510) + 2 = 1022. Since the tree has 10001000 elements (indices 00 to 999999), index 510510 has no right child and is a leaf node.

Now, let's examine the subtree rooted at index 126126 to determine the largest elements in the tree:

  • The right child of 126126 is 254254.
  • The children of 254254 are 509509 (left child) and 510510 (right child).

By the properties of a BST, for the subtree rooted at 254254:

value(509)<value(254)<value(510)\text{value}(509) < \text{value}(254) < \text{value}(510)

Since 254254 is the right child of 126126, all elements in the subtree of 254254 are greater than the element at 126126:

value(126)<value(509)<value(254)<value(510)\text{value}(126) < \text{value}(509) < \text{value}(254) < \text{value}(510)

Thus, the largest elements in descending order are:

  1. 1st largest: index 510510
  2. 2nd largest: index 254254
  3. 3rd largest: index 509509

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Search Trees questions