-1

Which data structure can be used 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?

  • deletion of the smallest element
  • insertion of a element if it is not already present in the set

PICK ONE OF THE CHOICES

  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 and heap can be used
  4. Neither balanced binary search tree nor heap can be used
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

2 Answers2

0

I think that the second one, "A balanced binary search tree can be used, but not a heap", because the worst case complexity of inserting and finding of a Balanced Search Tree is logN.

And we cannot use a Heap, because, for example in Binary Heap, which is the faster, the worst case of finding is N.

Popeye
  • 11,839
  • 9
  • 58
  • 91
Simone C.
  • 369
  • 4
  • 14
-1
A balanced binary search tree can be used, but not a heap

Because,

  1. Balanced binary tree has it's smallest elements in the leaves. Therefore, no overhead once the smallest element is identified. To identify, you have to check log(N) number of nodes.
  2. When inserting and element, all you do is, traverse till you find the position (you will have to traverse maximum of log(N) nodes) and add the new element as right or left child.
  3. But in heap, inserting an element make it call build heap, which is Nlog(N).

Checking whether the element exists in the tree can be done with a sligh modification in constant time in balanced binary tree.

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34