Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of the following operations on a priority queue implemented using a binary heap requires Theta of (log n) time ?

  • A.Checking the size of the queue
  • B.Accessing the minimum element
  • C.Deleting the minimum element
  • D.Creating a heap of n elements

Answer: C

The operation that takes Θ(log n) is deleting the minimum element.

In a binary min-heap the minimum is at the root. To delete it, the last element is moved to the root and then sifted down, swapping with its smaller child at each level until the heap property holds. The heap is a complete binary tree of height about log2 n, so this takes Θ(log n) time.

Checking the size reads a counter, Θ(1). Accessing the minimum reads the root, also Θ(1). Building a heap of n elements with bottom-up heapify takes Θ(n), because most nodes sit near the bottom and sift down only a little; inserting them one at a time would take O(n log n), but neither is Θ(log n).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.