mirror of
https://github.com/mfocko/blog.git
synced 2024-11-14 01:59:42 +01:00
107 lines
124 KiB
HTML
107 lines
124 KiB
HTML
|
<!doctype html>
|
|||
|
<html lang="en" dir="ltr" class="docs-wrapper docs-doc-page docs-version-current plugin-docs plugin-id-ib002 docs-doc-id-rb-trees/rules">
|
|||
|
<head>
|
|||
|
<meta charset="UTF-8">
|
|||
|
<meta name="generator" content="Docusaurus v2.4.1">
|
|||
|
<title data-rh="true">On the rules of the red-black tree | mf</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://blog.mfocko.xyz/ib002/rb-trees/rules"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-ib002-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-ib002-current"><meta data-rh="true" property="og:title" content="On the rules of the red-black tree | mf"><meta data-rh="true" name="description" content="Shower thoughts on the rules of the red-black tree.
|
|||
|
"><meta data-rh="true" property="og:description" content="Shower thoughts on the rules of the red-black tree.
|
|||
|
"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://blog.mfocko.xyz/ib002/rb-trees/rules"><link data-rh="true" rel="alternate" href="https://blog.mfocko.xyz/ib002/rb-trees/rules" hreflang="en"><link data-rh="true" rel="alternate" href="https://blog.mfocko.xyz/ib002/rb-trees/rules" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://0VXRFPR4QF-dsn.algolia.net" crossorigin="anonymous"><link rel="search" type="application/opensearchdescription+xml" title="mf" href="/opensearch.xml">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="mf RSS Feed">
|
|||
|
<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="mf Atom Feed">
|
|||
|
<link rel="alternate" type="application/json" href="/blog/feed.json" title="mf JSON Feed">
|
|||
|
|
|||
|
|
|||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css" integrity="sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM" crossorigin="anonymous"><link rel="stylesheet" href="/assets/css/styles.8480cb83.css">
|
|||
|
<link rel="preload" href="/assets/js/runtime~main.be793c46.js" as="script">
|
|||
|
<link rel="preload" href="/assets/js/main.9283b308.js" as="script">
|
|||
|
</head>
|
|||
|
<body class="navigation-with-keyboard">
|
|||
|
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
|
|||
|
<div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><b class="navbar__title text--truncate">mf</b></a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">Additional FI MU materials</a><ul class="dropdown__menu"><li><a aria-current="page" class="dropdown__link dropdown__link--active" href="/ib002/">IB002: Algorithms</a></li><li><a class="dropdown__link" href="/pb071/">PB071: C</a></li><li><a class="dropdown__link" href="/pb161/">PB161: C++</a></li></ul></div><a class="navbar__item navbar__link" href="/contributions">Contributions</a><a class="navbar__item navbar__link" href="/talks">Talks</a></div><div class="navbar__items navbar__items--right"><a class="navbar__item navbar__link" href="/blog">Blog</a><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-live="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" class="darkToggleIcon_wfgR"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div><div class="searchBox_ZlJk"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_
|
|||
|
formulated the way they are? How come they keep the tree balanced? Let's go through
|
|||
|
each of the red-black tree rules and try to change, break and contemplate about
|
|||
|
them.</p><p>We expect that you are familiar with the following set of the rules<sup id="fnref-1"><a href="#fn-1" class="footnote-ref">1</a></sup>:</p><ol><li>Every node is either red or black.</li><li>The root is black.</li><li>Every leaf (<code>nil</code>) is black.</li><li>If a node is red, then both its children are black.</li><li>For each node, all simple paths from the node to descendant leaves contain the
|
|||
|
same number of black nodes.</li></ol><p>Each section will go into <em>reasonable</em> details of each rule.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="1ª-every-node-is-either-red-or-black">1ª Every node is either red or black.<a href="#1ª-every-node-is-either-red-or-black" class="hash-link" aria-label="Direct link to 1ª Every node is either red or black." title="Direct link to 1ª Every node is either red or black."></a></h2><p>OK… This one is very simple. It is just a definition and is used in all other
|
|||
|
rules. Not much to talk about here. Or is there?</p><h3 class="anchor anchorWithStickyNavbar_LWe7" id="do-i-really-need-the-nodes-to-be-explicitly-colored">Do I really need the nodes to be explicitly colored?<a href="#do-i-really-need-the-nodes-to-be-explicitly-colored" class="hash-link" aria-label="Direct link to Do I really need the nodes to be explicitly colored?" title="Direct link to Do I really need the nodes to be explicitly colored?"></a></h3><p>The answer is no. Balancing of the red-black trees is “enforced” by the 4th and
|
|||
|
5th rule in the enumeration above. There are many ways you can avoid using colors.</p><h4 class="anchor anchorWithStickyNavbar_LWe7" id="black-height">Black height<a href="#black-height" class="hash-link" aria-label="Direct link to Black height" title="Direct link to Black height"></a></h4><p>We mentioned the 4th and 5th rule and that it enforces the balancing. What does
|
|||
|
it mean for us?</p><p>Well, we definitely do not have to use the colors, which even as a <em>boolean</em> flag
|
|||
|
would take at least 1 byte of space (and usually even more), cause… well, it is
|
|||
|
easier for the CPU to work with words rather than single bits.</p><p>We could use the black height, couldn't we? It would mean more memory used, cause
|
|||
|
it should be ideally big and unsigned. Can we tell the color of a node from the
|
|||
|
black height? Of course we can, if my child has the same black height as I do,
|
|||
|
it means that there was no black node added on the path between us and therefore
|
|||
|
my child would be colored red.</p><p>Example of a red-black tree that keeps count of black nodes on paths to the
|
|||
|
leaves follows:</p><p><img loading="lazy" alt="Red-black tree with black height" src="/assets/images/rb_height_light-36fa69317ced094d7bb7b0fdf32cb3fe.png#gh-light-mode-only" width="923" height="539" class="img_ev3q">
|
|||
|
<img loading="lazy" alt="Red-black tree with black height" src="/assets/images/rb_height_dark-75a70ddff74e5e1aff7e9986221b5687.png#gh-dark-mode-only" width="923" height="539" class="img_ev3q"></p><p>We mark the <em>black heights</em> in superscript. You can see that all leaves have the
|
|||
|
black height equal to <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn></mrow><annotation encoding="application/x-tex">1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">1</span></span></span></span></span>. Let's take a look at some of the interesting cases:</p><ul><li><p>If we take a look at the node with <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>key</mtext><mo>=</mo><mn>9</mn></mrow><annotation encoding="application/x-tex">\text{key} = 9</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em"></span><span class="mord text"><span class="mord">key</span></span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">9</span></span></span></span></span>, we can see that it is
|
|||
|
coloured red and its black height is 1, because it is a leaf.</p><p>Let's look at its parent (node with <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>key</mtext><mo>=</mo><mn>8</mn></mrow><annotation encoding="application/x-tex">\text{key} = 8</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em"></span><span class="mord text"><span class="mord">key</span></span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">8</span></span></span></span></span>). On its left side it has
|
|||
|
<code>nil</code> and on its right side the <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>9</mn></mrow><annotation encoding="application/x-tex">9</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">9</span></span></span></span></span>. And its black height is still <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn></mrow><annotation encoding="application/x-tex">1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">1</span></span></span></span></span>, cause
|
|||
|
except for the <code>nil</code> leaves, there are no other black nodes.</p><p>We can clearly see that if a node has the same black height as its parent, it
|
|||
|
is a red node.</p></li><li><p>Now let's take a look at the root with <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>key</mtext><mo>=</mo><mn>3</mn></mrow><annotation encoding="application/x-tex">\text{key} = 3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em"></span><span class="mord text"><span class="mord">key</span></span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">3</span></span></span></span></span>. It has a black height
|
|||
|
of 3. Both of its children are black nodes and have black height of 2.</p><p>We can see that if a node has its height 1 lower than its parent, it is a black
|
|||
|
node.</p><p>The reasoning behind it is rather simple, we count the black nodes all the way
|
|||
|
to the leaves, therefore if my parent has a higher black height, it means that
|
|||
|
on the path from me to my parent there is a black node, but the only node added
|
|||
|
is me, therefore I must be black.</p></li></ul><h4 class="anchor anchorWithStickyNavbar_LWe7" id="isomorphic-trees">Isomorphic trees<a href="#isomorphic-trees" class="hash-link" aria-label="Direct link to Isomorphic trees" title="Direct link to Isomorphic trees"></a></h4><p>One of the other ways to avoid using color is storing the red-black tree in some
|
|||
|
isomorphic tree. The structure of 2-3-4 tree allows us to avoid using the color
|
|||
|
completely. This is a bit different approach, cause we would be basically using
|
|||
|
different tree, so we keep this note in just as a “hack”.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="2ª-the-root-is-black">2ª The root is black.<a href="#2ª-the-root-is-black" class="hash-link" aria-label="Direct link to 2ª The root is black." title="Direct link to 2ª The root is black."></a></h2><p>This rule might seem like a very important one, but overall is not. You can safely
|
|||
|
omit this rule, but you also need to deal with the consequences.</p><p>Let's refresh our memory with the algorithm of <em>insert fixup</em>:</p><div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#000000;--prism-background-color:#ffffff"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#000000"><span class="token plain">WHILE z.p.color == Red</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> IF z.p == z.p.p.left</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> y = z.p.p.right</span><br></span><span class="token-line" style="color:#000000"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> IF y.color == Red</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z.p.color = Black</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> y.color = Black</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z.p.p.color = Red</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z = z.p.p</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> ELSE</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> IF z == z.p.right</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z = z.p</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> Left-Rotate(T, z)</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z.p.color = Black</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> z.p.p.color = Red</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> Right-Rotate(T, z.p.p)</span><br></span><span class="token-line" style="color:#000000"><span class="token plain"> ELSE (same as above with “right” and “left” exchanged)</span><br></span><span class="token-line" style="color:#000000"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#000000"><span class="token plain">T.root.color = Black</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><div class="theme-admonition theme-admonition-tip alert alert--success admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><div class="admonitionContent_S0QG"><p>If you have tried to implement any of the more complex data structures, such as
|
|||
|
red-black trees, etc., in a statically typed language that also checks you for
|
|||
|
<code>NULL</code>-correctness (e.g. <em>mypy</em> or even C# with nullable reference types), you
|
|||
|
might have run into numerous issues in the cases where you are 100% sure that you
|
|||
|
cannot obtain <code>NULL</code> because of the invariants, but the static type checking
|
|||
|
doesn't know that.</p><p>The issue we hit with the <em>insert fixup</em> is very similar.</p></div></div><p>You might not realize the issue at the first sight, but the algorithm described
|
|||
|
with the pseudocode above expects that the root of the red-black tree is black by
|
|||
|
both relying on the invariant in the algorithm and afterwards by enforcing the
|
|||
|
black root property.</p><p>If we decide to omit this condition, we need to address it in the pseudocodes
|
|||
|
accordingly.</p><table><thead><tr><th align="center">Usual algorithm with black root</th><th align="center">Allowing red root</th></tr></thead><tbody><tr><td align="center"><img loading="lazy" alt="1ª insertion" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALMAAACbCAYAAAAp66qoAAAABmJLR0QA/wD/AP+gvaeTAAASYklEQVR4nO3deVSU1R8G8Ocdhh0FwQVBfyaoECCeLEsMBVMwj1tJLkFJWrnXCcXlZG7Z4QRamNvxmGZFLoBLmkKIiigkirugssqiICAuIOAMzHx/fxQezd2Y9w537ucc/nCcw32AZy6Xd973vhIREQSh+YtVsE4gCE1FlFnghiizwA0l6wA80Wg0KCsrQ1lZGW7dugWNRoPq6mo0NDTAwsICpqamMDc3h42NDdq3bw9bW1vWkbkiyvwC6urqkJ6ejnPnziEjIwOZmZnIy8tDeXk5NBrNM38eMzMzdOjQAS4uLvDw8IC7uzt69uwJNzc3SJKkw6+AT5I4mvF0Wq0Wx44dQ1xcHA4dOoT09HSoVCrY2treK6GLiwvat28PBwcHtGvXDra2tlAoFGjRogWUSiVqa2uhUqlw9+5d3LhxAyUlJSgtLUVxcTEuXLiAzMxMXLx4EWq1Gm3atEHfvn3Rv39/DB8+HP/73/9Yfwuag1hR5idITU3Fpk2bsGvXLpSUlMDZ2Rm+vr7w8fGBj49Pk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go through some of the possible ways I can look at this and
|
|||
|
how would they affect the other rules and balancing.</p><p>We will experiment with the following tree:
|
|||
|
<img loading="lazy" src="/assets/images/rb_light-f53bee3b32ddb2e7a4249828bc03b1a4.png#gh-light-mode-only" width="899" height="539" class="img_ev3q">
|
|||
|
<img loading="lazy" src="/assets/images/rb_dark-c025d61dee7913262c86277087751328.png#gh-dark-mode-only" width="899" height="539" class="img_ev3q"></p><p>We should start by counting the black nodes from root to the <code>nil</code> leaves based
|
|||
|
on the rules. We have multiple similar paths, so we will pick only the interesting
|
|||
|
ones.</p><ol><li>What happens if we do not count the <code>nil</code> leaves?</li><li>What happens if we consider leaves the nodes with <em>no descendants</em>, i.e. both
|
|||
|
of node's children are <code>nil</code>?</li><li>What happens if we do not count the <code>nil</code> leaves, but consider nodes with at
|
|||
|
least one <code>nil</code> descendant as leaves?</li></ol><table><thead><tr><th align="right">path</th><th align="right">black nodes</th><th align="right">1ª idea</th><th align="right">2ª idea</th><th align="right">3ª idea</th></tr></thead><tbody><tr><td align="right"><code>3 → 1 → 0 → nil</code></td><td align="right">4</td><td align="right">3</td><td align="right">4</td><td align="right">3</td></tr><tr><td align="right"><code>3 → 5 → 7 → 8 → nil</code></td><td align="right">4</td><td align="right">3</td><td align="right">-</td><td align="right">3</td></tr><tr><td align="right"><code>3 → 5 → 7 → 8 → 9 → nil</code></td><td align="right">4</td><td align="right">3</td><td align="right">4</td><td align="right">3</td></tr></tbody></table><p>First idea is very easy to execute and it is also very easy to argue about its
|
|||
|
correctness. It is correct, because we just subtract one from each of the paths.
|
|||
|
This affects <strong>all</strong> paths and therefore results in global decrease by one.</p><p>Second idea is a bit more complicated. We count the <code>nil</code>s, so the count is <span class="math math-inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>4</mn></mrow><annotation encoding="application/x-tex">4</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em"></span><span class="mord">4</span></span></span></span></span>
|
|||
|
as it should be. However, there is one difference. Second path no longer satisfies
|
|||
|
the condition of a <em>leaf</em>. Technically it relaxes the 5th rule, because we leave
|
|||
|
out some of the nodes. We should probably avoid that.</p><div class="theme-admonition theme-admonition-caution alert alert--warning admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><div class="admonitionContent_S0QG"><p>With the second idea, you may also feel that we are “bending” the rules a bit,
|
|||
|
especially the definition of the “leaf” nodes.</p><p>Given the definition of the red-black tree, where <code>nil</code> is considered to be an
|
|||
|
external node, we have decided that bending it a bit just to stir a thought about
|
|||
|
it won't hurt anybody. 😉</p></div></div><h2 class="anchor anchorWithStickyNavbar_LWe7" id="4ª-if-a-node-is-red-then-both-its-children-are-black">4ª If a node is red, then both its children are black.<a href="#4ª-if-a-node-is-red-then-both-its-children-are-black" class="hash-link" aria-label="Direct link to 4ª If a node is red, then both its children are black." title="Direct link to 4ª If a node is red, then both its children are black."></a></h2><p>This rule might seem rather silly on the first look, but there are 2 important
|
|||
|
functions:</p><ol><li>it allows the algorithms to <em>“notice”</em> that something went wrong (i.e. the
|
|||
|
tree needs to be rebalanced), and</li><li>it holds the balancing and height of the tree <em>“in check”</em> (with the help of
|
|||
|
the 5th rule).</li></ol><p>When we have a look at the algorithms that are used for fixing up the red-black
|
|||
|
tree after an insertion or deletion, we will notice that all the algorithms need
|
|||
|
is the color of the node. </p><blockquote><p>How come it is the only thing that we need?
|
|||
|
How come such naïve thing can be enough?</p></blockquote><p>Let's say we perform an insertion into the tree… We go with the usual and pretty
|
|||
|
primitive insertion into the binary-search tree and then, if needed, we “fix up”
|
|||
|
broken invariants. <em>How can that be enough?</em> With each insertion and deletion we
|
|||
|
maintain the invariants, therefore if we break them with one operation, there's
|
|||
|
only one path on which the invariants were <em>felled</em>. If we know that rest of the
|
|||
|
tree is correct, it allows us to fix the issues just by propagating it to the
|
|||
|
root and <em>abusing</em> the siblings (which are, of course, correct red-black
|
|||
|
subtrees) to fix or at least partially mitigate the issues and propagate them
|
|||
|
further.</p><p>Let's assume that we do not enforce this rule, you can see how it breaks the
|
|||
|
balancing of the tree below.</p><div class="tabs-container tabList__CuJ"><ul role="tablist" aria-orientation="horizontal" class="tabs"><li role="tab" tabindex="0" aria-selected="true" class="tabs__item tabItem_LNqP tabs__item--active">Enforcing this rule</li><li role="tab" tabindex="-1" aria-selected="false" class="tabs__item tabItem_LNqP">Omitting this rule</li></ul><div class="margin-top--md"><div role="tabpanel" class="tabItem_Ymn6"><p><img loading="lazy" src="/assets/images/correct_light-bc4770146072f748be4a5aa11abf3a0c.png#gh-light-mode-only" width="755" height="347" class="img_ev3q">
|
|||
|
<img loading="lazy" src="/assets/images/correct_dark-bbd8d4c1796b145025fed5b6dff03b84.png#gh-dark-mode-only" width="755" height="347" class="img_ev3q"></p></div><div role="tabpanel" class="tabItem_Ymn6" hidden=""><p><img loading="lazy" src="/assets/images/incorrect_light-e787e568e9a1528dcac5bf55ef29fdaa.png#gh-light-mode-only" width="803" height="443" class="img_ev3q">
|
|||
|
<img loading="lazy" src="/assets/images/incorrect_dark-9b8b3be328ffad83233de4536c120016.png#gh-dark-mode-only" width="803" height="443" class="img_ev3q"></p></div></div></div><p>We can create a <strong>big</strong> subtree with only red nodes and <strong>even</strong> when keeping
|
|||
|
the rest of the rules maintained, it will break the time complexity. It stops us
|
|||
|
from “hacking” the black height requirement laid by the 5th rule.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="5ª-for-each-node-all-simple-paths-from-the-node-to-descendant-leaves-contain-the-same-number-of-black-nodes">5ª For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.<a href="#5ª-for-each-node-all-simple-paths-from-the-node-to-descendant-leaves-contain-the-same-number-of-black-nodes" class="hash-link" aria-label="Direct link to 5ª For each node, all simple paths from the node to descendant leaves contain the same number of black nodes." title="Direct link to 5ª For each node, all simple paths from the node to descendant leaves contain the same number of black nodes."></a></h2><p>As it was mentioned, with the 4th rule they hold the balancing of the red-black
|
|||
|
tree.</p><div class="theme-admonition theme-admonition-tip alert alert--success admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><div class="admonitionContent_S0QG"><p>An important observation here is the fact that the red-black tree is a
|
|||
|
<strong>height</strong>-balanced tree.</p></div></div><p>Enforcing this rule (together with the 4th rule) keeps the tree balanced:</p><ol><li>4th rule makes sure we can't “hack” this requirement.</li><li>This rule ensures that we have “similar”<sup id="fnref-2"><a href="#fn-2" class="footnote-ref">2</a></sup> length to each of the leaves.</li></ol><div class="theme-admonition theme-admonition-tip alert alert--success admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>AVL tree</div><div class="admonitionContent_S0QG"><p>You might have heard about an <em>AVL tree</em> before. It is the first self-balanced
|
|||
|
tree to be ever introduced and works in a very similar nature as the red-black
|
|||
|
tree, the only difference is that it does not deal with the <em>black height</em>, but
|
|||
|
the height in general.</p><p>If you were to compare AVL with the red-black tree, you can say that AVL is much
|
|||
|
more strict while red-black tree can still maintain the same asymptotic time
|
|||
|
complexity for the operations, but having more relaxed rules.</p></div></div><div class="footnotes"><hr><ol><li id="fn-1">CORMEN, Thomas. Introduction to algorithms. Cambridge, Mass: MIT Press, 2009. isbn 9780262033848.<a href="#fnref-1" class="footnote-backref">↩</a></li><li id="fn-2">red nodes still exist<a href="#fnref-2" class="footnote-backref">↩</a></li></ol></div></div><footer class="theme-doc-footer docusaurus-mt-lg"><div class="theme-doc-footer-tags-row row margin-bottom--sm"><div class="col"><b>Tags:</b><ul class="tags_jXut padding--none margin-left--sm"><li class="tag_QGVx"><a class="tag_zVej tagRegular_sFm0" href="/ib002/tags/red-black-trees">red-black trees</a></li><li class="tag_QGVx"><a class="tag_zVej tagRegular_sFm0" href="/ib002/tags/balanced-trees">balanced trees</a></li></ul></div></div><div class="theme-doc-footer-edit-meta-row row"><div class="col"><a href="https://gitlab.com/mfocko/blog/tree/main/ib002/08-rb-trees/2023-06-10-rules.md" target="_blank" rel="noreferrer noopener" class="theme-edit-this-page"><svg fill="currentColor" height="20" width="20" viewBox="0 0 40 40" class="iconEdit_Z9Sw" aria-hidden="true"><g><path d="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><div class="col lastUpdated_vwxv"><span class="theme-last-updated">Last updated<!-- --> on <b><time datetime="2023-06-10T00:00:00.000Z">Jun 10, 2023</time></b></span></div></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/ib002/rb-trees/applications"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Použití červeno-černých stromů</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/ib002/category/graphs"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Graphs</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#introduction" class="table-of-contents__link toc-highlight">Introduction</a></li><li><a href="#1ª-every-node-is-either-red-or-black" class="table-of-contents__link toc-highlight">1ª Every node is either red or black.</a><ul><li><a href="#do-i-really-need-the-nodes-to-be-explicitly-colored" class="table-of-contents__link toc-highlight">Do I really need the nodes to be explicitly colored?</a></li></ul></li><li><a href="#2ª-the-root-is-black" class="table-of-contents__link toc-highlight">2ª The root is black.</a></li><li><a href="#3ª-every-leaf-nil-is-black" class="table-of-contents__link toc-highlight">3ª Every leaf (<code>nil</code>) is black.</a></li><li><a href="#4ª-if-a-node-is-red-then-both-its-children-are-black" class="table-of-contents__link toc-highlight">4ª If a node is red, then both its children are black.</a></li><li><a href="#5ª-for-each-node-all-simple-paths-from-the-node-to-descendant-leaves-contain-the-same-number-of-black-nodes" class="table-of-contents__link toc-highlight">5ª For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.</a></li></ul></div></div></div></div></main></div></div><footer class="footer footer--dark"><div class="container container-fluid"><div class="row footer__links"><div class="col footer__col"><div class="footer__title">Git</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://github.com/mfocko" target="_blank" rel="noopener noreferrer" class="footer__link-item">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://gitlab.com/mfocko" target="_blank" rel="noop
|
|||
|
<script src="/assets/js/runtime~main.be793c46.js"></script>
|
|||
|
<script src="/assets/js/main.9283b308.js"></script>
|
|||
|
</body>
|
|||
|
</html>
|