What data structure to use to have O(log n) key AND value lookup?

风格不统一 提交于 2019-12-11 12:35:24

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29356948/what-data-structure-to-use-to-have-olog-n-key-and-value-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!