7

I am studying red-black trees and I am reading the Cormen's "Introduction to Algorithms" book. Now I am trying to create red-black tree with numbers 1-10 by using the pseudo-code described in the book - RB-INSERT-FIXUP(T, z). Here is the screenshot enter image description here

Everything was fine until I inserted number "6" into the tree. According to pseudo-code I get the following result

enter image description here

As you can see all red-black tree requirements met, but I am confused because I know that red-black tree should be balanced on each step.

I can manually perform "left-rotate" procedure with "2" and "4" and change the colours. In that case I will get the following result, which is balanced appropriately

enter image description here

So my question is:

Is that all right to have unbalanced tree?, or I missed something during insertion nodes?

Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
  • I think rb tree can become unbalanced up to 2 log n in depth. you should check this. a perfectly balanced tree is roughly log n in depth. unbalanced-ness in rb tree is not bad enough to screw up it's big O for various operations. – thang Feb 15 '15 at 21:04

2 Answers2

15

This is fine. Red-black trees are balanced, but not necessarily perfectly. To be precise, properties of red-black tree guarantee that the longest path to the leaf (implicit, not shown in your picture) is at most twice as long as the shortest. Shortest one has length 2 (2 -> 1 -> leaf), longest one has length 4 (2 -> 4 -> 5 -> 6 -> leaf), so the invariant does hold.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
  • Thanks for the answer. I have just thought why don't we always balance tree if that is possible (like in the case I mentioned). Then I think it will impact on complexity. – Ashot Khachatryan Feb 15 '15 at 21:34
  • 1
    Yes, rb-tres seem to strike a nice balance between... balance and speed :) There are other balancing schemes - e.g. AVL tree guarantees more balanced tree with the same complexity, but worse constants. Doesn't seem to be used in practice much. – Marcin Łoś Feb 15 '15 at 22:03
  • @MarcinŁoś Can you take a look at my RedBlackTree remove method? http://stackoverflow.com/questions/28705454/how-to-fix-remove-in-redblacktree-implementation – committedandroider Feb 25 '15 at 19:21
  • @AshotKhachatryan, you should look at 2-3-4 trees, a form of perfectly height-balanced tree. 2-3-4 trees and red-black trees are very closely related, but it's generally easier to express the balance invariants of 2-3-4 trees and generally easier to implement operations for red-black trees. – dfeuer Mar 06 '15 at 22:08
5

They are not balanced, because they do not satisfy the balanced tree property:

A binary tree is balanced if for each node it holds that the number of inner nodes in the left subtree and the number of inner nodes in the right subtree differ by at most 1.

Some books call it "approximately balanced", because guaranteed the logarithmic time add/delete/search operations. (The balanced trees are the AVL trees.)

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
Béla
  • 51
  • 1
  • 1