712 views
1 1 vote

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 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

Position:
Show:

Related questions

145 145 votes
11 answers 11 answers
52.6k
52.6k views
Disha asked Sep 19, 2014
52,644 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)$$\...
81 81 votes
10 answers 10 answers
32.1k
32.1k views
Kathleen asked Sep 17, 2014
32,134 views
Consider the function $f$ defined below.struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p->next == NULL)|| ((p->data <= p -...
139 139 votes
17 answers 17 answers
49.2k
49.2k views
Kathleen asked Sep 17, 2014
49,185 views
Let S be a stack of size $n \geq1$. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform $n$ pop operations. Assume th...
95 95 votes
4 answers 4 answers
31.8k
31.8k views
Kathleen asked Sep 17, 2014
31,825 views
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...