Java has multiset, and SmallTalk has a Bag class, and they are said to be the same function: keep a set of values but allow multiple values (with a count for each).
But seems like multiset can be implemented by a hash table that keeps counts on the keys (or maybe there are other possible implementations).
Do some implementation of multisets or bags offer better time complexity than the hash above? The required operations can be
- insert item
- delete item
- get the minimum value in the set
- get the maximum value in the set
In particular, I am hoping that for each of the 4 operations above, it can be done better than O(n)
, possibly all O(log n)
or better. (the 3 and 4 above is expected to be O(log n)
only if the multiset is maintained in some sorted way whenever a value is added or removed from it.)