{"id":357,"date":"2018-07-19T06:17:07","date_gmt":"2018-07-19T06:17:07","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=357"},"modified":"2018-12-12T10:39:04","modified_gmt":"2018-12-12T10:39:04","slug":"red-black-trees-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/red-black-trees-i\/","title":{"rendered":"Red Black Trees -I"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/GIlwLCrU0QY\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of balanced binary search trees and discussed one such tree \u2013 the AVL tree. In this module we will discuss another balanced binary search tree \u2013 the Red Black tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022 To understand the concept of Red Black Trees\r\n\r\n\u2022 To discuss the properties of Red Black Trees\r\n\r\n\u2022 To describe rotation of Red Black Trees\r\n\r\n\u2022 To explain the Insertion into Red Black Trees\r\n\r\n&nbsp;\r\n\r\n<strong>25.1 Introduction to Red Black Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Red-black trees are a variation of binary search trees to ensure that the tree is <strong><em>balanced<\/em><\/strong>. In this case as in balanced trees the height is of the <em>O<\/em>(log <em>n<\/em>), where <em>n <\/em>is the number of nodes. Hence the operations associated with Red-black trees take <em>O<\/em>(log<em> n<\/em>) time in the worst case. However in the case of Red-black trees each node requires an extra bit when compared to binary search trees since each node is now associated with a bit indicating the color attribute which can be either <strong>red<\/strong> or <strong>black<\/strong>. The nodes of Red-black trees inherit all the other attributes associated with a node of the binary search tree such as key, and pointers to the left sub-tree, right sub-tree and the parent. Now in the case of Red-black trees all empty trees (leaves) are colored black. This is normally carried out by setting the single sentinel associated with color as <em>nil<\/em>, for all the leaves of red-black tree <em>T<\/em>, with <em>color<\/em>[<em>nil<\/em>] = black. Here we assume that the leaf nodes null nodes are set as nil. The parent of the root is also set to nil.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.2 Red-black Properties<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As we have already discussed the Red-black tree data structure requires an extra one-bit color field in each node. Every node is either red or black. Some of the properties associated with the Red-black tree are:<\/p>\r\n&nbsp;\r\n<ol>\r\n \t<li>Every node in a Red-black tree is either red or black<\/li>\r\n \t<li>The root is <strong style=\"font-size: 1em;text-align: justify\">black<\/strong><span style=\"font-size: 1em;text-align: justify\"> and every leaf (<\/span><em style=\"font-size: 1em;text-align: justify\">nil<\/em><span style=\"font-size: 1em;text-align: justify\">) is <\/span><strong style=\"font-size: 1em;text-align: justify\">black.<\/strong><span style=\"font-size: 1em;text-align: justify\"> We assume that every node that is not a leaf has 2 children (one or both of which may be nil nodes)<\/span><\/li>\r\n \t<li>If a node is <strong style=\"text-align: justify;font-size: 1em\">red<\/strong><span style=\"text-align: justify;font-size: 1em\">, then its parent is <\/span><strong style=\"text-align: justify;font-size: 1em\">black<\/strong><span style=\"text-align: justify;font-size: 1em\">. In other words, if a node is <\/span><strong style=\"text-align: justify;font-size: 1em\">red<\/strong><span style=\"text-align: justify;font-size: 1em\">, then both of its children are\u00a0<\/span><strong style=\"text-align: justify;font-size: 1em\">black.<\/strong><span style=\"text-align: justify;font-size: 1em\"> This means that Red-black trees do not allow 2 consecutive red nodes on a path.<\/span><\/li>\r\n \t<li>All simple paths from any node <em style=\"text-align: initial;font-size: 1em\">x<\/em><span style=\"text-align: initial;font-size: 1em\"> to a descendant leaf have the same number of black nodes = black-Height(<\/span><em style=\"text-align: initial;font-size: 1em\">x<\/em><span style=\"text-align: initial;font-size: 1em\">).<\/span><\/li>\r\n<\/ol>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>25.2.1 Red-black Tree \u2013 Example<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us understand the properties of Red-black trees using an example (Figure 25.1). For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs. NIL[T] has the same fields as an ordinary node. As we have discussed all leaf nodes are colored black.<\/p>\r\n&nbsp;\r\n\r\nColor[NIL[T]] = BLACK\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-360 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214.png\" alt=\"\" width=\"562\" height=\"338\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 25.2 shows the properties for a given Red-black tree. As you can see all nodes of the tree are colored as either red or black (property 1). The root node (<strong>10<\/strong>) and all leaf nodes are colored black (property 2). All the red nodes <strong>1, 5, 3, 25 and 60<\/strong> have black parents (property 3). In addition as per property 4 all these red nodes have both<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-361 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215.png\" alt=\"\" width=\"523\" height=\"279\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">children black and these red nodes cannot occur consequently on a path in the Red-black tree. Moreover the number of black nodes on a simple path from any black node to a descendent leaf node is the same (property 5). Example the number of black nodes from <strong>10<\/strong> to leaf node through any path <strong>(10-7-3-1-Nil, 10-7-8-Nil, 10-7-3-5-Nil) <\/strong>is 3 where the count does not include the node itself.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.2.3 Black-Height of a Node<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Based on property 5, we can also define the black-height of a node. In general the height of a nodeis defined as the number of edges in the longest path to a leaf.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to a leaf, without counting node x. In the example shown in Figure 25.3 the red node 50 has both height and black-height as 1 but 47 has height 2 and black height as 1 since there is only one black node (Nil) in the path from <strong>47 (47-50-Nil)<\/strong> to the leaf excluding <strong>47.<\/strong><\/p>\r\n<img class=\"alignnone size-full wp-image-362 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216.png\" alt=\"\" width=\"503\" height=\"314\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>25.3 Implementing Red-black Operations<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As in any data structure we can classify the operations associated with it as access operations and update operations. Access operations include searching for an element, finding the minimum or maximum element in the red-black tree, finding the predecessor, successor etc. Update operations include inserting elements into and deleting elements from the red-black tree. Remember that a red-black tree is basically a binary search tree (BST) and we can use all the BST algorithms without change by simply ignoring the colors associated with each node. The worst-case time complexity depends on h, the height of the tree O(h) = O(log n). Now after performing the BST operations, we have to ensure that the modified tree will still obey the red-black tree properties. In order to maintain the red-black tree properties we need to carry out some rotations of the nodes of the tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.4 Rotations <\/strong>and<strong> Red-Black-Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Rotations are the basic tree-restructuring operation for almost all <em>balanced<\/em> search trees for the purpose of rebalancing a balanced tree. These operations are necessary for re-structuring the red-black tree after insert and delete operations on red-black trees. The local operation in a search tree should ensure the binary search tree property.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Rotations take a red-black-tree and a node within the tree and together with some node re-coloring they help restore the red-black-tree property. These rotations change some of the connections between nodes and also ensure that the binary-search tree property is maintained. Rotation takes a red-black-tree and a node, appropriately changes pointers to change the local structure, and ensures that the binary-search-tree property is not violated. There are two types of rotations left rotation and right rotation which are inverse of each other. As is shown in Figure 25.4 during Left-Rotate(T,x), x is rotated to the left and now becomes the left sub-tree of y which now becomes the root. The left sub-tree \u03b2 of y becomes the new right sub-tree of x. Now the inverse of the left rotate is right rotate where Right-Rotate (T,y) right rotates about the node y. Rotate operations are used for restructuring the tree after insert and delete operations on red-black trees. These rotations are general rotations discussed in earlier modules.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-363 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217.png\" alt=\"\" width=\"550\" height=\"182\" \/>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 25.4 Left and Right Rotate<\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>25.4.1 Left Rotations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When performing left rotation on a node x we assume that the right child of x (y) is not NIL. The basic idea is to pivot around the link from x to y where y is the root of the right sub-tree of the node x. We make y the new root of the sub-tree and x becomes y\u2019s left child while y\u2019s left child becomes x\u2019s right child.<\/p>\r\n<img class=\"alignnone size-full wp-image-364 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218.png\" alt=\"\" width=\"435\" height=\"229\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>25.4.1.1<\/strong>\u00a0\u00a0\u00a0\u00a0 <strong>Example of Left Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 25.6 shows an example of left rotation of 12 with 9 as the pivot. Now 12 is rotated left and now becomes the left sub-tree of 9\u2019s parent 7. The left sub-tree of 12 rooted at 11 now becomes the right sub-tree of 9.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-365 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219.png\" alt=\"\" width=\"620\" height=\"239\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong> 25.4.2 Right Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In general, rotation of a node for re-balancing a balanced tree involves pointer manipulation. In the case of right rotation of node x about y where x is left sub-tree of y (Figure 25.5). We assume that for a right rotation on a node x, the left child of x of y is not NIL. When we pivot around the link from y to x, we make x the new root of the sub-tree and y becomes x\u2019s right child and x\u2019s right child becomes y\u2019s left child.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-366 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220.png\" alt=\"\" width=\"582\" height=\"323\" \/>\r\n<div>\r\n\r\n25.5 <strong>Modifying Operations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As is generally the case the operations INSERT and DELETE cause modifications to the red-black tree. After we carry out the operation, we need to change color and restructure the links of the tree via <strong><em>\u201crotations\u201d<\/em><\/strong> we have discussed in the previous section.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-367 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221.png\" alt=\"\" width=\"371\" height=\"274\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>25.6 Insertion \u2013 Red-Black Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We need to insert a new node z into a red-black-tree. The first step is the insertion of the node z into the tree as is done for an ordinary binary search tree. We color the new node as red and then go on to restore the red-black-tree properties. Now let us consider how to color the new node. Let us consider the example given in Figure 25.9. Let us assume that we have inserted 35 and colored it red . We see that property 4 is violated. As we know property 4 states that If a node is <strong>red<\/strong>, then both of its children should be <strong>black<\/strong> and Red-black trees do not allow 2 consecutive red nodes on a path \u2013 38 and 35 two consecutive red nodes. Let us assume that we have inserted 14 and colored it black, in this case property 5 which states that all\u00a0<span style=\"font-size: 1em;text-align: initial\">paths from a node to its leaves contain the same number of black nodes is violated.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nHence we need to carry out modifications to restore the red-black tree property.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-368 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222.png\" alt=\"\" width=\"389\" height=\"289\" \/>\r\n\r\n<strong>25.6.1 Insertion in Detail<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first step of insertion is creation of a new node with the data element and insertion of the new node into the Red-black tree using the binary search tree insertion module. Next the new node needs to be assigned a color. If we make the node black there may be the case of one root-to-external-node path has an extra black node (black pointer). This particular type of problem is hard to rectify. We could make the node red there may be the case of one root-to-external-node path may have two consecutive red nodes (pointers). This problem can be remedied by color flips and\/or a rotation. In case there is a violation of property, we move this violation of property 3 up the tree, by recoloring until it can be fixed with rotations and recoloring.<\/p>\r\n&nbsp;\r\n\r\n<strong>25.6.2 Red-Black Trees: The Problem with Insertion<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us assume that we are going to insert 8 (Figure 25.10), the place where it is inserted is decided by the binary search property and let us color it red.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-369 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223.png\" alt=\"\" width=\"607\" height=\"173\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us assume that now we want to insert 11, it cannot be colored red (violation of property 3) and cannot be colored black (violation of property 4). The solution is to rotate and recolor the node black as shown in Figure 25.11. In the following section we will discuss the different methods needed for rebalancing.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-370 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224.png\" alt=\"\" width=\"499\" height=\"310\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>25.7 Red-Black Tree \u2013 Rebalancing after Insertion<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now in order to explain the cases of rebalancing we will define the concept of uncle of a node x is the sibling of the parent of x. Now there are 3 cases where rebalancing will be carried out<\/p>\r\n&nbsp;\r\n\r\n<strong>25.7.1 Case 1<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Conditions for case 1 (Figure 25.12 (a))<\/strong>\r\n<ul>\r\n \t<li>Now the let check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013 (D) is red.<\/li>\r\n \t<li>We check whether z is a right child and<\/li>\r\n \t<li style=\"text-align: justify\">Whether z\u2019s grandparent that p[p[z]] is (here c) black where p[z] is the parent of z. This will be the case when p[z] is red.<\/li>\r\n \t<li>We also check if both z and its parent p[z] (here B and A) are red<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nOnly if all these conditions are true, do we carry out the operations associated with Case1.\r\n\r\n&nbsp;\r\n\r\n<strong>Steps for Case 1<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the \u201cred\u201d violation up the tree. The same action will be taken whether z is a left or a right child. The following steps need to be carry out the following steps (Figure 25.12 (b) &amp; (c)):<\/p>\r\n&nbsp;\r\n<ul>\r\n \t<li>We color p[z] black,<\/li>\r\n \t<li>We also color the uncle y of node z as black<\/li>\r\n \t<li>We then color the grandparent of z that is p[p[z]] red<\/li>\r\n \t<li>We then set new z = p[p[z]]<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-371 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225.png\" alt=\"\" width=\"377\" height=\"188\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-372 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226.png\" alt=\"\" width=\"615\" height=\"183\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 Now we will discuss Case 3 before going onto to Case 2\r\n\r\n&nbsp;\r\n\r\n<strong>25.7.2 Case 3<\/strong>\r\n\r\n<strong>Conditions for case 3 (Figure 25.13)<\/strong>\r\n<ul>\r\n \t<li>Now we check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013is black and<\/li>\r\n \t<li>We check whether z is a left child of B<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Steps for Case 3<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the p[z] up the tree. The action will be taken when z is a left child. The following steps need to be carried out (Figure 25.13):<\/p>\r\n\r\n<ul>\r\n \t<li>We color p[z] black,<\/li>\r\n \t<li>We then color the grandparent of z that is p[p[z]] (C) red<\/li>\r\n \t<li>We then RIGHT-ROTATE(T, p[p[z]] (C))<\/li>\r\n \t<li>Now p[z] (B) is black<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">No longer are 2 reds in a row. We have performed a right rotation that preserves property 4 that is all downward paths contain same number of black nodes<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-373 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227.png\" alt=\"\" width=\"596\" height=\"181\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>25.7.2 Case 2<\/strong>\r\n\r\n<strong>Conditions for case 2 (Figure 25.14)<\/strong>\r\n<ul>\r\n \t<li>Now the let check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013is black and<\/li>\r\n \t<li>We then check whether z is a right child<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Steps for Case 2<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the p[z] up the tree. The following action will be taken when z is a right child. The following steps need to be carry out the following steps (Figure 25.13):<\/p>\r\n\r\n<ul>\r\n \t<li>We make z to be the parent of z (p[z]) that is z is now A.<\/li>\r\n \t<li>We then LEFT-ROTATE(T, B) about A- Now z is a left child<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Now both z (A) and p[z] (B) are red and we have a case 3 which we rebalance using the steps outlined in Section 25.7.2.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-374 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228.png\" alt=\"\" width=\"575\" height=\"234\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>25.7 An Example showing all Three Cases<\/strong>\r\n\r\n&nbsp;\r\n\r\nFigure 25.15 shows an example for the insertion of 4 into the Red-black tree.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">4 is a right child. Here both 4 and it\u2019s parent 5 are red and the uncle of 4 (8) is also red. Moreover 4\u2019s grandparent is black. All these conditions gives rise to <strong>Case 1<\/strong> <strong>rebalancing<\/strong>. Accordingly we color the parent of 4 as well as it\u2019s uncle 8 as black. We then color the grandparent of 4 that is 7 as red and make 7 as the node which leads to rebalancing.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now in the rebalanced tree the node 7 and it\u2019s parent 2 are red, 7 is a right child and 7\u2019s uncle is black, all conditions that lead to a <strong>Case 2 rebalancing<\/strong>. Accordingly we make the node for rebalancing to be 7\u2019s parent 2, and then carry out a LEFT-ROTATE of the tree with 2 as the pivot.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now 2 and it\u2019s parent 7 are red, 2 is a left child and 2\u2019s uncle 14 is black. All these conditions gives rise to <strong>Case 3 rebalancing<\/strong>. Accordingly we color parent of 2 that is 7 black, and it\u2019s grandparent 11 red. We then RIGHT-ROTATE the tree with 11 as the pivot. Now we end up with the parent of 2 being black. Thus the insertion of 4 lead to three cases of rebalancing, case 1 with 4 as the main node, case 2 with 7 as the main node and finally case 3 with 2 as the main node. Finally we have a binary search tree that satisfies all the properties of the Red-Black tree.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-375 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229.png\" alt=\"\" width=\"575\" height=\"313\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the concept of Red Black Trees<\/li>\r\n \t<li>Discussed the properties of Red Black Trees<\/li>\r\n \t<li>Described rotation of Red Black Trees<\/li>\r\n \t<li>Explained the Insertion into Red Black Trees<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Red Black Trees -I<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/GIlwLCrU0QY\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"size-full wp-image-376 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230.png\" alt=\"\" width=\"634\" height=\"525\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/GIlwLCrU0QY\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of balanced binary search trees and discussed one such tree \u2013 the AVL tree. In this module we will discuss another balanced binary search tree \u2013 the Red Black tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To understand the concept of Red Black Trees<\/p>\n<p>\u2022 To discuss the properties of Red Black Trees<\/p>\n<p>\u2022 To describe rotation of Red Black Trees<\/p>\n<p>\u2022 To explain the Insertion into Red Black Trees<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.1 Introduction to Red Black Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Red-black trees are a variation of binary search trees to ensure that the tree is <strong><em>balanced<\/em><\/strong>. In this case as in balanced trees the height is of the <em>O<\/em>(log <em>n<\/em>), where <em>n <\/em>is the number of nodes. Hence the operations associated with Red-black trees take <em>O<\/em>(log<em> n<\/em>) time in the worst case. However in the case of Red-black trees each node requires an extra bit when compared to binary search trees since each node is now associated with a bit indicating the color attribute which can be either <strong>red<\/strong> or <strong>black<\/strong>. The nodes of Red-black trees inherit all the other attributes associated with a node of the binary search tree such as key, and pointers to the left sub-tree, right sub-tree and the parent. Now in the case of Red-black trees all empty trees (leaves) are colored black. This is normally carried out by setting the single sentinel associated with color as <em>nil<\/em>, for all the leaves of red-black tree <em>T<\/em>, with <em>color<\/em>[<em>nil<\/em>] = black. Here we assume that the leaf nodes null nodes are set as nil. The parent of the root is also set to nil.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.2 Red-black Properties<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As we have already discussed the Red-black tree data structure requires an extra one-bit color field in each node. Every node is either red or black. Some of the properties associated with the Red-black tree are:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>Every node in a Red-black tree is either red or black<\/li>\n<li>The root is <strong style=\"font-size: 1em;text-align: justify\">black<\/strong><span style=\"font-size: 1em;text-align: justify\"> and every leaf (<\/span><em style=\"font-size: 1em;text-align: justify\">nil<\/em><span style=\"font-size: 1em;text-align: justify\">) is <\/span><strong style=\"font-size: 1em;text-align: justify\">black.<\/strong><span style=\"font-size: 1em;text-align: justify\"> We assume that every node that is not a leaf has 2 children (one or both of which may be nil nodes)<\/span><\/li>\n<li>If a node is <strong style=\"text-align: justify;font-size: 1em\">red<\/strong><span style=\"text-align: justify;font-size: 1em\">, then its parent is <\/span><strong style=\"text-align: justify;font-size: 1em\">black<\/strong><span style=\"text-align: justify;font-size: 1em\">. In other words, if a node is <\/span><strong style=\"text-align: justify;font-size: 1em\">red<\/strong><span style=\"text-align: justify;font-size: 1em\">, then both of its children are\u00a0<\/span><strong style=\"text-align: justify;font-size: 1em\">black.<\/strong><span style=\"text-align: justify;font-size: 1em\"> This means that Red-black trees do not allow 2 consecutive red nodes on a path.<\/span><\/li>\n<li>All simple paths from any node <em style=\"text-align: initial;font-size: 1em\">x<\/em><span style=\"text-align: initial;font-size: 1em\"> to a descendant leaf have the same number of black nodes = black-Height(<\/span><em style=\"text-align: initial;font-size: 1em\">x<\/em><span style=\"text-align: initial;font-size: 1em\">).<\/span><\/li>\n<\/ol>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>25.2.1 Red-black Tree \u2013 Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us understand the properties of Red-black trees using an example (Figure 25.1). For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs. NIL[T] has the same fields as an ordinary node. As we have discussed all leaf nodes are colored black.<\/p>\n<p>&nbsp;<\/p>\n<p>Color[NIL[T]] = BLACK<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-360 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214.png\" alt=\"\" width=\"562\" height=\"338\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214.png 562w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214-300x180.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-214-350x210.png 350w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 25.2 shows the properties for a given Red-black tree. As you can see all nodes of the tree are colored as either red or black (property 1). The root node (<strong>10<\/strong>) and all leaf nodes are colored black (property 2). All the red nodes <strong>1, 5, 3, 25 and 60<\/strong> have black parents (property 3). In addition as per property 4 all these red nodes have both<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-361 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215.png\" alt=\"\" width=\"523\" height=\"279\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215.png 523w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215-300x160.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215-225x120.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-215-350x187.png 350w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">children black and these red nodes cannot occur consequently on a path in the Red-black tree. Moreover the number of black nodes on a simple path from any black node to a descendent leaf node is the same (property 5). Example the number of black nodes from <strong>10<\/strong> to leaf node through any path <strong>(10-7-3-1-Nil, 10-7-8-Nil, 10-7-3-5-Nil) <\/strong>is 3 where the count does not include the node itself.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.2.3 Black-Height of a Node<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Based on property 5, we can also define the black-height of a node. In general the height of a nodeis defined as the number of edges in the longest path to a leaf.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to a leaf, without counting node x. In the example shown in Figure 25.3 the red node 50 has both height and black-height as 1 but 47 has height 2 and black height as 1 since there is only one black node (Nil) in the path from <strong>47 (47-50-Nil)<\/strong> to the leaf excluding <strong>47.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-362 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216.png\" alt=\"\" width=\"503\" height=\"314\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216.png 503w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216-300x187.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216-225x140.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-216-350x218.png 350w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.3 Implementing Red-black Operations<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As in any data structure we can classify the operations associated with it as access operations and update operations. Access operations include searching for an element, finding the minimum or maximum element in the red-black tree, finding the predecessor, successor etc. Update operations include inserting elements into and deleting elements from the red-black tree. Remember that a red-black tree is basically a binary search tree (BST) and we can use all the BST algorithms without change by simply ignoring the colors associated with each node. The worst-case time complexity depends on h, the height of the tree O(h) = O(log n). Now after performing the BST operations, we have to ensure that the modified tree will still obey the red-black tree properties. In order to maintain the red-black tree properties we need to carry out some rotations of the nodes of the tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.4 Rotations <\/strong>and<strong> Red-Black-Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Rotations are the basic tree-restructuring operation for almost all <em>balanced<\/em> search trees for the purpose of rebalancing a balanced tree. These operations are necessary for re-structuring the red-black tree after insert and delete operations on red-black trees. The local operation in a search tree should ensure the binary search tree property.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Rotations take a red-black-tree and a node within the tree and together with some node re-coloring they help restore the red-black-tree property. These rotations change some of the connections between nodes and also ensure that the binary-search tree property is maintained. Rotation takes a red-black-tree and a node, appropriately changes pointers to change the local structure, and ensures that the binary-search-tree property is not violated. There are two types of rotations left rotation and right rotation which are inverse of each other. As is shown in Figure 25.4 during Left-Rotate(T,x), x is rotated to the left and now becomes the left sub-tree of y which now becomes the root. The left sub-tree \u03b2 of y becomes the new right sub-tree of x. Now the inverse of the left rotate is right rotate where Right-Rotate (T,y) right rotates about the node y. Rotate operations are used for restructuring the tree after insert and delete operations on red-black trees. These rotations are general rotations discussed in earlier modules.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-363 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217.png\" alt=\"\" width=\"550\" height=\"182\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217.png 550w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217-300x99.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217-225x74.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-217-350x116.png 350w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<div>\n<p style=\"text-align: center\"><strong>Figure 25.4 Left and Right Rotate<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.4.1 Left Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When performing left rotation on a node x we assume that the right child of x (y) is not NIL. The basic idea is to pivot around the link from x to y where y is the root of the right sub-tree of the node x. We make y the new root of the sub-tree and x becomes y\u2019s left child while y\u2019s left child becomes x\u2019s right child.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-364 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218.png\" alt=\"\" width=\"435\" height=\"229\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218.png 435w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218-300x158.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218-225x118.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-218-350x184.png 350w\" sizes=\"auto, (max-width: 435px) 100vw, 435px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.4.1.1<\/strong>\u00a0\u00a0\u00a0\u00a0 <strong>Example of Left Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 25.6 shows an example of left rotation of 12 with 9 as the pivot. Now 12 is rotated left and now becomes the left sub-tree of 9\u2019s parent 7. The left sub-tree of 12 rooted at 11 now becomes the right sub-tree of 9.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-365 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219.png\" alt=\"\" width=\"620\" height=\"239\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219.png 620w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219-300x116.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219-225x87.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-219-350x135.png 350w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong> 25.4.2 Right Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In general, rotation of a node for re-balancing a balanced tree involves pointer manipulation. In the case of right rotation of node x about y where x is left sub-tree of y (Figure 25.5). We assume that for a right rotation on a node x, the left child of x of y is not NIL. When we pivot around the link from y to x, we make x the new root of the sub-tree and y becomes x\u2019s right child and x\u2019s right child becomes y\u2019s left child.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-366 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220.png\" alt=\"\" width=\"582\" height=\"323\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220.png 582w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-220-350x194.png 350w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n<div>\n<p>25.5 <strong>Modifying Operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As is generally the case the operations INSERT and DELETE cause modifications to the red-black tree. After we carry out the operation, we need to change color and restructure the links of the tree via <strong><em>\u201crotations\u201d<\/em><\/strong> we have discussed in the previous section.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-367 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221.png\" alt=\"\" width=\"371\" height=\"274\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221.png 371w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221-300x222.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221-225x166.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-221-350x258.png 350w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.6 Insertion \u2013 Red-Black Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We need to insert a new node z into a red-black-tree. The first step is the insertion of the node z into the tree as is done for an ordinary binary search tree. We color the new node as red and then go on to restore the red-black-tree properties. Now let us consider how to color the new node. Let us consider the example given in Figure 25.9. Let us assume that we have inserted 35 and colored it red . We see that property 4 is violated. As we know property 4 states that If a node is <strong>red<\/strong>, then both of its children should be <strong>black<\/strong> and Red-black trees do not allow 2 consecutive red nodes on a path \u2013 38 and 35 two consecutive red nodes. Let us assume that we have inserted 14 and colored it black, in this case property 5 which states that all\u00a0<span style=\"font-size: 1em;text-align: initial\">paths from a node to its leaves contain the same number of black nodes is violated.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Hence we need to carry out modifications to restore the red-black tree property.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-368 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222.png\" alt=\"\" width=\"389\" height=\"289\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222.png 389w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222-300x223.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222-225x167.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-222-350x260.png 350w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/p>\n<p><strong>25.6.1 Insertion in Detail<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first step of insertion is creation of a new node with the data element and insertion of the new node into the Red-black tree using the binary search tree insertion module. Next the new node needs to be assigned a color. If we make the node black there may be the case of one root-to-external-node path has an extra black node (black pointer). This particular type of problem is hard to rectify. We could make the node red there may be the case of one root-to-external-node path may have two consecutive red nodes (pointers). This problem can be remedied by color flips and\/or a rotation. In case there is a violation of property, we move this violation of property 3 up the tree, by recoloring until it can be fixed with rotations and recoloring.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.6.2 Red-Black Trees: The Problem with Insertion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us assume that we are going to insert 8 (Figure 25.10), the place where it is inserted is decided by the binary search property and let us color it red.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-369 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223.png\" alt=\"\" width=\"607\" height=\"173\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223.png 607w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223-300x86.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223-225x64.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-223-350x100.png 350w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us assume that now we want to insert 11, it cannot be colored red (violation of property 3) and cannot be colored black (violation of property 4). The solution is to rotate and recolor the node black as shown in Figure 25.11. In the following section we will discuss the different methods needed for rebalancing.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-370 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224.png\" alt=\"\" width=\"499\" height=\"310\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224.png 499w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224-300x186.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224-225x140.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-224-350x217.png 350w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.7 Red-Black Tree \u2013 Rebalancing after Insertion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now in order to explain the cases of rebalancing we will define the concept of uncle of a node x is the sibling of the parent of x. Now there are 3 cases where rebalancing will be carried out<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.7.1 Case 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Conditions for case 1 (Figure 25.12 (a))<\/strong><\/p>\n<ul>\n<li>Now the let check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013 (D) is red.<\/li>\n<li>We check whether z is a right child and<\/li>\n<li style=\"text-align: justify\">Whether z\u2019s grandparent that p[p[z]] is (here c) black where p[z] is the parent of z. This will be the case when p[z] is red.<\/li>\n<li>We also check if both z and its parent p[z] (here B and A) are red<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Only if all these conditions are true, do we carry out the operations associated with Case1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Steps for Case 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the \u201cred\u201d violation up the tree. The same action will be taken whether z is a left or a right child. The following steps need to be carry out the following steps (Figure 25.12 (b) &amp; (c)):<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>We color p[z] black,<\/li>\n<li>We also color the uncle y of node z as black<\/li>\n<li>We then color the grandparent of z that is p[p[z]] red<\/li>\n<li>We then set new z = p[p[z]]<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-371 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225.png\" alt=\"\" width=\"377\" height=\"188\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225.png 377w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225-225x112.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-225-350x175.png 350w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-372 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226.png\" alt=\"\" width=\"615\" height=\"183\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226.png 615w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226-300x89.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226-225x67.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-226-350x104.png 350w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 Now we will discuss Case 3 before going onto to Case 2<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.7.2 Case 3<\/strong><\/p>\n<p><strong>Conditions for case 3 (Figure 25.13)<\/strong><\/p>\n<ul>\n<li>Now we check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013is black and<\/li>\n<li>We check whether z is a left child of B<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Steps for Case 3<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the p[z] up the tree. The action will be taken when z is a left child. The following steps need to be carried out (Figure 25.13):<\/p>\n<ul>\n<li>We color p[z] black,<\/li>\n<li>We then color the grandparent of z that is p[p[z]] (C) red<\/li>\n<li>We then RIGHT-ROTATE(T, p[p[z]] (C))<\/li>\n<li>Now p[z] (B) is black<\/li>\n<\/ul>\n<p style=\"text-align: justify\">No longer are 2 reds in a row. We have performed a right rotation that preserves property 4 that is all downward paths contain same number of black nodes<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-373 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227.png\" alt=\"\" width=\"596\" height=\"181\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227.png 596w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227-300x91.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227-225x68.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-227-350x106.png 350w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.7.2 Case 2<\/strong><\/p>\n<p><strong>Conditions for case 2 (Figure 25.14)<\/strong><\/p>\n<ul>\n<li>Now the let check whether the node that is inserted z\u2019s (B) \u201cuncle\u201d y \u2013is black and<\/li>\n<li>We then check whether z is a right child<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Steps for Case 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If all the above conditions are true we essentially need to push the p[z] up the tree. The following action will be taken when z is a right child. The following steps need to be carry out the following steps (Figure 25.13):<\/p>\n<ul>\n<li>We make z to be the parent of z (p[z]) that is z is now A.<\/li>\n<li>We then LEFT-ROTATE(T, B) about A- Now z is a left child<\/li>\n<\/ul>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Now both z (A) and p[z] (B) are red and we have a case 3 which we rebalance using the steps outlined in Section 25.7.2.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-374 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228.png\" alt=\"\" width=\"575\" height=\"234\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228.png 575w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228-300x122.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228-225x92.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-228-350x142.png 350w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>25.7 An Example showing all Three Cases<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Figure 25.15 shows an example for the insertion of 4 into the Red-black tree.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">4 is a right child. Here both 4 and it\u2019s parent 5 are red and the uncle of 4 (8) is also red. Moreover 4\u2019s grandparent is black. All these conditions gives rise to <strong>Case 1<\/strong> <strong>rebalancing<\/strong>. Accordingly we color the parent of 4 as well as it\u2019s uncle 8 as black. We then color the grandparent of 4 that is 7 as red and make 7 as the node which leads to rebalancing.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now in the rebalanced tree the node 7 and it\u2019s parent 2 are red, 7 is a right child and 7\u2019s uncle is black, all conditions that lead to a <strong>Case 2 rebalancing<\/strong>. Accordingly we make the node for rebalancing to be 7\u2019s parent 2, and then carry out a LEFT-ROTATE of the tree with 2 as the pivot.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now 2 and it\u2019s parent 7 are red, 2 is a left child and 2\u2019s uncle 14 is black. All these conditions gives rise to <strong>Case 3 rebalancing<\/strong>. Accordingly we color parent of 2 that is 7 black, and it\u2019s grandparent 11 red. We then RIGHT-ROTATE the tree with 11 as the pivot. Now we end up with the parent of 2 being black. Thus the insertion of 4 lead to three cases of rebalancing, case 1 with 4 as the main node, case 2 with 7 as the main node and finally case 3 with 2 as the main node. Finally we have a binary search tree that satisfies all the properties of the Red-Black tree.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-375 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229.png\" alt=\"\" width=\"575\" height=\"313\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229.png 575w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-229-350x191.png 350w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the concept of Red Black Trees<\/li>\n<li>Discussed the properties of Red Black Trees<\/li>\n<li>Described rotation of Red Black Trees<\/li>\n<li>Explained the Insertion into Red Black Trees<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Red Black Trees -I<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/GIlwLCrU0QY\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-376 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230.png\" alt=\"\" width=\"634\" height=\"525\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230.png 634w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230-300x248.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230-65x54.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230-225x186.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-230-350x290.png 350w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n","protected":false},"author":3,"menu_order":25,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-357","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":9,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/357\/revisions"}],"predecessor-version":[{"id":956,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/357\/revisions\/956"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/357\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=357"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=357"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=357"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}