<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prajwal Samshi on Medium]]></title>
        <description><![CDATA[Stories by Prajwal Samshi on Medium]]></description>
        <link>https://medium.com/@prajwalspdvg?source=rss-05198c7843fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ogTwJTfey4-xSzTrGvr6QQ.jpeg</url>
            <title>Stories by Prajwal Samshi on Medium</title>
            <link>https://medium.com/@prajwalspdvg?source=rss-05198c7843fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:05:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prajwalspdvg/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Red-Black Tree Step by Step: A Practical Guide to Balanced Trees]]></title>
            <link>https://medium.com/@prajwalspdvg/building-a-red-black-tree-step-by-step-a-practical-guide-to-balanced-trees-1ce77f69ff79?source=rss-05198c7843fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ce77f69ff79</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[red-black-tree]]></category>
            <dc:creator><![CDATA[Prajwal Samshi]]></dc:creator>
            <pubDate>Sat, 04 Oct 2025 14:16:23 GMT</pubDate>
            <atom:updated>2026-01-05T09:06:41.156Z</atom:updated>
            <content:encoded><![CDATA[<h4>Step-by-step visualization of Red-Black Tree balancing.</h4><p>In my <a href="https://medium.com/@prajwalspdvg/understanding-red-black-trees-a-balanced-binary-search-tree-explained-da05065792b8">previous article</a>, I walked through the fundamentals of Red-Black Trees — their properties, operations, and why they matter.</p><p>Now, let’s take things further. Instead of just reading about the rules, we’ll actually build a Red-Black Tree step by step. This will help you understand not just the rules, but also how rotations and recoloring actually keep the tree balanced.</p><p>For our example, we’ll insert the following sequence of elements into the tree:<br>10, 18, 7, 15, 16, 30, 25, 40.</p><p>We’ll start with an empty tree and build it node by node, applying the Red-Black Tree rules along the way.</p><p>The tree is initially empty, so the first node is added as the root and is colored black</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/126/1*__EuzaBUE1UNqEGAhAHnfA.png" /></figure><p>Now the tree is not empty, so we create a new node and add the next integer with the color red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*r1ohxQXdwOe5P1GZha37vw.png" /></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*vkSzT-IlwDqY-4jCBrRaUQ.png" /></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*rvkVFvlKnrViTvSXh2cg0g.png" /></figure><p>The tree right now violates both the binary search tree and Red-Black Tree properties.</p><p>Since the parent and its sibling are both red, recolor the parent and the sibling to black.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*pvn_TpY-JMBb6QpmA4W3Bg.png" /></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*PG7RzbZNjaJhRUS_PzsIzg.png" /></figure><p>The tree violates both the binary search tree and Red-Black Tree properties.</p><p>Since the parent’s sibling is null, we apply suitable rotations and recolor the nodes. First we will apply left rotation across node 15 and then apply right rotation across node 18 and will recolor the nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HkaX_2ucLnDphxX6eI3Agw.png" /><figcaption>Performing a left rotation around node 15 to correct the Red-Black Tree violation.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1y1Xy8KBF5i7SGuQnUMYXQ.png" /><figcaption>Applying a right rotation around node 18 to restore balance.</figcaption></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*32C2ASnA8a6D4wiwWSCOxw.png" /></figure><p>The tree right now violates both the binary search tree and Red-Black Tree properties. Since the parent and its sibling are both red, recolor the parent and the sibling to black, and recolor the grandparent to red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*m70q4Ag89zppZqx2f6P4sw.png" /><figcaption>Recoloring the parent and sibling to black, and the grandparent to red.</figcaption></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*qKAAl6ir3P-TOe4w5oI2Ug.png" /></figure><p>The tree violates both the binary search tree and Red-Black Tree properties.</p><p>Since the parent’s sibling is null, we apply suitable rotations and recolor the nodes. First we will apply right rotation across node 30 and then apply left rotation across node 18 and will recolor the nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iJeb_TikgTALKKYzhWfwfg.png" /><figcaption>Performing a right rotation around node 30 to correct the Red-Black Tree violation.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fKpoF0TyqNTKaZO34sEg8w.png" /><figcaption>Applying a left rotation around node 18 to restore balance.</figcaption></figure><p>The tree does not violate any Binary Search Tree or Red-Black Tree properties.</p><p>Next, we insert the element as a red node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*8eQ5ZCjkfi1yAvLxtRlgdA.png" /></figure><p>The tree violates both the binary search tree and Red-Black Tree properties. Since the parent and its sibling are both red, recolor the parent and the sibling to black, and recolor the grandparent to red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hiAUpcJz9qYQRAuBf4yKZw.png" /><figcaption>Recoloring the parent and sibling to black, and the grandparent to red.</figcaption></figure><p>The tree violates both the binary search tree and Red-Black Tree properties.</p><p>Here, the Red-Black Tree property is violated at node 25. Since its parent has a sibling, we check the color of the sibling. As the parent’s sibling is black, we apply rotations and recolor the nodes.</p><p>Apply left rotation across node 16 and recolor the nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*oQ4W86O5mrC_3tUhSgncOQ.png" /></figure><p>By inserting nodes step by step and applying recoloring and rotations when necessary, we ensure that the Red-Black Tree always remains balanced. This controlled balancing is what guarantees logarithmic performance for search, insertion, and deletion operations, even in the worst case</p><p><a href="https://www.cs.usfca.edu/~galles/visualization/RedBlack.html">Click here</a> to explore an interactive visualization of Red-Black Tree construction.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ce77f69ff79" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Red-Black Trees: A Balanced Binary Search Tree Explained]]></title>
            <link>https://medium.com/@prajwalspdvg/understanding-red-black-trees-a-balanced-binary-search-tree-explained-da05065792b8?source=rss-05198c7843fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/da05065792b8</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[binary-search-tree]]></category>
            <dc:creator><![CDATA[Prajwal Samshi]]></dc:creator>
            <pubDate>Sun, 28 Sep 2025 13:18:17 GMT</pubDate>
            <atom:updated>2025-10-04T14:41:07.418Z</atom:updated>
            <content:encoded><![CDATA[<h4>A step-by-step guide to understanding the properties, operations, and mathematical analysis of red-black trees.</h4><p>A Red-Black Tree is a type of self-balancing binary search tree where each node is colored either red or black.</p><p>It is a self-balancing binary search tree that makes use of these colors to maintain the balance factor during the insertion and deletion operation</p><p>The structure of red black tree is as follows:</p><pre>typedef struct tr_n_t {key_t key;<br>  struct tr_n_t *left;<br>  struct tr_n_t *right;<br>  enum {red, black} color;<br> /* possibly other information */<br> } tree_node_t;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wUqmvmSHFF9MkgJbJUjTbg.png" /><figcaption>Red-Black Tree with Node Colors</figcaption></figure><p>The following conditions will apply while assigning the colors to the node.</p><p>· The root node is always black in color.<br>· No two adjacent nodes must be red in color.<br>· Every path in the tree (from the root node to the leaf node) must have the same amount of black coloured nodes.</p><h4><strong>Basic Operations of Red-Black Trees</strong></h4><p>The operations on Red-Black Trees include all the basic operations usually performed on a Binary Search Tree. Some of the basic operations of an RB Tree include:</p><p>· Insertion<br>· Deletion<br>· Search</p><h4><strong>Insertion operation</strong></h4><p>Insertion operation of a Red-Black tree follows the same insertion algorithm of a binary search tree. The elements are inserted following the binary search property and as an addition, the nodes are color coded as red and black to balance the tree according to the red-black tree properties.</p><p>Following are the steps to insert an element into a red-black tree while maintaining both binary search tree and red-black tree properties.</p><p><strong>Case 1: </strong>Check whether the tree is empty; make the current node as the root and color the node black if it is empty.</p><p><strong>Case 2: </strong>But if the tree is not empty, we create a new node and color it red. Here we face two different cases:</p><ul><li>If the parent of the new node is a black colored node, we exit the operation and tree is left as it is.</li><li>If the parent of this new node is red and the color of the parent’s sibling is either black or if it does not exist, we apply a suitable rotation and recolor accordingly.</li><li>If the parent of this new node is red and color of the parent’s sibling is red, recolor the parent, the sibling and grandparent nodes to black. The grandparent is recolored only if it is <strong>not</strong> the root node; if it is the root node recolor only the parent and the sibling.</li></ul><h4>Example</h4><p>Let us construct an RB tree for the following element:<br>1, 2, 3, 4, 5, 6, 7</p><p>The tree is checked to be empty so the first node added is a root and is colored black.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/151/1*NaQLiqT8Ybe4HmCpLK1eAA.png" /></figure><p>Now, the tree is not empty so we create a new node and add the next integer with color red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*Yqx5jwrLd6Vm5VeQmdqc_A.png" /></figure><p>The nodes do not violate the binary search tree and RB tree properties; hence we move ahead to add another node.</p><p>The tree is not empty; we create a new red node with the next integer to it. But the parent of the new node is not a black-colored node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*zs5BeWgTJLWG3IBiytYTYA.png" /></figure><p>The tree right now violates both the binary search tree and RB tree properties; since parent’s sibling is NULL, we apply a suitable rotation and recolor the nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*74N0-K_Z9TiFOF2GvRUUUQ.png" /></figure><p>Now that the RB Tree property is restored, we add another node to the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*DxzIPgr25OQs0wFsdxTHxw.png" /></figure><p>The tree once again violates the RB Tree balance property, so we check for the parent’s sibling node color, red in this case, so we just recolor the parent and the sibling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*eFp8bvGq1sUk_iOOBe6jzw.png" /></figure><p>We next insert the element 5, which makes the tree violate the RB Tree balance property once again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*p0h8Mhzji8HjPQ1tWV2bzg.png" /></figure><p>And since the sibling is NULL, we apply a suitable rotation and recolor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ccsg14gOGp_wz9UjC8fg2Q.png" /></figure><p>Now, we insert element 6, but the RB Tree property is violated and one of the insertion cases need to be applied.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*ce73XpuHn-0yUFvulKcoRQ.png" /></figure><p>The parent’s sibling is red, so we recolor the parent, parent’s sibling and the grandparent nodes since the grandparent is not the root node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*2kPg6BUf8xim9dq-dQ0enw.png" /></figure><p>Now, we add the last element, 7, but the parent node of this new node is red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*dEUtkRBC4yYPqhmMGFGvkQ.png" /></figure><p>Since the parent’s sibling is NULL, we apply a suitable rotations (RR rotation)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*NZQKd8dJS6t28Y298yogDQ.png" /></figure><p>The final RB Tree is achieved.</p><h4>Deletion operation</h4><p>The deletion operation on red black tree must be performed in such a way that it must restore all the properties of a binary search tree and a red black tree.</p><p>Following the steps to perform the deletion operation on the red-black tree. we will perform deletion based on the binary search tree properties.</p><p><strong>Case 1: </strong>If either the node to be deleted or the node’s parent is red, just delete it.</p><p><strong>Case 2: </strong>If the node is a double black, just remove the double black (double black occurs when the node to be deleted is a black colored leaf node, as it adds up the NULL nodes which are considered black colored nodes too)</p><p><strong>Case 3: </strong>If the double black’s sibling node is also a black node and its child nodes are also black in color, follow the steps below:</p><ul><li>Remove double black</li><li>Recolor its parent to black (if the parent is a red node, it becomes black; if the parent is already a black node, it becomes double black)</li><li>Recolor the parent’s sibling with red</li><li>If double black node still exists, we apply other cases.</li></ul><p><strong>Case 4: </strong>If the double black node’s sibling is red, we perform the following steps:</p><ul><li>Swap the colors of the parent node and the parent’s sibling node.</li><li>Rotate parent node in the double black’s direction.</li><li>Reapply other cases that are suitable.</li></ul><p><strong>Case 5: </strong>If the double black’s sibling is a black node but the sibling’s child node that is closest to the double black is red, follow the steps below:</p><ul><li>Swap the colors of double black’s sibling and the sibling’s child in question</li><li>Rotate the sibling node is the opposite direction of double black (i.e. if the double black is a right child apply left rotations and vice versa)</li><li>Apply case 6.</li></ul><p><strong>Case 6: </strong>If the double black’s sibling is a black node but the sibling’s child node that is farther to the double black is red, follow the steps below:</p><ul><li>Swap the colors of double black’s parent and sibling nodes</li><li>Rotate the parent in double black’s direction (i.e. if the double black is a right child apply right rotations and vice versa)</li><li>Remove double black</li><li>Change the color of red child node to black.</li></ul><h4>Example</h4><p>Consider the below Red-Black tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*NZQKd8dJS6t28Y298yogDQ.png" /></figure><p>Delete elements 4, 5, 3 from the tree.</p><p>To delete the element 4, let us perform the binary search deletion first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*u9LBgSBZTXiw7K0ZLKAdAQ.png" /></figure><p>After performing the binary search deletion, the RB Tree property is not disturbed, therefore the tree is left as it is.</p><p>Then, we delete the element 5 using the binary search deletion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*NhwZxjddS_eoLeefe0j4JA.png" /></figure><p>Both the RB properties are violated after performing the binary search deletion, i.e., all the paths in the tree do not hold the same number of black nodes; so we swap the colors to balance the tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*TDVvf5u6ViuL8rPiT1bYFw.png" /></figure><p>Then, we delete the node 3 from the tree obtained.</p><p>Applying binary search deletion, we delete node 3 normally as it is a leaf node. And we get a double node as 3 is a black colored node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*MM9ZQAw-SUo5sbzGOXG9vA.png" /></figure><p>We apply case 3 deletion as double black’s sibling node is black and its child nodes are also black. Here, we remove the double black, recolor the double black’s parent and sibling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*ybj2u8lh2tgrXnZIIvjKLg.png" /></figure><p>All the desired nodes are deleted and the RB Tree property is maintained.</p><h4>Search operation</h4><p>The search operation in red-black tree follows the same algorithm as that of a binary search tree. The tree is traversed and each node is compared with the key element to be searched; if found it returns a successful search. Otherwise, it returns an unsuccessful search.</p><h4>Complexity</h4><p>In a red-black tree, the insertion, deletion, and search operations each have a time complexity of O(log n), while the space complexity is O(n).</p><p>A red-black tree of height h has at least 2^{(h/2)+1} -1 leaves for h even and at least (3/2) * 2^{(h-1)/2} — 1 leaves for h odd.</p><p>Equivalently, the height h of a red-black tree with n nodes is bounded by<br>h ≤ 2 log2​ (n+1).</p><p>This bound is derived from the black-height property, which guarantees that every path from root to leaf contains the same number of black nodes. Consequently, red-black trees remain approximately balanced, with the maximum height being at most twice that of a perfectly balanced binary tree.</p><p><a href="https://medium.com/@prajwalspdvg/building-a-red-black-tree-step-by-step-a-practical-guide-to-balanced-trees-1ce77f69ff79">Click here</a> for an example of how to construct a red-black tree.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da05065792b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Weight-Balanced Trees]]></title>
            <link>https://medium.com/@prajwalspdvg/weight-balanced-trees-032f8cce23a1?source=rss-05198c7843fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/032f8cce23a1</guid>
            <category><![CDATA[binary-search-tree]]></category>
            <category><![CDATA[weight-balanced-trees]]></category>
            <category><![CDATA[balanced-tree]]></category>
            <dc:creator><![CDATA[Prajwal Samshi]]></dc:creator>
            <pubDate>Fri, 16 May 2025 14:08:11 GMT</pubDate>
            <atom:updated>2025-05-16T14:08:11.681Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Weight- Balanced Trees</strong></h3><p>The weight of a tree is the number of its leaves, so in a weight-balanced tree, the weight of the left and right subtrees in each node should be “balanced”.</p><p>An α-weight-balanced tree of height h ≥ 2 has at least (1/(1-α)^h leaves.</p><p>An α-weight-balanced tree with n leaves has height at most log(1/(1-α))n = ((log(1/(1−α))^−1)*(log n).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*Y3Ov6MZxR6H8Z8JsE-trXQ.png" /><figcaption>Weight-Balanced Tree with Node Weights</figcaption></figure><p>The structure of a node is as follows:</p><pre>typedef struct tr_n_t {key_t key;<br> struct tr_n_t *left;<br> struct tr_n_t *right;<br> int weight;<br> /* possibly other information */<br> } tree_node_t;</pre><p>The weight of a node *n is defined recursively by the following rules:</p><p>If *n is a leaf (n -&gt; left = NULL), then n -&gt; weight = 1.</p><p>Else n -&gt; weight is the sum of the weight of the left and right subtrees: <br>n -&gt; weight = n -&gt; left -&gt; weight + n -&gt; right -&gt; weight.</p><p>The node *n is α-weight-balanced if<br>n -&gt; left -&gt; weight ≥ αn -&gt; weight and n -&gt; right -&gt; weight ≥ αn -&gt; weight</p><p>Where α is a constant value and α value lies between (0,1)</p><p>If *n is the current node, then the following cases are used as a rebalancing algorithm.</p><p>Case 1: n -&gt; left -&gt; weight ≥ αn-&gt; weight and<br>n -&gt; right -&gt; weight ≥ αn -&gt; weight.</p><p>In this case, no rebalancing is necessary; we go up to the next node.</p><p>Case 2: n -&gt; right -&gt; weight &lt; αn -&gt; weight</p><p>Case 2.1: If n -&gt; left -&gt; left -&gt; weight &gt; (α +ε)n -&gt; weight.</p><p>Perform a right rotation around n, followed by recomputing the weight in n-&gt;right.</p><p>Case 2.2: Else perform a left rotation around n-&gt;left, followed by a right rotation around n, followed by recomputing the weight in n -&gt; left and <br>n -&gt; right.</p><p>Case 3: n -&gt; left -&gt; weight &lt; αn -&gt; weight</p><p>Case 3.1: If n -&gt; right-&gt; right -&gt; weight &gt; (α +ε)n -&gt; weight.</p><p>Perform a left rotation around n, followed by recomputing the weight in <br>n -&gt; left.</p><p>Case 3.2: Else perform a right rotation around n-&gt;right, followed by a left rotation around n, followed by recomputing the weight in n -&gt; left and <br>n -&gt; right.</p><p>Unlike height-balanced trees, where rebalancing can sometimes be done without traversing all the way to the root, weight-balanced trees require updating the entire path from the point of modification up to the root. This is because weight information, unlike height, is affected at every level along the path.</p><p>A weight-balanced tree supports find, insert, and delete operations with a time complexity of O(log n) and a space complexity of O(n).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=032f8cce23a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Height-Balanced Trees]]></title>
            <link>https://medium.com/@prajwalspdvg/height-balanced-trees-e85670587e54?source=rss-05198c7843fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e85670587e54</guid>
            <dc:creator><![CDATA[Prajwal Samshi]]></dc:creator>
            <pubDate>Sat, 03 May 2025 06:33:23 GMT</pubDate>
            <atom:updated>2025-05-03T06:33:23.091Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Balanced Search tree:</strong></h3><p>A balanced binary tree, also referred to as a height - balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differs by not more than 1.</p><h4>Height-Balanced Tree</h4><p>A tree is height-balanced if, in each interior node, the height of the right subtree and the height of the left subtree differ by at most 1.</p><p>The structure of a node is as follows:</p><pre>typedef struct tr_n_t {key_t key;<br> struct tr_n_t *left;<br> struct tr_n_t *right;<br> int height;<br> /*possibly other information */<br> }tree_node_t;</pre><p>The height of a node *n is defined recursively by the following rules:</p><p>if *n is a leaf (n -&gt; left = NULL), then n -&gt; height = 0,</p><p>else n -&gt; height is one larger than the maximum of the height of the left and right subtrees:</p><p>n -&gt; height = 1 + max (n -&gt; left -&gt; height, n -&gt; right -&gt; height).</p><p>The height information must be corrected whenever the tree changes and must be used to keep the tree height balanced.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*en5X144NkrZ_3tJfAg21ZA.png" /><figcaption>Height-Balanced Tree with Node Heights</figcaption></figure><p>The tree changes only by insert and delete operations, and by any such operation, the height can change only for the nodes that contain the changed leaf in their subtree, that is, only for the nodes on the path from the root to the changed leaf. At the leaf that was changed, or in the case of an insert, the two neighboring leaves, the height is 0.</p><p>When a leaf node is changed (or when a new leaf is inserted), the height at that point becomes 0. As we move upward toward the root, at each node, we can be sure of the following:</p><ul><li>The height information in both the left and right subtrees is already correct.</li><li>Both subtrees are already height-balanced — one because we fixed it in the previous step, and the other because it wasn’t affected by the change.</li><li>The difference in height between the two subtrees is at most 2. This happens because, before the update, the height difference was at most 1, and the update only changed the height by at most 1.</li></ul><p>At each step going upward, our job is to:</p><ul><li>Balance the current node if needed.</li><li>Update the node’s height correctly.</li><li>Then continue moving up toward the root.</li></ul><p>If *n is the current node, there are the following possibilities:</p><p>Case 1: |n -&gt; left -&gt; height − n -&gt; right -&gt; height | ≤ 1.</p><p>In this case, no rebalancing is necessary in this node.</p><p>If the height also did not change, then from this node upward nothing changed and finish the rebalancing; otherwise, we need to correct the height of *n and go up to the next node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*ng7AjKqqp1LYUkBxTT3uqA.png" /><figcaption>No need for rebalancing</figcaption></figure><p>Case 2: |n -&gt; left -&gt; height − n -&gt; right -&gt; height| = 2.</p><p>In this case, we need to rebalance *n and need to apply the rotations to restore the balance</p><p>Case 2.1: If n -&gt;left -&gt; height = n -&gt; right -&gt; height + 2 and <br>n -&gt; left -&gt; left -&gt; height = n -&gt; right -&gt; height + 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*FlGgXP4834FTXHXMc-enaA.png" /></figure><p>Perform right rotation around n that is if left subtree of n is taller and if its left child is even taller, followed by recomputing the height in n -&gt; right and n.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*MOkqAY96wIDneF_-bl_FSQ.png" /><figcaption>After Performing Right Rotation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nvNBHYsk54V47XrGq35G0Q.png" /><figcaption>Rebalancing a Node in a Height-Balanced Tree: Case 2.1</figcaption></figure><p>Case 2.2: If n -&gt; left -&gt; height = n -&gt; right -&gt; height + 2 and <br>n -&gt; left -&gt; left -&gt; height = n -&gt; right -&gt; height.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/255/1*qpuTSseGGI2zhiFCAiSAvw.png" /></figure><p>Perform left rotation around n -&gt; left, followed by a right rotation around n, followed by recomputing the height in n -&gt; right, n -&gt; left, and n.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*jZ4rTwlc7OqNNaBgkaQf_Q.png" /><figcaption>After Performing left Rotation and then Right Rotation</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*Z6ewOzcwP7hFJpmNiddt1A.png" /><figcaption>Rebalancing a Node in a Height-Balanced Tree: Case 2.2</figcaption></figure><p>Case 2.3: If n -&gt; right -&gt; height = n -&gt; left -&gt; height + 2 and <br>n -&gt; right -&gt; right -&gt; height = n -&gt; left -&gt; height + 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*YmeVx--3KQEkBOgJCDjAwA.png" /></figure><p>Perform left rotation around n, followed by recomputing the height in <br>n -&gt; left and n.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*TiUXu3dL5pQY3Tmue7Jaew.png" /><figcaption>After Performing Left Rotation</figcaption></figure><p>Case 2.4: If n -&gt; right -&gt; height = n -&gt; left -&gt; height + 2 and <br>n -&gt; right -&gt; right -&gt; height = n -&gt; left -&gt; height.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*KIjY1ZrQvBllTd38hwtG6w.png" /></figure><p>Perform right rotation around n -&gt; right, followed by a left rotation around n, followed by recomputing the height in n -&gt; right, n -&gt; left, and n.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*3m_4h60h1nRM1wxFUQM-VA.png" /><figcaption>After Performing Right Rotation and then Left Rotation</figcaption></figure><p>A height-balanced tree supports find, insert, and delete operations with a time complexity of O(log n) and a space complexity of O(n).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e85670587e54" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>