0

It seems to me that Binary Search Tree can do everything a Binary heap can do, plus additional things.

|                     |   Heap   | Bal. BST |
---------------------------------------------
| Lookup min element  |  O(1)    |  O(1)    |
---------------------------------------------
| Add an element      |  O(logn) |  O(logn) |
---------------------------------------------    
| Find and Remove     |  O(n)    |  O(logn) |
| an element          |          |          |
---------------------------------------------

As a consequence of find and remove properties, it is possible to mutate an element, and we can pretty much ensure the order is still maintained after mutation in O(logn) time

The advantages I see with Binary Heap are

i) Simpler to implement ii) Memory allocated is contiguous (and thus faster access)

(i) is non-issue as I'll be rarely implementing any of them from scratch. If we are mutating the elements a lot, then (ii) is not a significant advantage.

It seems to me that balanced binary tree can do everything that a binary heap can do, then why is it not used ubiquitously? (Like Doubly Linked Lists are used ubiquitously over Singly LinkedLists)

Anoop
  • 5,540
  • 7
  • 35
  • 52

3 Answers3

3

One correction, finding the minimum is O(log(n)) for a BST. Other areas where the heap has better numbers than the BST. Adding an element to a heap has expected value O(1) in practice (the average is 2 comparisons) which is better than a BST. Turning a list into a heap has time O(n) which is again better than O(n log(n)) for a BST.

And finally you are wrong to discount both performance and contiguous memory as requirements. Heaps are typically used as a way to implement priority queues, which are often used in performance critical pieces of code like schedulers. For an extreme example, consider the scheduler in your OS kernel. Furthermore once you're inside the kernel, abstractions that you're used to like swapping memory in and out become explicit. As a result it is often required within kernel code to use data structures that use physically contiguous memory. Which is a significant win for heaps.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • 1
    +1. And although removing the minimum element from either a heap or a red-black tree is O(log *n*), the rebalancing operations in the latter are much more complex, so the constant factor is significantly higher even aside from memory locality considerations. – ruakh Aug 28 '18 at 00:04
  • I believe you can implement balanced BSTs so that looking up the minimum is O(1). Just keep a pointer to the leftmost node. – Cody Johnson Jun 05 '19 at 00:58
3

For binary heaps:

  • Simpler implementation is a significant advantage because it corresponds to better performance -- just less code to execute
  • Contiguous memory is a fantastic advantage due to reduced memory allocation overhead and reference locality
  • The greatly reduced memory consumption is another great advantage that you failed to mention.

Also, singly linked lists are used more often than doubly linked lists in real work. Often they are not strictly lists, though, because we take advantage of the fact that singly-linked lists can share tails.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
2

The balanced BST has space overhead of at least two pointers per item (more if balance information is stored separately). This is significant when the items are small.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120