The Wayback Machine - https://web.archive.org/web/20240726071051/https://www.geeksforgeeks.org/left-leaning-red-black-tree-insertion/
Open In App

Left Leaning Red Black Tree (Insertion)

Last Updated : 21 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Prerequisites : Red – Black Trees.
A left leaning Red Black Tree or (LLRB), is a variant of red black tree, which is a lot easier to implement than Red black tree itself and guarantees all the search, delete and insert operations in O(logn) time.

Which nodes are RED and Which are Black ? 
Nodes which have double incoming edge are RED in color. 
Nodes which have single incoming edge are BLACK in color.

Characteristics of LLRB 
1. Root node is Always BLACK in color. 
2. Every new Node inserted is always RED in color. 
3. Every NULL child of a node is considered as BLACK in color. 
Eg : only 40 is present in tree. 

 
     root
       |
       40 <-- as 40 is the root so it 
      /  \    is also Black in color. 
    NULL  NULL <-- Black in color.

4. There should not be a node which has RIGHT RED child and LEFT BLACK child(or NULL child as all NULLS are BLACK) if present , left rotate the node, and swap the colors of current node and its LEFT child so as to maintain consistency for rule 2 i.e., new node must be RED in color. 

 CASE 1.
   root                        root     
     |                          ||      
     40      LeftRotate(40)     50     
    /  \\        --->          /  \    
  NULL  50                   40   NULL    

                               root              
                                |               
         ColorSwap(50, 40)      50 
           --->               //  \              
                             40  NULL                     

5. There should not be a node which has LEFT RED child and LEFT RED grandchild, if present Right Rotate the node and swap the colors between node and it’s RIGHT child to follow rule 2. 

  CASE 2.
   root                         root      
    |                            ||        
    40        RightRotate(40)    20        
   //  \         --->          //   \      
  20    50                    10    40           
 //                                   \      
10                                     50

                          root
                           |
    ColorSwap(20, 40)      20
      --->                // \\
                         10   40
                               \
                               50
 

6. There should not be a node which has LEFT RED child and RIGHT RED child, if present Invert the colors of all nodes i. e., current_node, LEFT child, and RIGHT child. 

   CASE 3.
   root                           root      
    |       !color(20, 10, 30)     ||       
    20            --->             20                
  //  \\                          /  \  
 10    30                        10  30 

                                     root
        As the root is always black    |                     
                --->                  20   
                                     /  \
                                   10   30   

Why are we following the above mentioned rules? Because by following above characteristics/rules we are able to simulate all the red-black tree’s properties without caring about the complex implementation of it. 

Example: 

Insert the following data into LEFT LEANING RED-BLACK
TREE and display the inorder traversal of tree.
Input : 10 20 30 40 50 25
Output : 10 20 30 40 50 25
        root
         |
         40
       //  \
      20    50
     /  \
   10    30
        //
       25 

Approach : 
Insertions in the LLRB is exactly like inserting into a Binary search tree . The difference is that  After we insert the node into the tree we will retrace our steps back to root and try to enforce the above rules for LLRB. 
While doing the above rotations and swapping of color it may happen that our root becomes RED in color so we also. We have to make sure that our root remains always BLACK in color. 

LLRB insertion Example.

C++




// C++ program to implement insert operation
// in Red Black Tree.
#include <bits/stdc++.h>
using namespace std;
 
typedef struct node
{
    struct node *left, *right;
    int data;
     
    // red ==> true, black ==> false
    bool color;
     
}node;
 
// Utility function to create a node.
node* createNode(int data, bool color)
{
    node *myNode = new node();
    myNode -> left = myNode -> right = NULL;
    myNode -> data = data;
 
    // New Node which is created is
    // always red in color.
    myNode -> color = true;
    return myNode;
}
 
// Utility function to rotate node anticlockwise.
node* rotateLeft(node* myNode)
{
    cout << "left rotation!!\n";
    node *child = myNode -> right;
    node *childLeft = child -> left;
 
    child -> left = myNode;
    myNode -> right = childLeft;
 
    return child;
}
 
// Utility function to rotate node clockwise.
node* rotateRight(node* myNode)
{
    cout << "right rotation\n";
    node *child = myNode -> left;
    node *childRight =  child -> right;
 
    child -> right = myNode;
    myNode -> left = childRight;
     
    return child;
}
 
// Utility function to check whether
// node is red in color or not.
int isRed(node *myNode)
{
    if (myNode == NULL)
       return 0;
        
    return (myNode -> color == true);
}
 
// Utility function to swap color of two
// nodes.
void swapColors(node *node1, node *node2)
{
    bool temp = node1 -> color;
    node1 -> color = node2 -> color;
    node2 -> color = temp;
}
 
// Insertion into Left Leaning Red Black Tree.
node* insert(node* myNode, int data)
{
     
    // Normal insertion code for any Binary
    // Search tree.
    if (myNode == NULL)
        return createNode(data, false);   
 
    if (data < myNode -> data)
        myNode -> left = insert(myNode -> left, data);
 
    else if (data > myNode -> data)
        myNode -> right = insert(myNode -> right, data);
 
    else   
        return myNode;
         
    // case 1.
    // when right child is Red but left child is
    // Black or doesn't exist.
    if (isRed(myNode -> right) &&
       !isRed(myNode -> left))
    {
         
        // Left rotate the node to make it into
        // valid structure.
        myNode = rotateLeft(myNode);
 
        // Swap the colors as the child node
        // should always be red
        swapColors(myNode, myNode -> left);
    }
     
    // case 2
    // when left child as well as left grand
    // child in Red
    if (isRed(myNode -> left) &&
        isRed(myNode -> left -> left))
    {  
         
        // Right rotate the current node to make
        // it into a valid structure.
        myNode = rotateRight(myNode);
        swapColors(myNode, myNode -> right);
    }
     
    // case 3
    // when both left and right child are Red in color.
    if (isRed(myNode -> left) && isRed(myNode -> right))
    {
         
        // Invert the color of node as well
        // it's left and right child.
        myNode -> color = !myNode -> color;
 
        // Change the color to black.
        myNode -> left -> color = false;
        myNode -> right -> color = false;
    }
    return myNode;
}
 
// Inorder traversal
void inorder(node *node)
{
    if (node)
    {
        inorder(node -> left);
        cout<< node -> data << " ";
        inorder(node -> right);
    }
}
 
// Driver code
int main()
{
     
    node *root = NULL;
     
    /* LLRB tree made after all insertions are made.
             
    1. Nodes which have double INCOMING edge means
       that they are RED in color.
    2. Nodes which have single INCOMING edge means
       that they are BLACK in color.
     
        root
         |
         40
       //  \
      20    50
     /  \
    10    30
        //
       25    */
     
    root = insert(root, 10);
     
    // To make sure that root remains
    // black is color
    root -> color = false;
     
    root = insert(root, 20);
    root -> color = false;
     
    root = insert(root, 30);
    root -> color = false;
     
    root = insert(root, 40);
    root -> color = false;
     
    root = insert(root, 50);
    root -> color = false;
     
    root = insert(root, 25);
    root -> color = false;
     
    // Display the tree through inorder traversal.
    inorder(root);
     
    return 0;
}
 
// This code is contributed by rutvik_56


C




// C program to implement insert operation
// in Red Black Tree.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
 
typedef struct node
{
    struct node *left, *right;
    int data;
 
    // red ==> true, black ==> false
    bool color;
} node;
 
 
 
// utility function to create a node.
node* createNode(int data, bool color)
{
    node *myNode = (node *) malloc(sizeof(node));
    myNode -> left = myNode -> right = NULL;
    myNode -> data = data;
 
    // New Node which is created is
    // always red in color.
    myNode -> color = true;
    return myNode;
}
 
// utility function to rotate node anticlockwise.
node* rotateLeft(node* myNode)
{
    printf("left rotation!!\n");
    node *child = myNode -> right;
    node *childLeft = child -> left;
 
    child -> left = myNode;
    myNode -> right = childLeft;
 
    return child;
}
 
// utility function to rotate node clockwise.
node* rotateRight(node* myNode)
{
    printf("right rotation\n");
    node *child = myNode -> left;
    node *childRight =  child -> right;
 
    child -> right = myNode;
    myNode -> left = childRight;
     
    return child;
}
 
// utility function to check whether
// node is red in color or not.
int isRed(node *myNode)
{
    if (myNode == NULL)
       return 0;
    return (myNode -> color == true);
}
 
// utility function to swap color of two
// nodes.
void swapColors(node *node1, node *node2)
{
    bool temp = node1 -> color;
    node1 -> color = node2 -> color;
    node2 -> color = temp;
}
 
// insertion into Left Leaning Red Black Tree.
node* insert(node* myNode, int data)
{
    // Normal insertion code for any Binary
    // Search tree.
    if (myNode == NULL)
        return createNode(data, false);   
 
    if (data < myNode -> data)
        myNode -> left = insert(myNode -> left, data);
 
    else if (data > myNode -> data)
        myNode -> right = insert(myNode -> right, data);
 
    else   
        return myNode;
 
     
    // case 1.
    // when right child is Red but left child is
    // Black or doesn't exist.
    if (isRed(myNode -> right) && !isRed(myNode -> left))
    {
        // left rotate the node to make it into
        // valid structure.
        myNode = rotateLeft(myNode);
 
        // swap the colors as the child node
        // should always be red
        swapColors(myNode, myNode -> left);
         
    }
     
    // case 2
    // when left child as well as left grand child in Red
    if (isRed(myNode -> left) && isRed(myNode -> left -> left))
    {  
        // right rotate the current node to make
        // it into a valid structure.
        myNode = rotateRight(myNode);
        swapColors(myNode, myNode -> right);
    }
     
     
    // case 3
    // when both left and right child are Red in color.
    if (isRed(myNode -> left) && isRed(myNode -> right))
    {
        // invert the color of node as well
        // it's left and right child.
        myNode -> color = !myNode -> color;
 
        // change the color to black.
        myNode -> left -> color = false;
        myNode -> right -> color = false;
    }
 
    return myNode;
}
 
// Inorder traversal
void inorder(node *node)
{
    if (node)
    {
        inorder(node -> left);
        printf("%d ", node -> data);
        inorder(node -> right);
    }
}
 
// Driver function
int main()
{
    node *root = NULL;
    /* LLRB tree made after all insertions are made.
             
    1. Nodes which have double INCOMING edge means
       that they are RED in color.
    2. Nodes which have single INCOMING edge means
       that they are BLACK in color.
     
        root
         |
         40
       //  \
      20    50
     /  \
   10    30
        //
       25    */
 
    root = insert(root, 10);
    // to make sure that root remains
    // black is color
    root -> color = false;
     
    root = insert(root, 20);
    root -> color = false;
     
    root = insert(root, 30);
    root -> color = false;
    
    root = insert(root, 40);
    root -> color = false;
     
    root = insert(root, 50);
    root -> color = false;
     
    root = insert(root, 25);
    root -> color = false;
 
    // display the tree through inorder traversal.
    inorder(root);
     
    return 0;
}


Java




// Java program to implement insert operation
// in Red Black Tree.
 
class node
{
    node left, right;
    int data;
 
    // red ==> true, black ==> false
    boolean color;
     
    node(int data)
    {
        this.data = data;
        left = null;
        right = null;
         
        // New Node which is created is
        // always red in color.
        color = true;
    }
}
 
public class LLRBTREE {
 
    private static node root = null;
     
    // utility function to rotate node anticlockwise.
    node rotateLeft(node myNode)
    {
        System.out.printf("left rotation!!\n");
        node child = myNode.right;
        node childLeft = child.left;
 
        child.left = myNode;
        myNode.right = childLeft;
 
        return child;
    }
 
    // utility function to rotate node clockwise.
    node rotateRight(node myNode)
    {
        System.out.printf("right rotation\n");
        node child = myNode.left;
        node childRight = child.right;
 
        child.right = myNode;
        myNode.left = childRight;
 
        return child;
    }
 
    // utility function to check whether
    // node is red in color or not.
    boolean isRed(node myNode)
    {
        if (myNode == null)
            return false;
        return (myNode.color == true);
    }
 
    // utility function to swap color of two
    // nodes.
    void swapColors(node node1, node node2)
    {
        boolean temp = node1.color;
        node1.color = node2.color;
        node2.color = temp;
    }
 
    // insertion into Left Leaning Red Black Tree.
    node insert(node myNode, int data)
    {
        // Normal insertion code for any Binary
        // Search tree.
        if (myNode == null)
            return new node(data);
 
        if (data < myNode.data)
            myNode.left = insert(myNode.left, data);
         
        else if (data > myNode.data)
            myNode.right = insert(myNode.right, data);
         
        else
            return myNode;
 
 
        // case 1.
        // when right child is Red but left child is
        // Black or doesn't exist.
        if (isRed(myNode.right) && !isRed(myNode.left))
        {
            // left rotate the node to make it into
            // valid structure.
            myNode = rotateLeft(myNode);
 
            // swap the colors as the child node
            // should always be red
            swapColors(myNode, myNode.left);
 
        }
 
        // case 2
        // when left child as well as left grand child in Red
        if (isRed(myNode.left) && isRed(myNode.left.left))
        {
            // right rotate the current node to make
            // it into a valid structure.
            myNode = rotateRight(myNode);
            swapColors(myNode, myNode.right);
        }
 
 
        // case 3
        // when both left and right child are Red in color.
        if (isRed(myNode.left) && isRed(myNode.right))
        {
            // invert the color of node as well
            // it's left and right child.
            myNode.color = !myNode.color;
 
            // change the color to black.
            myNode.left.color = false;
            myNode.right.color = false;
        }
 
        return myNode;
    }
 
 
    // Inorder traversal
    void inorder(node node)
    {
        if (node != null)
        {
            inorder(node.left);
            System.out.print(node.data + " ");
            inorder(node.right);
        }
    }
 
    public static void main(String[] args) {
    /* LLRB tree made after all insertions are made.
             
    1. Nodes which have double INCOMING edge means
        that they are RED in color.
    2. Nodes which have single INCOMING edge means
        that they are BLACK in color.
     
        root
        |
        40
        // \
    20 50
    / \
    10 30
        //
        25 */
     
    LLRBTREE node = new LLRBTREE();
     
    root = node.insert(root, 10);
    // to make sure that root remains
    // black is color
    root.color = false;
     
    root = node.insert(root, 20);
    root.color = false;
     
    root = node.insert(root, 30);
    root.color = false;
     
    root = node.insert(root, 40);
    root.color = false;
     
    root = node.insert(root, 50);
    root.color = false;
     
    root = node.insert(root, 25);
    root.color = false;
 
    // display the tree through inorder traversal.
    node.inorder(root);
 
    }
     
}
 
// This code is contributed by ARSHPREET_SINGH


C#





Python3





Javascript





Output

left rotation!!
left rotation!!
left rotation!!
10 20 25 30 40 50 

Time Complexity: O(log n)
References 
Left Leaning Red Black Trees 

 



Previous Article
Next Article

Similar Reads

C Program for Red Black Tree Insertion
Following article is extension of article discussed here.In AVL tree insertion, we used rotation as a tool to do balancing after insertion caused imbalance. In Red-Black tree, we use two tools to do balancing. Recoloring Rotation We try recoloring first, if recoloring doesn't work, then we go for rotation. Following is detailed algorithm. The algor
6 min read
Insertion in Red-Black Tree
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.  RecoloringRotationRecolouring is the change in colour of the node i.e. if it is red then change
15+ min read
Count of graphs formed by changing color of any red-colored node with black parent to black
Given a directed graph G consisting of N nodes and N-1 edges, and a positive integer K, and initially, all the nodes of the graph are red except for K, which is black, the task is to count the number of different possible graphs formed by changing the color of any red-colored node to black, only if their parent is colored black, any number of times
8 min read
Check if a given Binary Tree is height balanced like a Red-Black Tree
In a Red-Black Tree, the maximum height of a node is at most twice the minimum height (The four Red-Black tree properties make sure this is always followed). Given a Binary Search Tree, we need to check for following property. For every node, length of the longest leaf to node path has not more than twice the nodes on shortest path from node to lea
9 min read
Red Black Tree vs AVL Tree
In this post, we will compare Red-Black Tree and AVL Tree. Red Black Tree: Properties: Self-Balancing is provided by painting each node with two colors(Red or Black).When the Tree is modified, a new tree is subsequently rearranged and repainted.It requires 1 bit of color information for each node in the tree.Time complexity: O(logn). Constraints ma
2 min read
Applications, Advantages and Disadvantages of Red-Black Tree
Red-Black Tree is one type of self-balancing tree where each node has one extra bit that is often interpreted as colour of the node. This bit (the colour) is used to ensure that the tree remains balanced. Properties of Red-Black Trees: Red-Black Trees have the accompanying properties: Each hub has a variety.The root is black.Each leaf is an excepti
4 min read
What is the difference between Heap and Red-Black Tree?
What is Heap? A Heap is a special Tree-based data structure in which the tree is a complete binary tree. There are two types of heap - Min heap and Max heap. To learn more about heap, go through the following article: Introduction to Heap What is a Red-Black Tree?The red-Black tree is a self-balancing binary search tree in which each node contains
2 min read
Red-Black Tree definition & meaning in DSA
A red-black tree is a self-balancing binary search tree in which each node of the tree has an color, which can either be red or black. Characteristics of Red Black Tree:The root node is always black and each node can be either black or red.Every leaf node of the red-black tree is black.The children of red nodes are black.The number of black nodes w
2 min read
Deletion in Red-Black Tree
Deletion in a red-black tree is a bit more complicated than insertion. When a node is to be deleted, it can either have no children, one child or two children. Here are the steps involved in deleting a node in a red-black tree:If the node to be deleted has no children, simply remove it and update the parent node.If the node to be deleted has only o
15+ min read
Print a matrix in alternate manner (left to right then right to left)
Given a 2D array, the task is to print the 2D in alternate manner (First row from left to right, then from right to left, and so on). Examples: Input : arr[][2] = {{1, 2} {2, 3}}; Output : 1 2 3 2 Input :arr[][3] = { { 7 , 2 , 3 }, { 2 , 3 , 4 }, { 5 , 6 , 1 }}; Output : 7 2 3 4 3 2 5 6 1 The solution of this problem is that run two loops and print
5 min read
three90RightbarBannerImg