3 ms·
B-trees are “optimal” in the sense that they’re O(log N) for all the usual operations (insertion/deletion, appenditem, getitem, setitem, and even concatenation
by kragen 12y ago
B-trees are “optimal” in the sense that they’re O(log N) for all the usual operations (insertion/deletion, appenditem, getitem, setitem, and even concatenation with some work) but simple exponentially-growing arrays are O(1) for getitem, setitem, and appenditem, which is asymptotically faster. Also, their constant factors are better, and their memory footprint is smaller.
hyc_symas’s comment seems a little confused because they are talking about doing binary search within B-tree pages. But if you’re using B-trees to implement dynamic non-sparse arrays, which seems to be the topic, you don’t need any binary searches. You can just subtract the beginning-of-page index from the index you’re indexing to in order to get the offset within the page.
That kind of comment, managing to be simultaneously ignorant and arrogantly dismissive, is why I don’t comment much on HN these days.
- hyc_symas 12y agoSorry, but the only ignorance here is your own. You assumed that "storing into an array" means that the array index is the only thing relevant for locating a record. But the fact that you're talking about inserts/deletes means that the array index of a particular element is mutable and therefore insufficient for uniquely locating a record. Which means you must perform some type of search to retrieve a particular item. Your O(1) getitem is nonsense. Yes, you can say "give me the 5th element of this array" but the discussion was about maps/dictionaries, where you're more often going to say "give me item foo that was inserted before". When folks like you post while obviously not paying attention, yes, you elicit a dismissive response.