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 Code ; 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 .
using System ; class node { public node left , right ; public int data ; public Boolean color ; public node ( int data ) { this . data = data ; left = null ; right = null ; color = true ; } } public class LLRBTREE { private static node root = null ; node rotateLeft ( node myNode ) { Console . Write ( " left ▁ rotation ! ! STRNEWLINE " ) ; node child = myNode . right ; node childLeft = child . left ; child . left = myNode ; myNode . right = childLeft ; return child ; } node rotateRight ( node myNode ) { Console . Write ( " right ▁ rotation STRNEWLINE " ) ; node child = myNode . left ; node childRight = child . right ; child . right = myNode ; myNode . left = childRight ; return child ; } Boolean isRed ( node myNode ) { if ( myNode == null ) return false ; return ( myNode . color == true ) ; } void swapColors ( node node1 , node node2 ) { Boolean temp = node1 . color ; node1 . color = node2 . color ; node2 . color = temp ; } node insert ( node myNode , int data ) { 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 ; 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 != null ) { inorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inorder ( node . right ) ; } } static public void Main ( String [ ] args ) { LLRBTREE node = new LLRBTREE ( ) ; root = node . insert ( root , 10 ) ; 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 ; node . inorder ( root ) ; } }