0

As we all know, the insertion and deletion all require O(log n). AVL tree require O(log n), because it need O(log n) to insert and O(log n) to rotation for balance.

RB tree require O(log n), because it need O(log n) to insert, In INTRODUCTION TO ALGORITHMS THIRD EDITION, the RB-INSERT-FIXUP need O(log n) for case 1(color flips), and at most 2 times to rotation. So it seems that AVL require 2O(log n), but RB tree require 2O(log n)+C.

Why we think RB tree is more faster than AVL in insertion? Just because rotation need more time than color flips? A rotation and color flips both require O(1), why rotation is more time-consuming than color flips? Thanks!:)

Aleeee
  • 1,913
  • 6
  • 17
  • 27

1 Answers1

2

If I understand your question correctly, yes it is true that RB-Trees and AVL-Trees both offer lookup, insertion, deletion in O(logn) time.

AVL-Trees are more rigidly balanced than RB-Trees. To attain this, a lot of rotations are required, which are time consuming. RB-Trees are slightly unbalanced, as they have weaker rules for balancing, thus they need lesser operations for insertion and deletion. As a consequence, lookup in AVL-Trees is faster than RB-Trees, but insertion and deletion is faster in RB-Trees.

EDIT

Please read this blog post. The point is that RB trees balance faster than AVL trees after insertion. Yes, a rotation does take O(1) time in AVL trees, at most two rotations need to be done, but the point of rotation still needs to be found, and the time for rotation becomes O(logn). Whereas in RB trees, rebalancing after insertion runs in amortized constant time. So, O(1) amortized time for color flips, not O(logn).

max
  • 4,248
  • 2
  • 25
  • 38
  • the insertion need O(log n). In RB-INSERT-FIXUP ,the while loop repeats only if case 1 occurs, and then the pointer z moves two levels up the tree. The total number of times the while loop can be executed is therefore O(lg n). This is from . So the whole RB tree insertion need 2O(log n)+C. The AVL also need 2O(log n). – Aleeee Oct 09 '13 at 08:43