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