I have googled and couldnt find any DS to store and read bi directional data in O(1) time. E.g. books and authors. With the name of book, authors has to be found. With the name of author, books has to be found.

How in DB, these relations like Join tables are stored?

thanks in advance.

有帮助吗?

解决方案

The idea is a combination of the following:

  • A hash map of the first element to the second element (or a list of them)
  • A hash map of the second element to the first element (or a list of them)

Hash maps give expected O(1) lookup.

I don't believe databases typically use hash maps though, more typically b-trees as far as I know (giving O(log n) performance).

其他提示

Expanding on Dukeling's answer, I believe Google has an implementation called HashBiMap: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/HashBiMap.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top