GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 47

Binary search along the heap insertion path

Data Structures · Heaps & Priority Queues · Full GATE CSE 2007 paper

GATE 2007, Q47 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 47, GATE 2007: Insert an element into an array-represented max heap of n elements. Use binary search along the path from the new leaf to the root to locate its insertion position. What is the asymptotic worst-case number of key comparisons?
Options:
(A) Θ(log₂ n)
(B) Θ(log₂ log₂ n)
(C) Θ(n)
(D) Θ(n log₂ n)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. The binary search is not across the entire array. It searches the sorted ancestor chain, whose length is logarithmic in n; take the logarithm of that length.

2. Derivation and interpretation. A complete binary heap has a root-to-new-leaf path containing Θ(log n) positions. Existing keys along this path are monotone because it is a max heap. Binary search among h=Θ(log n) positions uses Θ(log h)=Θ(log log n) key comparisons in the usual worst-case interpretation, so B. This is a bound on comparisons, not all insertion work: shifting keys along the path can still require Θ(log n) moves. Array indices allow an ancestor at a specified depth to be selected without doing key comparisons.

Original paper, page 11