Question:medium

Let \(A\) be a priority queue implemented using a max-heap. 
Extract-Max(A) deletes and returns the maximum element; 
Insert(A, key) inserts a new element. The max-heap property is preserved after each operation. 
When \(A\) has \(n\) elements, which statement about the worst-case running times is TRUE?

Show Hint

In heaps, the cost is proportional to the tree height. For binary heaps, height \(\sim \log_2 n\) \(\Rightarrow\) both sift-up and sift-down are \(O(\log n)\). To get \(O(1)\) amortized inserts, you’d need a different structure (e.g., Fibonacci heap), not a binary heap.
Updated On: Feb 3, 2026
  • Both Extract-Max(A) and Insert(A,key) run in \(O(1)\).
  • Both Extract-Max(A) and Insert (A,key) run in \(O(\log n)\).
  • Extract-Max(A) runs in \(O(1)\) whereas Insert(A,key) runs in \(O(n)\).
  • Extract-Max}(A) runs in \(O(1)\) whereas Insert(A,key) runs in \(O(\log n)\).
Show Solution

The Correct Option is B

Solution and Explanation

The problem involves understanding the worst-case running times of two operations, Extract-Max(A) and Insert(A, key), on a max-heap data structure, which is commonly used to implement priority queues.

  1. Max-Heap Properties:
    • A max-heap is a complete binary tree where the value of each node is greater than or equal to the values of its children.
    • The height of a max-heap with \( n \) elements is \( O(\log n) \), which is crucial for determining the time complexity of operations.
  2. Time Complexity Analysis:
    • Extract-Max(A):
      • This operation removes and returns the maximum element from the heap, which is located at the root.
      • To maintain the max-heap property after removal, the last element is moved to the root, and then the heap property is restored by a process called 'heapify'.
      • The heapifying process, either up or down the tree, takes \( O(\log n) \) time due to the tree's height.
    • Insert(A, key):
      • When inserting a new key, it is initially added at the end of the heap.
      • To restore the max-heap property, the new key may need to be moved upwards (heapified up) in the tree.
      • This upward traversal also takes \( O(\log n) \) time in the worst case.
  3. Conclusion:
    • Both operations, Extract-Max(A) and Insert(A, key), have a worst-case running time of \( O(\log n) \) due to the need to traverse the height of the heap.
    • Hence, the correct statement is: Both Extract-Max(A) and Insert(A, key) run in \( O(\log n) \).
Was this answer helpful?
0