What’s the main reason for choosing Red Black (RB) trees instead of AVL trees?

Technology CommunityCategory: Binary TreeWhat’s the main reason for choosing Red Black (RB) trees instead of AVL trees?
VietMX Staff asked 5 months ago

Red-black trees are more general purpose. Both red-black trees and AVL trees are the most commonly used balanced binary search trees and they support insertion, deletion and look-up in guaranteed O(log n) time. However, there are following points of comparison between the two:

  • AVL trees are more rigidly balanced and hence provide faster look-ups. Thus for a look-up intensive task use an AVL tree.
  • For an insert intensive tasks, use a Red-Black tree. RB-Trees guarantee O(1) rotations per insert operation
  • AVL trees store the balance factor at each node. This takes O(n) extra space. However, if we know that the keys that will be inserted in the tree will always be greater than zero, we can use the sign bit of the keys to store the colour information of a red-black tree. Thus, in such cases red-black tree takes no extra space.

RB Trees do relatively well on:

  • add, remove, and
  • look-up

AVL trees have:

  • faster look-ups (AVL tree is, usually, has better balance ~1.44 log(N) than RB-tree 2 log(N)) at the cost of
  • slower add/remove.