Pregunta

I am reading the Wikipedia explanation of red black tree removal process.

There is one simple thing which I am not able to understand.

Example: I have a totally black RBTree

                     3(B)
                 /          \
                /            \
               1(B)          5(B)
             /     \        /    \
            /       \      /      \
           0(B)     2(B) 4(B)     6(B)

Wikipedia states that if you have a node to delete with 2 leaf children and the sibling is also a node with 2 Leaf children, then we can simply remove the node and recolor the parent and sibling.

Suppose in the tree above I want to delete 0. Then no amount of recoloring 1 or 2 helps because no matter what you do, the two subrees (the 1 side) and the 5 side end up with different black heights.

What am I missing?

I found that Wikipedia had a very good explanation of insert, but the explanation of delete is confusing.

¿Fue útil?

Solución

Deleting node "0" in your tree is actually the most complicated case. Let's follow the description from wikipedia step by step:

We use the label M to denote the node to be deleted; C will denote a selected child of M, which we will also call "its child". If M does have a non-leaf child, call that its child, C; otherwise, choose either leaf as its child, C.

So, in this case, M is your node "0", which will be deleted, and C is any of its NIL children (leaves, which are always NIL). Just to remind, your original tree - following your beautiful ascii-art is:

                     3(B)
                 /          \
                /            \
               1(B)          5(B)
             /     \        /    \
            /       \      /      \
           0(B)     2(B) 4(B)     6(B)

So M is "0", and note that C (the NIL leaf) is not painted here. The wikipedia follows:

The complex case is when both M and C are black (this is our case).... In the diagrams below, we will also use P for N's new parent (M's old parent), SL for S's left child, and SR for S's right child.

So, P is "1", and S is "2", all nodes are black. Then you follow the case descriptions. You skip case 1, as "0" is not root. You skip case 2, as "2" is not red. Case 3 is matching:

Case 3: P, S, and S's children are black. In this case, we simply repaint S red. The result is that all paths passing through S, which are precisely those paths not passing through N, have one less black node. Because deleting N's original parent made all paths passing through N have one less black node, this evens things up. However, all paths through P now have one fewer black node than paths that do not pass through P, so property 5 (all paths from any given node to its leaf nodes contain the same number of black nodes) is still violated. To correct this, we perform the rebalancing procedure on P, starting at case 1.

So at this point, you delete "0", and repaint S = "2" red:

                     3(B)
                 /          \
                /            \
               1(B)          5(B)
                   \        /    \
                    \      /      \
                    2(R) 4(B)     6(B)

Then, following the description, you go to Case 1, but this time with its parent node P (= "1") substitued as N. But N = "1" is not root, so you jump to Case 2. S = "5" is not red, so we jump to Case 3 again. And here, we re-paint S = "5" red:

                     3(B)
                 /          \
                /            \
               1(B)          5(R)
                   \        /    \
                    \      /      \
                    2(R) 4(B)     6(B)

Then we have to jump to Case 1 again, with P = "3" substitued as N this time. N = "3" and we see that this is a root, so we are done! The tree is balanced!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top