Consider the process of inserting an element into a , where the 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:
View worked solution
Answer: B
To find the correct position for a newly inserted element in a Max Heap of size using binary search, we analyze the path from the new leaf to the root:
- Path Length: A binary heap with elements has a height of . The path from any leaf to the root contains at most nodes, which is nodes.
- 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.
- 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 to find the insertion point.
The number of comparisons required for a binary search on a sorted sequence of length is .
Substituting :
Hence, the correct option is B.
Free practice · Your answers on this page are not saved.
