12

Heap supports insert operation in $O(\log n)$ time. And while heap supports remove min/max in $O(\log n)$ time, to remove any element (non min/max) heap takes $O(n)$ time.

However, red-black tree supports insert/remove both in $O(\log n)$ time. We can just remove the first/last element in a red-black tree to remove min/max in $O(\log n)$ time.

I do understand that heaps are used specifically to remove the min/max, but it seems like red-black trees can do what heaps can do but just faster/equal.

Is there any distinctive advantage to using heaps over red-black trees?

xskxzr
  • 7,455
  • 5
  • 23
  • 46
Wonjoo Lee
  • 123
  • 1
  • 5
  • 1
  • In some cases, a heap can maintain an index to remove any element in O(log n) time, not just min/max.
  • – Gassa Mar 22 '19 at 00:59
  • 1
  • In practice, the constant matters. The same reason most languages' standard libraries use QuickSort as the base sorting function, instead of HeapSort: the former is just faster.
  • – Gassa Mar 22 '19 at 01:00
  • 2
    https://stackoverflow.com/q/17708519/5376789 – xskxzr Mar 22 '19 at 03:52