Computer Sciences > GATE 2023 > Data Structures
Let A be a priority queue for maintaining a set of elements. Suppose A is implemented using a max-heap data structure. The operation EXTRACT-MAX(A) extracts and deletes the maximum element from A. The operation INSERT(A,key) inserts a new element key in A. The properties of a max-heap are preserved at the end of each of these operations.
When A contains n elements, which one of the following statements about the worst case running time of these two operations is TRUE?
A
Both EXTRACT-MAX(A) and INSERT(A,key) run in O(1)
B
Both EXTRACT-MAX(A) and INSERT(A,key) run in O(log(n)).
C
EXTRACT-MAX(A) runs in O(1) whereas INSERT(A,key) runs in O(n).
D
EXTRACT-MAX(A) runs in O(1) whereas INSERT(A,key) runs in O(log(n)).

Correct : b

Explanation:
To understand the worst-case running times of operations on a priority queue implemented via a max-heap, we look at how the tree structure is modified during insertion and deletion.
1. Worst-Case Analysis of INSERT(A, key):
• To insert a new element, it is initially placed at the next available leaf position at the bottom of the complete binary tree to maintain the structural property.
• To restore the max-heap property (where a parent must be greater than or equal to its children), the newly added element is swapped upward with its parent iteratively. This process is called heapify-up or percolate-up.
• In the worst-case scenario, the new key is larger than all existing elements and travels from the leaf all the way up to the root node.
• Since a complete binary tree with n elements has a height of ⌊log2(n)⌋, the maximum number of levels it can traverse is proportional to the height. Thus, the worst-case time complexity is O(log(n)).
2. Worst-Case Analysis of EXTRACT-MAX(A):
• The maximum element in a max-heap is always located at the root node, which can be accessed in O(1) time.
• However, removing the root leaves a hole at the top. To fix this, the last element at the deepest leaf position is moved to the root.
• To restore the max-heap property, this element is then swapped downward with its largest child iteratively until it finds its correct position. This process is called heapify-down or percolate-down.
• In the worst-case scenario, the swapped element is smaller than most elements and travels from the root down to the bottom-most leaf level.
• Just like insertion, this traversal path is bounded by the height of the tree. Therefore, the worst-case time complexity is O(log(n)).
3. Conclusion:
Both maintenance operations require a tree traversal up or down the height of the tree in the worst case, making O(log(n)) the correct runtime bound for both, matching option (b).

Similar Questions

Which one of the following sequences when stored in an array at locations A[1],...,A[10] forms a max-heap?
#950 MCQ
Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let DLLdel be...
#951 MCQ
An algorithm has to store several keys generated by an adversary in a hash table. The adversary is malicious who tries to maximize the number of collisions. Let...
#957 MCQ

Related Topics

No tags found

Unique Visitor Count

Total Unique Visitors

Loading......