Here's a description of the data structure:
It operates like a regular map with get
, put
, and remove
methods, but has a sort
method that can be called to sorts the map. However, the map remembers its sorted structure, so subsequent calls to sort can be much quicker (if the structure doesn't change too much between calls to sort
).
For example:
- I call the
put
method 1,000,000 times. - I call the
sort
method. - I call the
put
method 100 more times. - I call the
sort
method.
The second time I call the sort
method should be a much quicker operation, as the map's structure hasn't changed much. Note that the map doesn't have to maintain sorted order between calls to sort
.
I understand that it might not be possible, but I'm hoping for O(1) get
, put
, and remove
operations. Something like TreeMap provides guaranteed O(log(n)) time cost for these operations, but always maintains a sorted order (no sort
method).
So what's the design of this data structure?
Edit 1 - returning the top-K entries
Alhough I'd enjoy hearing the answer to the general case above, my use case has gotten more specific: I don't need the whole thing sorted; just the top K elements.
Data structure for efficiently returning the top-K entries of a hash table (map, dictionary)
Thanks!