It seems to me that the only advantage of heap over binary tree is to find the smallest item in the heap in complexity of O(1) instead of O(log(2)n) in binary tree.
When
First of all there are different binary trees (some of them are quite difficult, some of them provide only average O(log n)
), and heap is one of them.
The second: while operations on most trees are O(log n)
, they are more complex, there is constant factor.
Heap needs constant additional memory, while trees usually need to store pointers in every node.
By the way, heap is quite easy and only use arrays (I'm not sure that if it's implemented this way in Java, but I do think so)
Heaps are usually simpler to implement than properly balanced binary trees. Additionally, they require less memory overhead (elements can be stored directly in an array, without having to allocate tree nodes and pointers and everything), potentially speedier performance (largely due to the memory locality of using a single contiguous array)...why wouldn't you use them?
Worst case complexity in case of binary tree will be O(n) when binary tree converges to an array while in heap it remains O(log(n)). you can use balanced binary trees like red black or AVl but then it wud become more complex and would require more memory.
Your first choice should depend on anticipated access patterns, and how much data you're likely to be storing:...
...but I recommend leaving the option as open as you can, so that you can benchmark at least one of the alternatives and switch to it, if it performs better.
Over the last twenty years, I've only worked on two applications where heaps were the best choice for anything (once for a LRU, and once in a nasty operations-research application, restoring additivity to randomly perturbed k-dimensional hypercubes, where most cells in the hypercube appeared in k different heaps and memory was at a premium) . However, on those two occasions, they performed vastly better than the alternatives: literally dozens of times faster than balanced trees or b-trees.
For the hypercube problem that I mentioned in the last paragraph, my team-lead thought red-black trees would perform better than heaps, but benchmarking showed that red-black trees were slower by far (as I recall, they were about twenty times slower), and although b-trees were significantly faster, heaps beat them comfortably too.
The important feature of the heap, in both the cases I mentioned above, was not the O(1) look-up of the minimum value, but rather the O(1) average update time for an element chosen at random.
-James Barbetti (Well, I thought I was. But captcha keeps telling me I'm not human)
If you use a find or search operation a lot then a balanced binary tree is preferred. Line segments intersection code use balanced trees instead of heaps because of this one reason.