Skip to content
Related Articles

Related Articles

Red-Black Tree | Set 2 (Insert)
  • Difficulty Level : Medium
  • Last Updated : 06 May, 2021

In the previous post, we discussed the introduction to Red-Black Trees. In this post, insertion is discussed. In AVL tree insertion, we used rotation as a tool to do balancing after insertion. In the Red-Black tree, we use two tools to do the balancing. 

  1. Recoloring
  2. Rotation

Recolouring is the change in colour of the node i.e. if it is red then change it to black and vice versa. It must be noted that the colour of the NULL node is always black. Moreover, we always try recolouring first, if recolouring doesn’t work, then we go for rotation. Following is a detailed algorithm. The algorithms have mainly two cases depending upon the colour of the uncle. If the uncle is red, we do recolour. If the uncle is black, we do rotations and/or recolouring.

The representation we will be working with is: 
 

This representation is based on X

Logic:

First, you have to insert the node similarly to that in a binary tree and assign a red colour to it. Now, if the node is a root node then change its colour to black, but if it does not then check the colour of the parent node. If its colour is black then don’t change the colour but if it is not i.e. it is red then check the colour of the node’s uncle. If the node’s uncle has a red colour then change the colour of the node’s parent and uncle to black and that of grandfather to red colour and repeat the same process for him (i.e. grandfather).

But, if the node’s uncle has black colour then there are 4 possible cases:



  • Left Left Case (LL rotation):

  • Left Right Case (LR rotation):

  • Right Right Case (RR rotation):

  • Right Left Case (RL rotation):

Now, after these rotations, if the colours of the nodes are miss matching then recolour them.

Algorithm:

Let x be the newly inserted node.

  1. Perform standard BST insertion and make the colour of newly inserted nodes as RED.
  2. If x is the root, change the colour of x as BLACK (Black height of complete tree increases by 1).
  3. Do the following if the color of x’s parent is not BLACK and x is not the root. 
    a) If x’s uncle is RED (Grandparent must have been black from property 4
    (i) Change the colour of parent and uncle as BLACK. 
    (ii) Colour of a grandparent as RED. 
    (iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x. 

    b) If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g) (This is similar to AVL Tree
    (i) Left Left Case (p is left child of g and x is left child of p) 
    (ii) Left Right Case (p is left child of g and x is the right child of p) 
    (iii) Right Right Case (Mirror of case i) 
    (iv) Right Left Case (Mirror of case ii)

Example: Creating a red-black tree with elements 3, 21, 32 and 17 in an empty tree.

Solution: 



When the first element is inserted it is inserted as a root node and as root node has black colour so it acquires the colour black.

 

The new element is always inserted with a red colour and as 21 > 3 so it becomes the part of the right subtree of the root node. 

 

Now, as we insert 32 we see there is a red father-child pair which violates the Red-Black tree rule so we have to rotate it. Moreover, we see the conditions of RR rotation (considering the null node of the root node as black) so after rotation as the root node can’t be Red so we have to perform recolouring in the tree resulting in the tree shown above. 

 

Final Tree Structure: 

The final tree will look like this

Please refer C Program for Red Black Tree Insertion for complete implementation of the above algorithm.

Red-Black Tree | Set 3 (Delete)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :