# Explain an Insertion Operation in a Red-Black Tree | Data Structure Tutorial

# Explain an Insertion Operation in a Red-Black Tree | Data Structure Tutorial

In this article, you will learn about Insertion Operation in a Red-black Tree.

The red-Black tree is a self-balancing binary search tree in which each node has an extra bit for denoting the color of the node, either red or black.

The new node in the Red-black tree is always inserted in a Red color node. After performing the insertion operation, if the tree is invading the properties of the red-black tree then we have to perform the following operations:

- Recolor
- Rotation

**The Algorithm to insert a New Node:**

The Steps that need to be followed to insert a new item into a red-black tree:

- Take the new node i.
- Let m be the leaf and n be the root of the Red-Black Tree.
- First, we need to check whether the tree is empty. If yes, insert i(i.e new node) as a root node of the tree in black color.
- Otherwise, perform the following steps until we reach the leaf's value NIL.
- Compare the value of newkey with the rootKey.
- If the newkey is greater than the rootkey, go to the right subtree.
- Or go to the left subtree.
- Now, allocate the parent of the leaf as the parent of the i (i.e new node).
- If the value of the leafkey is greater than the value of the newkey, make the i as rightChild.
- Or else make i (i.e new node) as leftChild.
- Now, the value of the leftChild and rightChild of the newNode is Null.
- Color the newNode as RED.
- If the property of the red-black tree is broken, then immediately call the InsertFix - algorithm.

Give a reason to assign the red color to the new node in the Red-black Tree.

We insert the new node in a red color node because it does not invade the depth property of a Red-black tree. If we entangle the red node into a red node, then the rule is broken.

**Algorithm to keep the property of Red-black Tree, after insertion operation:**

This particular algorithm is used in preserving the property of a Red-Black tree if it broke while performing the insertion operation

- Perform the following steps until the color of the parent node n is RED.
- If n is the left child of grandParent gn of newNode, execute the following steps:

**Case - I:**

- Suppose, the color of the right child of the gn of n is RED, then assign the black color to the children of the gn and change the color of the gn to RED.
- Set gn to the n (i.e new node).

**Case - II:**

- Before executing this step, we need to check the value of the while loop. If the condition is not satisfied, then the loop is broken. Else if the new node is the right child of n, then set p to the new node.
- Do left rotate in newnode n.

**Case - III:**

- Again the condition of the while loop is checked, if not satisfied, it means the loop is broken. Now, set the color of n as BLACK and the color of the grandparent gn as RED.
- Do Right-Rotate in gn.

3. Otherwise, perform the following steps:

- If the color of the left child of gn of q is RED, assign the color of both the children of gn as BLACK and the color of gn as RED.
- Set gn to a new node.
- Else if the newnode is the left child of n, then set n to the newnode and do the right rotation of the newnode.
- Set the color of n as BLACK and the color of gn as RED.
- Do left rotation gn.

4. After all this, set the color of the root node as BLACK.