<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
<channel>
<title>patrick dewane gathers wool</title>
<link>http://www.patrickdewane.com/</link>
<description />
<language>en-US</language>
<lastBuildDate>Mon, 09 May 2011 15:43:00 -0500</lastBuildDate>
<generator>http://www.typepad.com/</generator>

<docs>http://www.rssboard.org/rss-specification</docs>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/patrickdewanegatherswool" /><feedburner:info uri="patrickdewanegatherswool" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
<title>Red-Black trees, oh my (part 4)</title>
<link>http://feedproxy.google.com/~r/patrickdewanegatherswool/~3/pSFST-tBtHg/red-black-trees-oh-my-part-4.html</link>
<guid isPermaLink="false">http://www.patrickdewane.com/2011/05/red-black-trees-oh-my-part-4.html</guid>
<description>What goes up… Do you know what’s even better than building a red-black tree? Destroying one. But not in that awesome wrecking ball kind of way. We will be a tad more graceful and careful. In researching this topic, I...</description>
<content:encoded><![CDATA[<h2>What goes up… </h2>  <p>Do you know what’s even better than <a href="http://www.patrickdewane.com/2011/02/red-black-trees-oh-my-part-3.html" target="_blank">building</a> a red-black tree? Destroying one. But not in that awesome wrecking ball kind of way. We will be a tad more graceful and careful.</p>  <p>In researching this topic, I found many comments regarding the high level of difficulty involved with deleting nodes from a red-black tree. Further, I found dozens of red-black tree examples that eschewed deletion altogether! Is this fear justified?</p>  <p>Deleting nodes from a red-black tree certainly has its complications, but from these complications we will observe, prove, and generalize our way through the barriers of fear<sup>1</sup>.</p>  <p>On the surface, it may seem that breaking down a red-black tree should be easier than building it up. Is the same not true of other man-made structures such as bridges and buildings?</p>  <p>Not quite. Rather than tearing down a skyscraper with a wrecking ball, we will attempt to remove all load bearing walls from the 7<sup>th</sup> floor while maintaining structural integrity of the building.</p>  <p>That was a much better analogy.</p>  <h2>Source of complication</h2>  <p>What about deletion makes it more complicated than insertion? This question is based on the fact that deletion <em>is</em> more complicated than insertion. Looks like we have our first observation to make.</p>  <p>Observe what happens to a red-black tree when a node is inserted into it:</p>  <ol>   <li>Insertion happens at the leaf level. </li>    <li>New nodes are colored red to prevent a black violation (we previously reasoned that red violations were easier to fix than black violations). </li>    <li>New nodes are colored red since there is then a chance a violation may not occur at all. </li>    <li>If a red violation occurs, we remedy it by re-coloring and/or rotating nodes in the tree. </li> </ol>  <p>Now we can compare these observations with what can be expected of a red-black tree when a node is removed from it.</p>  <ol>   <li>Deletion could happen anywhere in the tree. We may have to delete the root node, an interior node, or a leaf node. </li>    <li>The node to delete could be red or black in color. We may need to know how to fix both a red and a black violation. </li> </ol>  <p>When inserting nodes, we are in control of where insertion happens and the circumstances under which it happens. The opposite is the case when we delete a node.</p>  <p>If deleting a node could be made similar to inserting a node, we may be able to leverage at least some of our existing knowledge. The two aspects of insertion that make it easier to deal with are color and locality.</p>  <p>Choosing the color of a node allows us to pick which violation we want to fix. We choose to insert red nodes instead of a black ones to avoid having to rebalance the black-height of the tree. We will not have this luxury when deleting a node since each node’s color is pre-determined and required to maintain the integrity of the tree. </p>  <p>There is a way to stack the deck in our favor by forcing a red node down the path of the deletion, but I will not be showing how to do such. </p>  <p>Locality may not seem to offer much benefit, but it provides us with a consistent base case for dealing with deletion. If we could always delete a leaf or near-leaf node (a node with at most one non-null child) then each deletion will work under similar conditions. This will allow us to generalize our deletion algorithm by focusing on the small picture and building on it.</p>  <h2>Heir to the throne</h2>  <p>If you are familiar with deleting nodes from a regular binary search tree, then you probably know what comes next.</p>  <p>When deleting an internal node from a binary search tree, the trick is to locate that node’s in-order predecessor (<strong>IOP</strong>) or in-order successor (<strong>IOS</strong>) and use its value as a replacement for the node to delete. Then simply remove the IOP or IOS from the tree. </p>  <p>We maintain the binary search tree characteristics of the tree by using the IOP as a replacement as it ensures that the left sub-tree has smaller values as the IOP is the largest valued node in the left sub-tree. The same is true of the IOS, but flip-flopped.</p>  <h3>Definitions</h3>  <ol>   <li>In-order predecessor (IOP)      <br />The IOP of a node <strong>X</strong> is the largest valued non-null node <strong>Y </strong>in the left sub-tree of <strong>X</strong>. </li>    <li>In-order successor (IOS)      <br />The IOS of a node <strong>X</strong> is the smallest valued non-null node <strong>Y</strong> in the right sub-tree of <strong>X</strong>. </li> </ol>  <p>Many tree deletion algorithms, even ones I’ve written, alternate between the IOP and IOS when deleting nodes. The idea here is to help keep the tree balanced by not always deleting nodes on one particular side. We are not going to be doing this. Rather, we will focus on the IOS to keep the algorithm simpler. Remember that I want this to be a learning resource and not necessarily the most efficient implementation. Writing code to alternate between the IOP and IOS is quite trivial though if you want to do that on your own.</p>  <p>The code for locating the IOS of a node is very straightforward given the definition:</p>  <blockquote>   <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 400px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> Node InOrderSuccessor(Node current)<br />{<br />    <span style="color: #0000ff">if</span> (current == <span style="color: #0000ff">null</span>)<br />    {<br />        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;<br />    }<br />    Func&lt;Node, Node&gt; ios = <span style="color: #0000ff">null</span>;<br />    ios = c =&gt;<br />    {<br />        <span style="color: #0000ff">if</span> (c[Node.Direction.Left] == <span style="color: #0000ff">null</span>)<br />        {<br />            <span style="color: #0000ff">return</span> c;<br />        }<br />        <span style="color: #0000ff">return</span> ios(c[Node.Direction.Left]); <span style="color: #008000">// tail recursive, bam.</span><br />    };<br />    <span style="color: #0000ff">return</span> ios(current[Node.Direction.Right]);<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<p>Given a node, the above function will find its IOS by examining its right sub-tree and then recursing left as far as possible.</p>

<p>An interesting property of the IOS is that if it has a child, it will only have a right child. Perhaps this is not interesting and rather obvious. But it is useful information to leverage when we work on fixing the loss of a black node.</p>

<p>In order for us to take advantage of those observations, let’s prove them first.</p>

<h3>Lemmata</h3>

<ol>
  <li>The IOS of any internal or near-leaf node is a leaf node or a near-leaf node with a null left child. </li>

<p>  <li>A near-leaf node can have an IOS or IOP, but not both. </li><br />
</ol></p>

<h3>Proofs</h3>

<ol>
  <li>This requires two parts. First we’ll prove that an internal or near-leaf node can have an IOS and then we’ll prove that an IOS has a null left child.&#160; <br /><strong>A. An internal or near-leaf node can have an IOS.</strong> 

<p>    <br /><em>If <strong>X</strong> is an internal node then by definition it must have two non-null children. If <strong>X</strong> is a near-leaf node then by definition it must have at most one non-null child. Recall that the IOS of <strong>X</strong> is the smallest valued non-null node <strong>Y</strong> in the right sub-tree of <strong>X</strong>. Therefore <strong>X</strong> can have an IOS if it has a non-null right child.</em> </p>

<p>    <br /><strong>B. An IOS has a null left child.</strong> </p>

<p>    <br /><em>By definition, the IOS of a node <strong>X</strong> has the property that it is the smallest valued non-null node in <strong>X</strong>’s right sub-tree. Recall that every node in a binary search tree has the property that the value of its left child (if not null) is smaller than its own value. This means that the smallest valued non-null node in any sub-tree is the left most node from the root of the sub-tree. Therefore the IOS of a node <strong>X</strong> is the left most non-null node in the right sub-tree of <strong>X</strong>.</em> </li></p>

<p>  <li>By definition, a near-leaf node has at most one non-null child. For a node to have both an IOS and an IOP, it must have two non-null children. It then follows that a near-leaf node cannot have both an IOS and an IOP. </li><br />
</ol></p>

<p>A few simple proofs can certainly go a long way. My deletion algorithm is based on these proofs holding, so its important to make sure our assumptions and observations can be proven.</p>

<h2>Thoughts on deletion</h2>

<p>What can we expect of the tree when a node is deleted from it? What types of violations might arise and when? How does the state of the tree give rise to how the tree might be fixed? Why is the sky blue? These are just a few questions that we will need answered before we can determine how to remedy any violations introduced by deleting a node.</p>

<p>We know that deleting a node really starts after we’ve located a leaf or near-leaf node, which may be the IOS or the original node to delete.</p>

<p>Our algorithm for deleting a node will be recursive and we will fix any violations that arise as we unwind our call stack. This should sound familiar as it is the approach we took with inserting nodes. This worked out well for insertion since we were able to look down the tree for red violations as we popped back up it. We could have returned some type of indication that a red violation existed instead of looking for it each time. Identifying a red violation however was trivial and this overhead was not worrisome.</p>

<p>Things are going to have to be a bit different with deletion however since we now have to worry about black height violations when we remove a black node.</p>

<p>There are a few options on the table to address this problem.</p>

<ol>
  <li>After a recursive call, check the black height of the left and right sub-trees of the current root and see if they are different. </li>

<p>  <li>Prior to a recursive call, check the black height of the sub-tree in the direction of the deletion. After the recursive call, check the black height of this sub-tree again and see if it is different. </li></p>

<p>  <li>Modify the return type of the recursive function to indicate a black node loss. </li><br />
</ol></p>

<p>Numbers one and two add a certain amount of complexity I feel is unnecessary. This is merely opinion though and you might feel differently. I chose number three for a couple reasons. First, it allows us to directly communicate a black node loss. Second, it does not clutter up the deletion algorithm with code which detracts from learning the mechanics of the algorithm.</p>

<p>So at this time I will introduce the <em>Nonad</em>:</p>

<blockquote>
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">class</span> Nonad<br />{<br />    <span style="color: #0000ff">public</span> Node Value { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> BlackLoss { get; set; }<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<p>A <em>Nonad</em> is a class composed of a <em>Node</em> and a Boolean value. The Boolean is used to indicate the loss of a black node in the sub-tree with the corresponding node as the root.</p>

<p>The name <em>Nonad</em> is most certainly a joke and is a combination of the words <em>Node</em> and <em>Monad</em>. The idea here borrows from the use of monads in software by supplementing (amplifying) the <em>Node</em> type with information regarding the loss of a black node in its sub-tree.</p>

<h2>The ground work</h2>

<p>I promise we’re close to the fun stuff. Next we lay the ground work for the <em>Remove</em> function by accounting for the recursion and the identification of the IOS.</p>

<blockquote>
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 400px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> Nonad Remove(Node current, <span style="color: #0000ff">int</span> toRemove)<br />{<br />    <span style="color: #0000ff">if</span> (current == <span style="color: #0000ff">null</span>)<br />    {<br />        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Node to remove, {0}, not found.&quot;</span>, toRemove.ToString()));<br />    }<br />    <span style="color: #0000ff">if</span> (current.Value == toRemove)<br />    {<br />        <span style="color: #0000ff">if</span> (IsInternalNode(current))<br />        {<br />            Node ios = InOrderSuccessor(current);<br />            current.Value = ios.Value;<br />            toRemove = ios.Value;<br />        }<br />        <span style="color: #0000ff">else</span><br />        {<br />            <span style="color: #008000">// what can we expect of this node?</span><br />        }<br />    }<br /><br />    Node.Direction deleteDirection = toRemove &lt; current.Value ? Node.Direction.Left : Node.Direction.Right;<br />    Node.Direction siblingDirection = Node.Opposite(deleteDirection);<br />    Nonad n = Remove(current[deleteDirection], toRemove);<br />    current[deleteDirection] = n.Value;<br />    <span style="color: #0000ff">if</span> (n.BlackLoss)<br />    {<br />        <span style="color: #008000">// we lost a black node in the direction of the deletion.</span><br />    }<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<p>Remember that when we find the node to delete it could be an internal node. When this happens, we need take the value of its IOS and then continue deleting the IOS. The code for identifying an internal node is also straightforward:</p>

<blockquote>
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> IsInternalNode(Node current)<br />{<br />    <span style="color: #0000ff">return</span> current != <span style="color: #0000ff">null</span> &amp;&amp; current[Node.Direction.Left] != <span style="color: #0000ff">null</span> &amp;&amp; current[Node.Direction.Right] != <span style="color: #0000ff">null</span>;<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<p>Regular readers of this blog will probably laugh at the use of the <a href="http://www.patrickdewane.com/2011/04/dear-null.html">null anti-pattern</a> here. In the context of this program however, this is not an issue (although there remains situational ambiguity).</p>

<p>Notice that once we find an internal node and get its IOS we do not return from the function, but rather proceed with recursing towards the IOS which is the node we need to delete.</p>

<p>The other case we need to account for is when we find the node to delete and it is not internal (a leaf or near-leaf). This node could be the IOS or the original node to delete. The code is the same in either case.</p>

<p>What can we expect of this leaf or near-leaf node? In the case of a red leaf node we can simply remove it without affecting the black height of the tree. It probably goes without saying that removing a red node does not cause a red violation.</p>

<p>If the node is a black leaf node, then we don’t have much a choice here. We have to remove the node and indicate the black node loss in our return type.</p>

<p>What if this node is near-leaf node? In this case we know that it has a single non-null child. If this is the IOS of the original node to delete, then it will have a non-null right child. However, the original node to delete may be a near-leaf and therefore could have a child on either side (but not both). This means we cannot assume the direction of the non-null child.</p>

<p>Could a near-leaf node be red? Assume it could. If this node has a red child, we would have a red violation. If this node has a black child we would have a black violation since the child in the opposite direction is null. Therefore a near-leaf node cannot be red.</p>

<p>Could a near-leaf node be black? Again, assume it could. This node could have a red child but not a black child. Just as above, a black child would indicate a black violation since the child in the opposite direction is null. Therefore a near-leaf node must be black and have a single non-null red child.</p>

<p>Deleting a black near-leaf node with a red child is very easy. We simply replace the black near-leaf <strong>X</strong> with the red leaf <strong>Y</strong> and then paint <strong>Y</strong> black:</p>

<blockquote>
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 400px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">if</span> (current.Value == toRemove)<br />{<br />    <span style="color: #0000ff">if</span> (IsInternalNode(current))<br />    {<br />        Node ios = InOrderSuccessor(current);<br />        current.Value = ios.Value;<br />        toRemove = ios.Value;<br />    }<br />    <span style="color: #0000ff">else</span><br />    {<br />        Node child = <span style="color: #0000ff">null</span>;<br />        <span style="color: #0000ff">bool</span> blackLoss = <span style="color: #0000ff">false</span>;<br />        <span style="color: #0000ff">if</span> (current.NodeColor == Node.Color.Black)<br />        {<br />            child = current[Node.Direction.Left] ?? current[Node.Direction.Right];<br />            <span style="color: #0000ff">if</span> (child == <span style="color: #0000ff">null</span>)<br />            {<br />                blackLoss = <span style="color: #0000ff">true</span>;<br />            }<br />            <span style="color: #0000ff">else</span><br />            {<br />                child.NodeColor = Node.Color.Black;<br />            }<br />        }<br />        <span style="color: #0000ff">return</span> ToNonad(child, blackLoss);<br />    }<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<p>To reiterate, we’re OK if we delete a red leaf node or a black near-leaf node with a red child. We get ourselves into a bit of trouble when deleting a black leaf node.</p>

<p>Next, we switch gears and focus on what to do after a recursive call and notice (via the amazingly resourceful <em>Nonad</em>) the loss of a black node. </p>

<h2>Fix it already</h2>

<p>Are you ready for a brave, brazen, and brash comment? Okay, well here it is:</p>

<blockquote>
  <p>Deleting a node from a red-black tree is not very difficult.</p>
</blockquote>

<p>At least not as difficult as many others make it sound and look. </p>

<p>Believe it or not, we did a lot of the heavy lifting last time when we observed, justified, and generalized our insertion scenarios. Our previous learning's will help us tremendously in this section.</p>

<p>To supplement our existing knowledge, we need to figure out how to balance the black height of a sub-tree after one of its branches loses a black node. Two ideas come to mind. First, we could decrease the black height of the other branch. Although this would balance the black height of the sub-tree, we would have to propagate the black height loss further up the tree. This might be our only recourse in certain circumstances. Second, we could rotate the tree in such a way as to put a black node in the branch that lost a black node.</p>

<p>In the spirit of the last post, we will refer to the root node of a sub-tree as <strong>P</strong>, the child in the path of the deletion <strong>X</strong>, and <strong>X</strong>’s sibling <strong>S</strong>. When you see a gray node, know that I got lazy and did not want to create additional figures for each possible combination of node colors. A gray node will be examined at a later point and could be red or black and in some cases null. In addition, I will also use two parallel lines to indicate the direction of a black node loss (which is also the path of the deletion).</p>

<p>I will refer to directions without explicitly mentioning <em>left</em> or <em>right</em>. Rather, I will talk about the <em>direction of the deletion</em> and the <em>direction of the sibling</em> as sufficient substitutes.</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed793883301538e6067a5970b-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb10" border="0" alt="rb10" src="http://patrickdewane.typepad.com/.a/6a00e5536ed793883301538e6067ac970b-pi" width="498" height="668" /></a> 

<p>    <br /><strong><em>Figure 1</em></strong></p><br />
</blockquote></p>

<p>Unlike last time, I did not present any scenarios that are invalid or impossible for us to encounter. At this point we know enough about red-black trees to not have to invalidate bunk scenarios. Also keep in mind that I drew only the cases where the deletion happened down the right sub-tree of the root node <strong>P</strong>.</p>

<ol>
  <li><strong>Figure 1.1</strong> 

<p>    <br /><em>The sibling <strong>S</strong> is red, which means the root of the tree <strong>P</strong> must be black. Assuming <strong>P</strong>’s branches had an equal black height prior to the deletion, we can make a few very important observations. The first observation is that <strong>S</strong> must have two black children. Second, if <strong>X</strong> is not null, the black height of <strong>S</strong> must be greater than one. This is indicated by the <strong>C1</strong> and <strong>C2</strong> nodes. I colored these node gray since they could be red with black children or black themselves. Notice that I did not draw the children of <strong>B1</strong>. I did this to emphasize the children of <strong>B2</strong>, as they might cause us some problems in the future.</em> </li></p>

<p>  <li><strong>Figure 1.2</strong> </p>

<p>    <br /><em>The sibling <strong>S</strong> is black. If <strong>X</strong> is null, then <strong>S</strong> cannot have any children. In this case the gray nodes <strong>C1</strong> and <strong>C2</strong> will be null. If <strong>X</strong> is not null, then <strong>S </strong>must have a black height greater than one. This means the nodes <strong>C1</strong> and <strong>C2</strong> could be black or red with black children.</em> </li></p>

<p>  <li><strong>Figure 1.3</strong> </p>

<p>    <br /><em>Same observations as above, but the root of the tree <strong>P</strong> is red.</em> </li></p>

<p>  <li><strong>Figure 1.4</strong> </p>

<p>    <br /><em>This is a special case of figures 1.2 and 1.3. In this case, <strong>S</strong> has a red child in the direction of the deletion and a black child in the sibling direction. This provides sufficient information to know that <strong>X</strong> is not null.</em> </li></p>

<p>  <li><strong>Figure 1.5</strong> </p>

<p>    <br /><em>This is the same case as figure 1.4, except that S has a red child in the sibling direction.</em> </li><br />
</ol></p>

<p>Currently, we know deleting a black leaf node causes us to propagate the black height loss up the tree for fixing. And since this is the only black height loss scenario we are aware of, we use this case to determine how to address the above figures.</p>

<p>In figure 1.1 the sibling <strong>S</strong> is red. <strong>S</strong> must have two black children, and since <strong>X</strong> is null those children must not have any black children of their own. Observe that the black heights of <strong>B1</strong> and <strong>B2</strong> must be the same as the black height of <strong>X</strong>. Since <strong>X</strong> is null, the black heights of these three nodes must be 0. Further, observe that the black height of <strong>P</strong> prior to the deletion was 1. To preserve this fact and fix the black node loss, we can rotate <strong>S</strong> in the direction of the deletion, making sure to color <strong>S</strong> black to preserve the color of the root:</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e884694e7970d-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb11" border="0" alt="rb11" src="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e884694ed970d-pi" width="476" height="169" /></a> 

<p>    <br /><strong><em>Figure 2</em></strong></p><br />
</blockquote></p>

<p>Remember that prior to the deletion, the black of height of the sub-tree rooted at <strong>P</strong> was 1. We’ve now moved our red node into the root of this sub-tree and colored it black. At first glance it looks like we’re done. However, if <strong>X</strong> is null we have a black height violation rooted at <strong>P </strong>because <strong>B2</strong> is not null. This is simple enough to deal with though by coloring <strong>B2</strong> red:</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833015432260a4b970c-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb12" border="0" alt="rb12" src="http://patrickdewane.typepad.com/.a/6a00e5536ed793883301538e5328b1970b-pi" width="476" height="169" /></a> 

<p>    <br /><strong><em>Figure 3</em></strong></p><br />
</blockquote></p>

<p><strong>X</strong> being null, we’ve re-balanced the black height of the <strong>S</strong>’s tree!</p>

<p>Remember my earlier comment regarding locality? This is where we benefit from dealing with violations at the leaf level. In the case where <strong>X</strong> is null, this black violation was very easy to remedy. Further, it has given us the perfect opportunity to build upon the leaf case and make it work for trees of arbitrary sizes. Which is what we will now do considering the cases where <strong>X</strong> is not null.</p>

<p>Since we colored <strong>B2</strong> red, we may have introduced a red violation since its children <strong>C1</strong> and/or <strong>C2</strong> could be red. We will casually call this node the <em>problem child</em>. Interestingly, note that the problem child is in the direction of the deletion (which is why we showed this nodes children in figure 1.1).</p>

<p>Thankfully we already know how to fix a red violation! I’ll briefly cover what to do, but refer to <a href="http://www.patrickdewane.com/2011/02/red-black-trees-oh-my-part-3.html">my post on insertion</a> for more details.</p>

<p>If <strong>C1</strong> is red, then we do not care about the color of <strong>C2</strong>. We simply color <strong>C1</strong> black and rotate the parent of the problem child in the direction of the deletion:</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833015432260f98970c-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb13" border="0" alt="rb13" src="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e88469a5b970d-pi" width="476" height="169" /></a> 

<p>    <br /><strong><em>Figure 3</em></strong></p><br />
</blockquote></p>

<p>Because we colored <strong>B2</strong> red and picked up the black node <strong>P</strong> in its path, we maintain that <strong>B1</strong> and <strong>B2</strong> have equal black heights. </p>

<p>Further, we’ve made up for the original black node loss introducing the black node <strong>P</strong> in the path of the deletion. This means the red sibling case affords us the opportunity to fix the black node loss without propagating it up the tree. </p>

<p>Now assume <strong>C1</strong> is black and <strong>C2</strong> is red (a jagged red violation). We know how to fix a straight red violation, so to make the jagged violation straight we rotate the problem child in the direction opposite that of the deletion. From there, we proceed with a straight red violation as depicted in figure 3.</p>

<p>We now know how to remedy the loss of a black node in the red sibling case. The black sibling cases are not terribly difficult either.</p>

<p>Remember that we’re looking for an opportunity to rotate a black node into <strong>X</strong>’s branch while still maintaining the black height of <strong>S</strong>’s branch. Let’s examine the cases where the black sibling <strong>S</strong> does not have any immediate red children, as shown by figures 1.2 and 1.3.</p>

<p>In these figures, assume the nodes <strong>C1</strong> and <strong>C2</strong> are either null or black. If these nodes are null, then so is <strong>X</strong>. Observe that since <strong>S</strong> is black, <strong>P</strong> can be black or red. In the case where <strong>P</strong> is black, we do not have a red node at our disposal and must propagate the black node loss up the tree. Before we do though, we can balance <strong>P</strong>’s black height by coloring <strong>S</strong> red. Doing so balances the black height at <strong>P</strong> since each of its branches lost a black node.</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e885384b5970d-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb14" border="0" alt="rb14" src="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e885384bb970d-pi" width="476" height="123" /></a> 

<p>    <br /><strong><em>Figure 4</em></strong></p><br />
</blockquote></p>

<p>In the event that <strong>P</strong> is red, we’re in luck. After coloring <strong>S</strong> red to balance each of <strong>P</strong>’s branches, we color <strong>P</strong> black to make up for the black height loss.</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed793883301538e602620970b-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb15" border="0" alt="rb15" src="http://patrickdewane.typepad.com/.a/6a00e5536ed79388330154323300b1970c-pi" width="476" height="123" /></a> 

<p>    <br /><strong><em>Figure 5</em></strong></p><br />
</blockquote></p>

<p>Looks good.</p>

<p>In figures 1.4 and 1.5 you’ll see <strong>S</strong> has a red child. We will refer to this node as the <em>niece</em> of <strong>X</strong>. Notice that the color of <strong>P</strong> is gray. In these figures the color of <strong>P</strong> is irrelevant since we have enough information to fix the tree without needing to leverage <strong>P</strong>’s color. If we happen to rotate a node into <strong>P</strong>’s position, we will be careful to maintain the color of the root.</p>

<p>Figure 1.4 shows us that the red node <strong>R1</strong> is in the sibling direction. We will call this a straight red niece (straight meaning <strong>R1</strong> is in the same position to <strong>S</strong> as <strong>S</strong> is to its parent <strong>P</strong>). Observe that the loss of a black node in <strong>X</strong>’s branch means that the sub-trees rooted at <strong>R1</strong> and <strong>B1</strong> now have black heights equal to <strong>X</strong>. This observation is the key to knowing what to do next.</p>

<p>If we could rotate <strong>P</strong> in the direction of the deletion and color it black, that would make up for the black node loss. This rotation would force <strong>S</strong> into the root of the tree, leaving the sibling branch short a black node. Simply coloring <strong>R1</strong> black would make up for this black node loss, balancing the tree.</p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed793883301538e6067b2970b-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb16" border="0" alt="rb16" src="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e8853c4f0970d-pi" width="580" height="169" /></a> 

<p>    <br /><strong><em>Figure 6</em></strong></p><br />
</blockquote></p>

<p>Keeping the color of the root intact, we rotated a black node into the direction of the deletion and balanced out the loss of <strong>S</strong> in the sibling direction by coloring <strong>R1</strong> black. If <strong>X</strong> is null, that means <strong>C1</strong>, <strong>C2</strong> and <strong>B1</strong> must also be null, meaning our tree is in great shape. If <strong>X</strong> is not null, how can we be sure that we haven’t thrown the black height of our tree off? </p>

<p>Recall that the black height of <strong>X</strong> (after it lost a black node) is equal to the black height of <strong>B1</strong>. Further, since we rotated the black node <strong>P</strong> into the direction of the deletion we have made up for the initial black node loss. On the other side of the tree, coloring <strong>R1</strong> black makes up for rotating <strong>S</strong> out of that branch and also balances out the addition of the black node <strong>P</strong> in the other branch. How great is that!</p>

<p>Our final case is figure 1.5, which shows us <strong>S</strong> has a red child <strong>R1</strong> in the direction of the deletion. We will call this a jagged red niece (jagged meaning <strong>R1</strong> is in the opposite position to <strong>S</strong> as <strong>S</strong> is to its parent <strong>P</strong>). </p>

<p>If we could rotate this figure in such a way as to create the straight red niece scenario, we would be finished. By now I’m sure there’s no surprise that we can do just that. Simply rotate <strong>S</strong> in the sibling direction and color <strong>S</strong> red and <strong>R1</strong> black. </p>

<p>&#160;</p>

<blockquote>
  <p><a href="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833014e8853c4fd970d-pi"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="rb17" border="0" alt="rb17" src="http://patrickdewane.typepad.com/.a/6a00e5536ed7938833015432334216970c-pi" width="535" height="169" /></a> 

<p>    <br /><strong><em>Figure 7</em></strong></p><br />
</blockquote></p>

<p>As you can see, prior to the rotation <strong>R1</strong> had no impact on the black height of <strong>S</strong>’s sub-tree. This means the nodes <strong>B1</strong>, <strong>C2</strong> and <strong>C2</strong> must have equal black heights. We use these observations to conclude that we maintained the black height and the balance of the sibling branch.</p>

<p>Then we proceed with fixing the straight red niece scenario as shown in figure 6.</p>

<p>And now you know how to delete nodes from a red-black tree.</p>

<h2>The code</h2>

<p>Since I have code scattered all throughout this post, here is a consolidated form of the code necessary to delete a node from a red-black tree. You will notice some overlap between what you see here what I posted last time.</p>

<blockquote>
  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 600px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Remove(<span style="color: #0000ff">int</span> <span style="color: #0000ff">value</span>)<br />{<br />    _root = Remove(_root, <span style="color: #0000ff">value</span>).Value;<br />}<br /><br /><span style="color: #0000ff">private</span> Nonad Remove(Node current, <span style="color: #0000ff">int</span> toRemove)<br />{<br />    <span style="color: #0000ff">if</span> (current == <span style="color: #0000ff">null</span>)<br />    {<br />        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> Exception(<span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;Node to remove, {0}, not found.&quot;</span>, toRemove.ToString()));<br />    }<br />    <span style="color: #0000ff">if</span> (current.Value == toRemove)<br />    {<br />        <span style="color: #0000ff">if</span> (IsInternalNode(current))<br />        {<br />            Node ios = InOrderSuccessor(current);<br />            current.Value = ios.Value;<br />            toRemove = ios.Value;<br />        }<br />        <span style="color: #0000ff">else</span><br />        {<br />            Node child = <span style="color: #0000ff">null</span>;<br />            <span style="color: #0000ff">bool</span> blackLoss = <span style="color: #0000ff">false</span>;<br />            <span style="color: #0000ff">if</span> (current.NodeColor == Node.Color.Black)<br />            {<br />                child = current[Node.Direction.Left] ?? current[Node.Direction.Right];<br />                <span style="color: #0000ff">if</span> (child == <span style="color: #0000ff">null</span>)<br />                {<br />                    blackLoss = <span style="color: #0000ff">true</span>;<br />                }<br />                <span style="color: #0000ff">else</span><br />                {<br />                    child.NodeColor = Node.Color.Black;<br />                }<br />            }<br />            <span style="color: #0000ff">return</span> ToNonad(child, blackLoss);<br />        }<br />    }<br /><br />    Node.Direction deleteDirection = toRemove &lt; current.Value ? Node.Direction.Left : Node.Direction.Right;<br />    Node.Direction siblingDirection = Node.Opposite(deleteDirection);<br />    Nonad n = Remove(current[deleteDirection], toRemove);<br />    current[deleteDirection] = n.Value;<br />    <span style="color: #0000ff">bool</span> propogateBlackLoss = <span style="color: #0000ff">false</span>;<br />    <span style="color: #0000ff">if</span> (n.BlackLoss)<br />    {<br />        Node sibling = current[siblingDirection];<br />        <span style="color: #0000ff">if</span> (IsRed(sibling))<br />        {<br />            sibling.NodeColor = Node.Color.Black;<br />            Node problemChild = sibling[deleteDirection];<br />            problemChild.NodeColor = Node.Color.Red;<br />            current = Rotate(current, deleteDirection);<br /><br />            Node doubleRed = IsRed(problemChild[siblingDirection]) ? problemChild[siblingDirection] : IsRed(problemChild[deleteDirection]) ? problemChild[deleteDirection] : <span style="color: #0000ff">null</span>;<br />            <span style="color: #0000ff">if</span> (doubleRed != <span style="color: #0000ff">null</span>)<br />            {<br />                <span style="color: #0000ff">bool</span> jagged = doubleRed == problemChild[deleteDirection];<br />                <span style="color: #0000ff">if</span> (jagged)<br />                {<br />                    current[deleteDirection][siblingDirection] = Rotate(current[deleteDirection][siblingDirection], siblingDirection);<br />                    problemChild = doubleRed;<br />                }<br />                problemChild[siblingDirection].NodeColor = Node.Color.Black;<br />                current[deleteDirection] = Rotate(current[deleteDirection], deleteDirection);<br />            }<br />        }<br />        <span style="color: #0000ff">else</span><br />        {<br />            Node redNiece = IsRed(sibling[siblingDirection]) ? sibling[siblingDirection] : IsRed(sibling[deleteDirection]) ? sibling[deleteDirection] : <span style="color: #0000ff">null</span>;<br />            <span style="color: #0000ff">if</span> (redNiece == <span style="color: #0000ff">null</span>)<br />            {<br />                sibling.NodeColor = Node.Color.Red;<br />                <span style="color: #0000ff">if</span> (current.NodeColor == Node.Color.Black)<br />                {<br />                    propogateBlackLoss = <span style="color: #0000ff">true</span>;<br />                }<br />                <span style="color: #0000ff">else</span><br />                {<br />                    current.NodeColor = Node.Color.Black;<br />                }<br />            }<br />            <span style="color: #0000ff">else</span><br />            {<br />                <span style="color: #0000ff">bool</span> jagged = redNiece == sibling[deleteDirection];<br />                <span style="color: #0000ff">if</span> (jagged)<br />                {<br />                    sibling.NodeColor = Node.Color.Red;<br />                    redNiece.NodeColor = Node.Color.Black;<br />                    current[siblingDirection] = Rotate(sibling, siblingDirection);<br />                    sibling = current[siblingDirection];<br />                }<br />                sibling.NodeColor = current.NodeColor;<br />                current.NodeColor = Node.Color.Black;<br />                sibling[siblingDirection].NodeColor = Node.Color.Black;<br />                current = Rotate(current, deleteDirection);<br />            }<br />        }<br />    }<br />    <span style="color: #0000ff">return</span> ToNonad(current, propogateBlackLoss);<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Rotates the tree rooted at Root the direction specified by RotationDirection. The new root node of the sub tree is returned.</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;root&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;rotationDirection&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">private</span> Node Rotate(Node root, Node.Direction rotationDirection)<br />{<br />    Node.Direction opposite = Node.Opposite(rotationDirection);<br />    Node pivot = root[opposite];<br />    root[opposite] = pivot[rotationDirection];<br />    pivot[rotationDirection] = root;<br />    <span style="color: #0000ff">return</span> pivot;<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Returns true if the node is not null and red.</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;current&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> IsRed(Node current)<br />{<br />    <span style="color: #0000ff">return</span> current != <span style="color: #0000ff">null</span> &amp;&amp; current.NodeColor == Node.Color.Red;<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Returns the in-order sucessor of the specific node.</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;current&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">private</span> Node InOrderSuccessor(Node current)<br />{<br />    <span style="color: #0000ff">if</span> (current == <span style="color: #0000ff">null</span>)<br />    {<br />        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;<br />    }<br />    Func&lt;Node, Node&gt; ios = <span style="color: #0000ff">null</span>;<br />    ios = c =&gt;<br />    {<br />        <span style="color: #0000ff">if</span> (c[Node.Direction.Left] == <span style="color: #0000ff">null</span>)<br />        {<br />            <span style="color: #0000ff">return</span> c;<br />        }<br />        <span style="color: #0000ff">return</span> ios(c[Node.Direction.Left]); <span style="color: #008000">// tail recursive, bam.</span><br />    };<br />    <span style="color: #0000ff">return</span> ios(current[Node.Direction.Right]);<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Returns true if the node is internal to the tree.</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;current&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> IsInternalNode(Node current)<br />{<br />    <span style="color: #0000ff">return</span> current != <span style="color: #0000ff">null</span> &amp;&amp; current[Node.Direction.Left] != <span style="color: #0000ff">null</span> &amp;&amp; current[Node.Direction.Right] != <span style="color: #0000ff">null</span>;<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Wraps the Node into a Nonad.</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;node&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;doubleBlackViolation&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">private</span> Nonad ToNonad(Node node, <span style="color: #0000ff">bool</span> doubleBlackViolation)<br />{<br />    <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> Nonad() { Value = node, BlackLoss = doubleBlackViolation };<br />}</pre>

<p>    <br /></div><br />
</blockquote></p>

<h2>Next time</h2>

<p>I would like to wrap things up next time by showing how to validate a red-black tree automagically. Also, I’ll show what I did to randomly shuffle fixed sized datasets used to build up a tree and tear it right back down. It would also make sense to provide all the code shown throughout this series in one place, so I’ll take care of that as well.</p>

<p>Take care!</p>

<h2>Parts</h2>

<ol>
  <li><a href="http://www.patrickdewane.com/2011/01/red-black-trees-oh-my.html">Introduction</a> </li>

<p>  <li><a href="http://www.patrickdewane.com/2011/02/red-black-trees-oh-my-part-2.html">Explanation</a> </li></p>

<p>  <li><a href="http://www.patrickdewane.com/2011/02/red-black-trees-oh-my-part-3.html">Insertion</a> </li></p>

<p>  <li>Deletion (you are here) </li></p>

<p>  <li>Validation and testing </li><br />
</ol></p>

<p><sup>1</sup> – Lame, I know.</p><img src="http://feeds.feedburner.com/~r/patrickdewanegatherswool/~4/pSFST-tBtHg" height="1" width="1"/>]]></content:encoded>


<category>Algorithms</category>
<category>C#</category>
<category>Digging Deep</category>
<category>Programming</category>

<dc:creator>Patrick Dewane</dc:creator>
<pubDate>Mon, 09 May 2011 15:43:00 -0500</pubDate>

<feedburner:origLink>http://www.patrickdewane.com/2011/05/red-black-trees-oh-my-part-4.html</feedburner:origLink></item>

</channel>
</rss><!-- ph=1 -->
