Why would we ever use a heap over a red-black tree?
Take a scenario where we want a min-heap. The time complexities of a mean-heap are as follows.
Peek: O(1) average/worst
Delete First: O(log(n)) average/worst
Delete Arbitrary: O(n) average/worst
Search: O(n) average/worst
Now let’s reimplement this with a red-black tree that caches the left-most element.
Peek: O(1) average/worst
Delete First: O(1) average / O(log(n)) worst
Delete Arbitrary: O(log(n)) average/worst
Search: O(log(n)) average/worst
This is using the fact that rebalancing a rb-tree is amortized O(1). If we maintained an element to node address hash map, we can even make arbitrary deletions in an rb-tree O(1) average.
I get that heaps are usually more memory efficient and have better cache locality, but does it make that much of a difference? Especially if your application commonly uses say the delete first operation.
5