A Red Black Tree is a type of self-balancing binary search tree, in which every node is colored with a red or black. The red black tree satisfies all the properties of the binary search tree but there are some additional properties which were added in a Red Black Tree. The height of a Red- Black tree is O (Logn) where (n is the number of nodes in the tree)

A red-black tree satisfies the following properties:

  1. Red/Black Property: Every node is colored, either red or black.
  2. Root Property: The root is black.
  3. Leaf Property: Every leaf (NIL) is black.
  4. Red Property: If a red node has children then, the children are always black.
  5. Depth Property: For each node, any simple path from this node to any of its descendant leaf has the same black-depth (the number of black nodes).

An example of a red-black tree is:

Each node has the following attributes:

  • color
  • key
  • leftChild
  • rightChild
  • parent (except root node)

How the red-black tree maintains the property of self-balancing?

The red-black color is meant for balancing the tree.

The limitations put on the node colors ensure that any simple path from the root to a leaf is not more than twice as long as any other such path. It helps in maintaining the self-balancing property of the red-black tree.

Operations on a Red-Black Tree


Various operations that can be performed on a red-black tree are:

Rotating the subtrees in a Red-Black Tree

In rotation operation, the positions of the nodes of a subtree are interchanged.

Rotation operation is used for maintaining the properties of a red-black tree when they are violated by other operations such as insertion and deletion.

There are two types of rotations:

1.  Left Rotate

In left-rotation, the arrangement of the nodes on the right is transformed into the arrangements on the left node.

Algorithm

  1. Let the initial tree be:

                     

  1. If y has a left subtree, assign x as the parent of the left subtree of y.

                           

  1. If the parent of x p is NULL, make y as the root of the tree.
  2. Else if x is the left child of p, make y as the left child of p.
  3. Else assign y as the right child of p.

                   

  1. Make y as the parent of x.

                       

2.  Right Rotate

In right-rotation, the arrangement of the nodes on the left is transformed into the arrangements on the right node.

  1. Let the initial tree be:

                   

  1. If x has a right subtree, assign y as the parent of the right subtree of x.

                               

  1. If the parent of y is NULL, make x as the root of the tree.
  2. Else if y is the right child of its parent p, make x as the right child of p.
  3. Else assign x as the left child of p.

                                         

 

  1. Make x as the parent of y.

                                 

3.  Left-Right and Right-Left Rotate

In left-right rotation, the arrangements are first shifted to the left and then to the right.

  1. Do left rotation on x-y.

                 

 

  1. Do right rotation on y-z.

                   

In right-left rotation, the arrangements are first shifted to the right and then to the left.

  1. Do right rotation on x-y.

                                 

  1. Do left rotation on z-y.

                                 

Inserting an Element into a Red-Black Tree


While inserting a new node, the new node is always inserted as a RED node. After insertion of a new node, if the tree is violating the properties of the red-black tree then, we do the following operations.

  1. Recolor
  2. Rotation

Following steps are followed for inserting a new element into a red-black tree:

1. The newNode be:

                             

2. Let y be the leaf (ie. NIL) and x be the root of the The new node is inserted in the following tree.

                   

 

3. Check if the tree is empty (ie. whether x is NIL). If yes, insert newNode as a root node and color it black.

4. Else, repeat steps following steps until leaf (NIL) is reached.

    1. Compare newKey with rootKey.
    2. If newKey is greater than rootKey, traverse through the right subtree.
    3. Else traverse through the left subtree.

                                       

  1. Assign the parent of the leaf as parent of newNode.
  2. If leafKey is greater than newKey, make newNode as rightChild.
  3. Else, make newNode as leftChild.

                                   

  1. Assign NULL to the left and rightChild of newNode.
  2. Assign RED color to newNode.

                                     

 

  1. Call InsertFix-algorithm to maintain the property of red-black tree if violated.

Why newly inserted nodes are always red in a red-black tree?

This is because inserting a red node does not violate the depth property of a red-black tree.

If you attach a red node to a red node, then the rule is violated but it is easier to fix this problem than the problem introduced by violating the depth property.

Algorithm to maintain red-black property after insertion

This algorithm is used for maintaining the property of a red-black tree if insertion of a newNode

violates this property.

  1. Do the following until the parent of newNode p is RED.
  2. If p is the left child of grandParent gP of newNode, do the following.

Case-I:

  1. the color of the right child of gP of newNode is RED, set the color of both the children of gP as BLACK and the color of gP as RED.

b. Assign gP to newNode.

Case-II:

c.  (Before moving on to this step, while loop is checked. If conditions are not satisfied, it the loop is broken.)

Else if newNode is the right child of p then, assign p to newNode.

 

d. Left-Rotate newNode.

Case-III:

e. (Before moving on to this step, while loop is checked. If conditions are not satisfied, it the loop is broken.)

Set color of p as BLACK and color of gP as RED.

f. Right-Rotate gP.

  1. Else, do the
    1. If the color of the left child of gP of z is RED, set the color of both the children of gP as BLACK and the color of gP as RED.
    2. Assign gP to newNode.
    3. Else if newNode is the left child of p then, assign p to newNode and Right- Rotate newNode.
    4. Set color of p as BLACK and color of gP as RED.
    5. Left-Rotate gP.
  1. (This step is perfomed after coming out of the while loop.) Set the root of the tree as BLACK.

                 

 

The final tree look like this:

Deleting an element from a Red-Black Tree

This operation removes a node from the tree. After deleting a node, the red-black property is maintained again.

  1. Let the nodeToBeDeleted be:

  1. Save the color of nodeToBeDeleted in origrinalColor.

  1. If the left child of nodeToBeDeleted is NULL
    1. Assign the right child of nodeToBeDeleted to x.

                 

 

            b. Transplant nodeToBeDeleted with x.

 

  1. Else if the right child of nodeToBeDeleted is NULL
  1. Assign the left child of nodeToBeDeleted into x.

      b. Transplant nodeToBeDeleted with x.

  1. Else
    1. Assign the minimum of right subtree of noteToBeDeleted into y.
    2. Save the color of y in originalColor.
    3. Assign the rightChild of y into x.
    4. If y is a child of nodeToBeDeleted, then set the parent of x as y.
    5. Else, transplant y with rightChild of y.
    6. Transplant nodeToBeDeleted with y.
    7. Set the color of y with originalColor.
  2. If the originalColor is BLACK, call DeleteFix(x).

Algorithm to maintain Red-Black property after deletion

This algorithm is implemented when a black node is deleted because it violates the black depth property of the red-black tree.

This violation is corrected by assuming that node x (which is occupying y's original position) has an extra black. This makes node x neither red nor black. It is either doubly black or black-and- red. This violates the red-black properties.

However, the color attribute of x is not changed rather the extra black is represented in x's pointing to the node.

The extra black can be removed if

  1. It reaches the root node.
  2. If x points to a red-black In this case, x is colored black.
  3. Suitable rotations and recolorings are performed.

Following algorithm retains the properties of a red-black tree.

  1.  Do the following until the x is not the root of the tree and the color of x is BLACK
  2. If x is the left child of its parent then,
  1. Assign w to the sibling of x.

                 

 

b. If the sibling of x is RED,

Case-I:

  1. Set the color of the right child of the parent of x as BLACK.
  2. Set the color of the parent of x as RED.

 

iii. Left-Rotate the parent of x.

 

iv. Assign the rightChild of the parent of x to w.

 

c. If the color of both the right and the leftChild of w is BLACK,

Case-II:

  1. Set the color of w as RED
  2. Assign the parent of x to x.

d. Else if the color of the rightChild of w is BLACK

Case-III:

  1. Set the color of the leftChild of w as BLACK
  2. Set the color of w as RED

 

iii. Right-Rotate w.

iv. Assign the rightChild of the parent of x to w.

e. If any of the above cases do not occur, then do the following.

Case-IV:

  1. Set the color of w as the color of the parent of x.
  2. Set the color of the parent of parent of x as BLACK.
  3. Set the color of the right child of w as BLACK.

iv. Left-Rotate the parent of x.

v. Set x as the root of the tree.

3. Else same as above with right changed to left and vice versa.

4. Set the color of x as BLACK.

The workflow of the above cases can be understood with the help of the flowchart below.