Having a sorted dict (hash table, map or whatever key/value structure) you can easily have a binary search to look for an item. If we assume the keys are unique but values could be repeated, what data structure can we use to have O(log n) retrieval for keys and also O(log n) query to find count of values=something
in the given data?
Asked
Active
Viewed 1,366 times
2

Sam R.
- 16,027
- 12
- 69
- 122
1 Answers
3
Two binary search trees, one for keys, second for values, with mutual pointers will provide the required functionality. The pointers can be many-to-one from keys to values and one-to-many from values to keys.

Eugene Sh.
- 17,802
- 8
- 40
- 61
-
Was thinking the same but is it not tricky to keep two trees synced? – Sam R. Mar 30 '15 at 21:53
-
It depends what kind of manipulation you want to implement. For example, delete by value might be a little tricky, but not impossible (also you need to define, how exactly should it work, since the values are not unique). – Eugene Sh. Mar 30 '15 at 21:55