3

I've read some Q&As about self-balancing binary trees, but I'm not quite familiar with all of them.

The first one of them I got to know is AVL, the second is Red-Black tree.

There are something I don't quite understand: according to some books and articles, AVL can perform searching a little bit faster than Red-Black tree, well, this is understandable.

  1. Then what's Red-Black tree's edge over AVL?

  2. In AVL, probably after each insertion, we have to check for balance, but in Red-Black tree we don't have to do something like that frequently, right?

PS: I search SO for something similar, but I didn't get satisfying answer. Hope some friends can give me a detailed comparison of self-balancing trees.

Mat
  • 202,337
  • 40
  • 393
  • 406
Alcott
  • 17,905
  • 32
  • 116
  • 173

1 Answers1

2

An AVL tree has the following property: from each node, the difference in height of the left and the right subtree is at most 2.

In a red-black tree, on the other hand, the height of the left or right subtree of any node is at most twice the height of the other tree. That is, they differ at most by a factor of 2.

This shows intuitively that lookup is indeed faster in an AVL tree on average.

However, when inserting or deleting a node, we have to rebalance the AVL tree more often, to preserve the much stricter height invariant (on the other hand, rebalancing in a red-black tree is algorithmically much more complicated). This means that in practice, a red-black tree may perform much better than an AVL tree, in particular when it’s often changed.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • thanks for your answer. Did you mean, the only edge that rb-tree has over AVL is that rb-tree perform less rebalance operation, and it should be convenient if we do updates (insertion/deletion) a lot? – Alcott Aug 28 '11 at 00:19
  • @Alcott Essentially yes. In practice, rb-trees outperform AVL trees for most purposes. AVL trees perform better in situations where you build the tree *once*, and then only use it for lookup. But in such situations, you don’t need a self-balancing tree at all: since it’s static, it can be manually balanced once. – Konrad Rudolph Aug 28 '11 at 08:01
  • I know that B-tree is popular in database stuff, I also read several articles about B-tree's application in database, but I still don't quite understand how the data arranged in the disk and how to keep the B-tree structure in the disk? – Alcott Sep 02 '11 at 06:34
  • @Alcott => new question. The short answer, though: B-trees have better locality of reference. Everything else is pretty much the same as for main memory. – Konrad Rudolph Sep 02 '11 at 06:38