Skip to content
Official PYQ · 2007

GATE CS 2007 Q47 — Binary Heaps

Explain with AI

Consider the process of inserting an element into a Max Heap\text{Max Heap}, where the Max Heap\text{Max Heap} is represented by an array. Suppose we perform a binary search on the path from the new leaf to the root to find the position for the newly inserted element, the number of comparisons performed is:

Choose one option
View worked solution

Answer: B

To find the correct position for a newly inserted element in a Max Heap of size nn using binary search, we analyze the path from the new leaf to the root:

  1. Path Length: A binary heap with nn elements has a height of h=⌊log⁡2n⌋h = \lfloor \log_2 n \rfloor. The path from any leaf to the root contains at most h+1h + 1 nodes, which is Θ(log⁡2n)\Theta(\log_2 n) nodes.
  2. Sorted Order: Due to the Max Heap property, the value of a parent node is always greater than or equal to its children. Thus, the elements along the path from the leaf to the root are sorted in non-decreasing order.
  3. Binary Search: Since the path is sorted and its elements can be indexed directly (by repeatedly halving the array index), we can perform a binary search on this path of length L=Θ(log⁡2n)L = \Theta(\log_2 n) to find the insertion point.

The number of comparisons required for a binary search on a sorted sequence of length LL is Θ(log⁡2L)\Theta(\log_2 L).

Substituting L=Θ(log⁡2n)L = \Theta(\log_2 n):

Number of comparisons=Θ(log⁡2(log⁡2n))\text{Number of comparisons} = \Theta(\log_2 (\log_2 n))

Hence, the correct option is B.

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

Published
Report a correction
Keep exploringMore Binary Heaps questions