A data structure is required for storing a set of integers such that each of…
2009
A data structure is required for storing a set of integers such that each of the following operations can be done in O(log n) time, where n is the number of elements in the set. I. Deletion of the smallest element II. Insertion of an element if it is not already present in the set Which of the following data structures can be used for this purpose?
- A.
A heap can be used but not a balanced binary search tree
- B.
A balanced binary search tree can be used but not a heap
- C.
Both balanced binary search tree and heap can be used
- D.
Neither balanced search tree nor heap can be used
Attempted by 274 students.
Show answer & explanation
Correct answer: B
To satisfy O(log n) time complexity for all operations, we analyze the options. First, consider a Binary Heap: while insertion and deletion of the minimum element are O(log n), checking if an element exists before insertion is typically O(n) in a standard heap. Second, consider a Balanced Binary Search Tree (like AVL or Red-Black tree): searching for an element is O(log n), insertion is O(log n), and finding/deleting the minimum element is also O(log n). Since a Balanced Binary Search Tree meets all three time complexity requirements, it is the correct choice.
A video solution is available for this question — log in and enroll to watch it.