343 views
1 votes
1 votes

A data structure is required for storing a set of integers such that each of the following operations can be done in O(logn) time, where n is the number of elements in the set.

  1. Deletion of the smallest element

  2. Insertion of an element.

Which of the following data structures can be used for this purpose?

  1. A heap can be used but not a balanced binary search tree
  2. A balanced binary search tree can be used but not a heap

  3. Both balanced binary search tree and heap can be used

  4. Neither balanced search tree nor heap can be used

I. Deletion of an element for both balance BST and heap (min heap) will take O(logn)

II.for insertion of an element in heap should take O(logn) time by insert key operation and for balance Bst also it should be O(logn) as for balancing it will take O(logn) time .

Correct me if i am wrong 

1 Answer

2 votes
2 votes
  • In balanced BST insertion and deletion take O(logn) .

  • In min heap deletion of minimum element  Take O(logn) time . Becz minimum element present at root node. So delete a root node and coll heapify.

  • In min heap  insertion take O(logn) time.

  • In max heap delition of smallest element take O(n) time. Becz we need to first search the element . Which will take O(n)

  • In max heap insertion take O(logn) time.

Question dosnt say about min or max heap . We can consider worst case . 

If we take max heap then delition of min element take  O(n) . And insertion O(logn)

So option b is correct

Related questions

79 votes
79 votes
10 answers
1
Disha asked Sep 19, 2014
31,725 views
In a min-heap with $n$ elements with the smallest element at the root, the $7^{th}$ smallest element can be found in time$\Theta (n \log n)$$\Theta (n)$$\Theta(\log n)$$\...