by park0ur · 11y ago · 0 comments
Much more importantly, vectors are almost always the wrong answer (with a handful of exceptions). Consider a perfectly balanced tree with even 1k elements -- it will have height log2(1k) ~ 10, so approximately 11 reads, almost certainly from main memory, to access an element.
Exceptions are things like you have to iterate through elements in some order, a very high ratio of inserts/deletes to queries, etc -- the obvious stuff.
This is the worst argument ever. You basically said, "trees are the wrong answer, except for when you want to do tree stuff." Want to have a priority queue? Or want to search through your collection without sorting it first? Or insert into your sorted collection? Trees are your friend. They're a tool, just like hashmaps.And no, that's not what I wrote. Trees are an associative data structure. Hashmaps are a better associative data structure, except when you need the few capabilities trees have that hashmaps don't.
ps -- if you run that quote through fold -s -w 77, it won't be so wide...
My apologies. But, I maintain that trees have their uses. I would never use a hashmap to implement a priority queue, and i'd never use a tree to implement associative arrays!