Ask HN: What are some cool but obscure data structures you know about?
2051 pointsI'll start: bloom filters. Lets you test if a value is definitely NOT in a list of pre-stored values (or POSSIBLY in a list - with adjustable probability that influences storage of the values.)
Good use-case: routing. Say you have a list of 1 million IPs that are black listed. A trivial algorithm would be to compare every element of the set with a given IP. The time complexity grows with the number of elements. Not so with a bloom filter! A bloom filter is one of the few data structures whose time complexity does not grow with the number of elements due to the 'keys' not needing to be stored ('search' and 'insert' is based on the number of hash functions.)
Bonus section: Golomb Coded Sets are similar to bloom filters but the storage space is much smaller. Worse performance though.
Comments (753) 253 threads
Earlier commentsI've looked around and I've seen R+ Trees are potentially a solution but there's very little info on how they're actually created in code.
Does anyone know of any nice data structures to do this? Obviously the most basic way is just indexing each field and that works for simple equality but gets harder when you're trying to do other comparators like < and >.
https://en.wikipedia.org/wiki/Nested_set_model
You can ust to store tree structures in relational databases for faster querying stuff like "all nodes below node x", "How many nodes to the top node" and so on.
Querying is easy, but comes at the cost that changes like inserting and deleting are more expensive.
I used this for an auth service with roles, user and org units. pretty query heavy, worked very well.
→ Distinct count time complexity[2]: O(1)
See Redis's PFCOUNT[4].
[1] https://en.wikipedia.org/wiki/HyperLogLog
[2] For a fixed number of registers (e.g. Redis's implementation)
[3] https://redis.io/commands/pfcount/
All comments shown.
Couldn't load more comments. Check your connection and try again.