Skip to content
Official PYQ · 2015

GATE CS 2015 Set 2 Q17 — Binary Heaps

Explain with AI

Consider a complete binary tree where the left and right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tree to a heap is

Choose one option
View worked solution

Answer: A

Since the left and right subtrees of the root are already max-heaps, only the root element itself can violate the max-heap property. To convert the entire complete binary tree into a max-heap, we only need to perform a single Max-Heapify operation starting at the root.

The Max-Heapify procedure compares the root with its children, swaps it with the larger child if necessary, and recursively heapifies the affected subtree. In the worst case, the root element must travel down to a leaf node.

For a complete binary tree of nn nodes, the height is h=heta(log⁡n)h = heta(\log n). The number of comparisons and swaps in the worst case is proportional to this height. Thus, the worst-case complexity is Θ(log⁡n)\Theta(\log n), which gives a tight lower bound of Ω(log⁡n)\Omega(\log n) operations.

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

Published
Report a correction
Keep exploringMore Binary Heaps questions