What is the difference between Heap and Red-Black Tree?

Technology CommunityCategory: Binary TreeWhat is the difference between Heap and Red-Black Tree?
VietMX Staff asked 3 years ago

The major difference between a binary heap and a red-black tree is the performance on certain operations:

Heap Pros

  • It makes an ideal priority queue, since the min/max element (depending on your implementation) is always O(1) access time, so no need to search for it.
  • It’s also very fast for insertion of new values – O(1) on average, O(log n) worst case.

Heap Cons

  • Slow searches O(n) for random elements.

RB Tree Pros

  • Better searching O(log n) and good insertion O(log n) performance.

RB Tree Cons

  • Slower min/max searches.
  • More overhead in general.