Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Deleting node "0" in your tree is actually the most complicated case. Let's follow the <a href="http://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal" rel="nofollow">description from wikipedia</a> step by step:</p> <blockquote> <p>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.</p> </blockquote> <p>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:</p> <pre><code> 3(B) / \ / \ 1(B) 5(B) / \ / \ / \ / \ 0(B) 2(B) 4(B) 6(B) </code></pre> <p>So M is "0", and note that C (the NIL leaf) is not painted here. The wikipedia follows: </p> <blockquote> <p>The complex case is when both M and C are black <em>(this is our case)</em>.... 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.</p> </blockquote> <p>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:</p> <blockquote> <p><strong>Case 3:</strong> 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.</p> </blockquote> <p>So at this point, you delete "0", and repaint S = "2" red:</p> <pre><code> 3(B) / \ / \ 1(B) 5(B) \ / \ \ / \ 2(R) 4(B) 6(B) </code></pre> <p>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:</p> <pre><code> 3(B) / \ / \ 1(B) 5(R) \ / \ \ / \ 2(R) 4(B) 6(B) </code></pre> <p>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, <strong>so we are done! The tree is balanced!</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload