blog/algorithms/rb-trees/rules/index.html

196 lines
285 KiB
HTML
Raw Normal View History

<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-algorithms docs-version-current docs-doc-page docs-doc-id-rb-trees/rules" data-has-hydrated="false">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.0.0">
<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/algorithms/rb-trees/rules/"><meta data-rh="true" property="og:locale" content="en"><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-algorithms-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-algorithms-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/algorithms/rb-trees/rules/"><link data-rh="true" rel="alternate" href="https://blog.mfocko.xyz/algorithms/rb-trees/rules/" hreflang="en"><link data-rh="true" rel="alternate" href="https://blog.mfocko.xyz/algorithms/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.0f577c26.css">
<script src="/assets/js/runtime~main.1c96a548.js" defer="defer"></script>
<script src="/assets/js/main.92563b55.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</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="/algorithms/">Algorithms</a></li><li><a class="dropdown__link" href="/c/">C</a></li><li><a class="dropdown__link" href="/cpp/">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="navbarSearchContainer_Bca1"><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.
<p>Have you ever thought about the red-black tree rules in more depth? Why are they
formulated the way they are? How come they keep the tree balanced? Let&#x27;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><a href="#user-content-fn-1" id="user-content-fnref-1" data-footnote-ref="true" aria-describedby="footnote-label">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&#x27;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-0aff6e7a40a9f601e0dd1114e43e43b1.svg#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-921b2d98d9fe1e579474faf36486f281.svg#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="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>. Let&#x27;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="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>, we can see that it is
coloured red and its black height is 1, because it is a leaf.</p>
<p>Let&#x27;s look at its parent (node with <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>). On its left side it has
<code>nil</code> and on its right side the <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>. And its black height is still <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>, 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&#x27;s take a look at the root with <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>. 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&#x27;s refresh our memory with the algorithm of <em>insert fixup</em>:</p>
<div class="codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">WHILE z.p.color == Red</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> IF z.p == z.p.p.left</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> y = z.p.p.right</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> IF y.color == Red</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z.p.color = Black</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> y.color = Black</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z.p.p.color = Red</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z = z.p.p</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> ELSE</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> IF z == z.p.right</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z = z.p</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> Left-Rotate(T, z)</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z.p.color = Black</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> z.p.p.color = Red</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> Right-Rotate(T, z.p.p)</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> ELSE (same as above with “right” and “left” exchanged)</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><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 admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><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_BuS1"><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&#x27;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 style="text-align:center">Usual algorithm with black root</th><th style="text-align:center">Allowing red root</th></tr></thead><tbody><tr><td style="text-align:center"><img loading="lazy" alt="1ª insertion" src="data:image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Pgo8ZyBpZD0iZ3JhcGgwIiBjbGFzcz0iZ3JhcGgiIHRyYW5zZm9ybT0ic2NhbGUoMSAxKSByb3RhdGUoMCkgdHJhbnNsYXRlKDQgMTEyKSI+Cjx0aXRsZT5SQlRyZWU8L3RpdGxlPgo8cG9seWdvbiBmaWxsPSIjMWIxYjFkIiBzdHJva2U9InRyYW5zcGFyZW50IiBwb2ludHM9Ii00LDQgLTQsLTExMiAxMzAsLTExMiAxMzAsNCAtNCw0Ii8+CjwhLS0gMTQwMzk5ODA1NjAzMjgwIC0tPgo8ZyBpZD0ibm9kZTEiIGNsYXNzPSJub2RlIj4KPHRpdGxlPjE0MDM5OTgwNTYwMzI4MDwvdGl0bGU+CjxlbGxpcHNlIGZpbGw9Im5vbmUiIHN0cm9rZT0id2hpdGUiIGN4PSI2MyIgY3k9Ii05MCIgcng9IjI3IiByeT0iMTgiLz4KPHRleHQgdGV4dC1hbmNob3I9Im1pZGRsZSIgeD0iNjMiIHk9Ii04Ni4zIiBmb250LWZhbWlseT0iJ0Nhc2NhZGlhIENvZGUgUEwnLCAnSmV0QnJhaW5zIE1vbm8nLCAnSW9zZXZrYScsICdGaXJhIENvZGUnLCAnSGFjaycsIG1vbm9zcGFj
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="3ª-every-leaf-nil-is-black">3ª Every leaf (<code>nil</code>) is black.<a href="#3ª-every-leaf-nil-is-black" class="hash-link" aria-label="Direct link to 3ª-every-leaf-nil-is-black" title="Direct link to 3ª-every-leaf-nil-is-black"></a></h2>
<p>Now, this rule is a funny one. What does this imply and can I interpret this in
some other way? Let&#x27;s 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-9889570d993cf4a78a1bcccfbd76eab4.svg#gh-light-mode-only" width="899" height="539" class="img_ev3q">
<img loading="lazy" src="/assets/images/rb_dark-2917b0f8de62597646b619102f126a53.svg#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&#x27;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 style="text-align:right">path</th><th style="text-align:right">black nodes</th><th style="text-align:right">1ª idea</th><th style="text-align:right">2ª idea</th><th style="text-align:right">3ª idea</th></tr></thead><tbody><tr><td style="text-align:right"><code>3 → 1 → 0 → nil</code></td><td style="text-align:right">4</td><td style="text-align:right">3</td><td style="text-align:right">4</td><td style="text-align:right">3</td></tr><tr><td style="text-align:right"><code>3 → 5 → 7 → 8 → nil</code></td><td style="text-align:right">4</td><td style="text-align:right">3</td><td style="text-align:right">-</td><td style="text-align:right">3</td></tr><tr><td style="text-align:right"><code>3 → 5 → 7 → 8 → 9 → nil</code></td><td style="text-align:right">4</td><td style="text-align:right">3</td><td style="text-align:right">4</td><td style="text-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="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>
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 admonition_xJq3 alert alert--warning"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><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_BuS1"><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&#x27;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&#x27;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&#x27;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&#x27;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="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIKICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPgo8IS0tIEdlbmVyYXRlZCBieSBncmFwaHZpeiB2ZXJzaW9uIDIuNDMuMCAoMCkKIC0tPgo8IS0tIFRpdGxlOiAlMyBQYWdlczogMSAtLT4KPHN2ZyB3aWR0aD0iNTY2cHQiIGhlaWdodD0iMjYwcHQiCiB2aWV3Qm94PSIwLjAwIDAuMDAgNTY2LjAwIDI2MC4wMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CjxnIGlkPSJncmFwaDAiIGNsYXNzPSJncmFwaCIgdHJhbnNmb3JtPSJzY2FsZSgxIDEpIHJvdGF0ZSgwKSB0cmFuc2xhdGUoNCAyNTYpIj4KPHRpdGxlPiUzPC90aXRsZT4KPHBvbHlnb24gZmlsbD0id2hpdGUiIHN0cm9rZT0idHJhbnNwYXJlbnQiIHBvaW50cz0iLTQsNCAtNCwtMjU2IDU2MiwtMjU2IDU2Miw0IC00LDQiLz4KPCEtLSBOb2RlKHZhbHVlPTMsIHJhbms9MikgLS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IHk9Ii04Ni4zIiBmb250LWZhbWlseT0iJ0Nhc2NhZGlhIENvZGUgUEwnLCAnSmV0QnJhaW5zIE1vbm8nLCAnSW9zZXZrYScsICdGaXJhIENvZGUnLCAnSGFjaycsIG1vbm9zcGFjZSIgZm9udC1zaXplPSIxNC4wMCIgZmlsbD0id2hpdGUiPjA8L3RleHQ+CjwvZz4KPCEtLSBOb2RlKHZhbHVlPTEsIHJhbms9MSkmIzQ1OyZndDtOb2RlKHZhbHVlPTAsIHJhbms9MCkgLS0+CjxnIGlkPSJlZGdlMiIgY2xhc3M9ImVkZ2UiPgo8dGl0bGU+Tm9kZSh2YWx1ZT0xLCByYW5rPTEpJiM0NTsmZ3Q7Tm9kZSh2YWx1ZT0wLCByYW5rPTApPC90aXRsZT4KPHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSJ3aGl0ZSIgZD0iTTE4OC4xOSwtMTQ4LjgxQzE3MSwtMTM3LjY3IDE0NS4zOCwtMTIxLjA2IDEyNi4wMSwtMTA4LjUiLz4KPHBvbHlnb24gZmlsbD0idHJhbnNwYXJlbnQiIHN0cm9rZT0id2hpdGUiIHBvaW50cz0iMTI3
<img loading="lazy" src="/assets/images/incorrect_dark-d9c04aed74f7d364c3c3b1855b769ab0.svg#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 admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><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_BuS1"><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&#x27;t “hack” this requirement.</li>
<li>This rule ensures that we have “similar”<sup><a href="#user-content-fn-2" id="user-content-fnref-2" data-footnote-ref="true" aria-describedby="footnote-label">2</a></sup> length to each of the leaves.</li>
</ol>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><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_BuS1"><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>
<section data-footnotes="true" class="footnotes"><h2 class="anchor anchorWithStickyNavbar_LWe7 sr-only" id="footnote-label">Footnotes<a href="#footnote-label" class="hash-link" aria-label="Direct link to Footnotes" title="Direct link to Footnotes"></a></h2>
<ol>
<li id="user-content-fn-1">
<p>CORMEN, Thomas. Introduction to algorithms. Cambridge, Mass: MIT Press, 2009. isbn 9780262033848. <a href="#user-content-fnref-1" data-footnote-backref="" aria-label="Back to reference 1" class="data-footnote-backref"></a></p>
</li>
<li id="user-content-fn-2">
<p>red nodes still exist <a href="#user-content-fnref-2" data-footnote-backref="" aria-label="Back to reference 2" class="data-footnote-backref"></a></p>
</li>
</ol>
</section></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="/algorithms/tags/red-black-trees/">red-black trees</a></li><li class="tag_QGVx"><a class="tag_zVej tagRegular_sFm0" href="/algorithms/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://github.com/mfocko/blog/tree/main/algorithms/08-rb-trees/2023-06-10-rules.md" target="_blank" rel="noopener noreferrer" 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="/algorithms/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="/algorithms/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></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="noopener noreferrer" class="footer__link-item">GitLab<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__
</body>
</html>