Insertion in The Red Black Tree New

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

2.

b
Insertion in the Red Black Tree

A)Red Black Tree is a Self-Balanced Binary Search Tree in which each node of
the tree is colored with either Red or Black. There are three types of operations
we can perform on a Red Black Tree – Searching, Insertion and Deletion.
Let us suppose we have to insert an element in the following Red Black Tree.

To insert an element in a red-black tree the idea is very simple − we perform


insertion just like we insert in a regular binary tree. We start off from the root
node by checking the color of the node and insert it into a particular position.
However, In a red-black tree there should be some additional procedure to
insert an element in it.
However, we should know that a red-black tree is balanced when it follows the
conditions −
 Every Root Node must be Black.
 Every node is either Red or Black.
 If a node is red, then its children must be black.
 The path from the root till the end must contain an equal number of
black nodes.
If we want to insert a new node in a red-black tree, then we can do it by looking
at the insertion steps.
Steps to Insert an Element in a Red Black Tree −
 Check whether the tree is empty or not. If the tree is empty, then
insert a new node and color it as Black. (Because Root Node must
be always Black in color)’
 Otherwise if the Tree is not empty then insert the new node as a
leaf node to the end and color it as Red.
 If the parent of the new node is Red and its neighbouring(parent’s)
node is also Red then Flip the color of the both neighbour and
Parent and Grandparents (If it is not Root Node Otherwise Flip the
color of the Parent and neighbour only) i.e., Black.
 If the parent of the new node is Red and its neighbouring(parent’s)
node is empty or NULL, then Rotate (either Left-Left or Left-Right
rotation) the new node and parent.
There are two types of rotation that would apply- Left Left Rotation and Left
Right Rotation. The Rotation would apply in some conditions only. The
conditions are −
 If the parent of the new node is Red and the neighbouring node is
empty or NULL, then rotate left or right rotation.
 In Left-Left Rotation flip the color of the parent and grandparent.
Make the parent as Grandparent and grandparent as child.
Algorithm
RB Tree Insertion (root,key)
//The color of the inserted new node is Red
color[key] <- Red
while(key≠root and color (p[key]=Red))
do if p[key]= left(p[p[key]])
Then y←right[p[p[key]]
// If the parent of the new node is Red(if there is Grandparent instead
root Node) Flip the color.
if color[y]← Red
then color(p[key])← Black
color(p[p[key]])← Red
key← p[p[key]]
else if key← right[p[key]]
then key← p[key]
//When parent of new node has the red color and its sibling is NULL
LeftRotate(root,key)
color(p[key]) ← Black
color(p[p[key]]) ← Red
RotateRight(root,p[p[key]])
else exchange then left and right elements to make it balance.
color(root)← Black
B)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:

Node to be deleted
2. Save the color

of nodeToBeDeleted in origrinalColor. Saving


original color
3. If the left child of nodeToBeDeleted is NULL
a. Assign the right child of nodeToBeDeleted to

x.
b. Assign x to the rightChild
c. Transplant nodeToBeDeleted with x.

Transplant nodeToBeDeleted with x


4. Else if the right child of nodeToBeDeleted is NULL
a. Assign the left child of nodeToBeDeleted into x.
b. Transplant nodeToBeDeleted with x.
5. Else

a. Assign the minimum of right subtree of noteToBeDeleted into y.


b. Save the color of y in originalColor.
c. Assign the rightChild of y into x.
d. If y is a child of nodeToBeDeleted, then set the parent of x as y.
e. Else, transplant y with rightChild of y.
f. Transplant nodeToBeDeleted with y.
g. Set the color of y with originalColor.

6. 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 node. 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,
a. Assign w to the sibling

of x. Assigning w
b. If the sibling of x is RED,
Case-I:
a. Set the color of the right child of the parent of x as BLACK.
b. Set the color of the parent of x as
RED.

Color change
c. Left-Rotate the parent of x.Left-rotate
d. Assign the rightChild of the parent

of x to w. Reassign
w
c. If the color of both the right and the leftChild of w is BLACK,
Case-II:
a. Set the color of w as RED
b. Assign the parent of x to x.
d. Else if the color of the rightChild of w is BLACK
Case-III:
a. Set the color of the leftChild of w as BLACK
b. Set the color of w as
RED

Color change
c. Right-Rotate w.Right rotate
d. Assign the rightChild of the parent

of x to w. Reassign
w
e. If any of the above cases do not occur, then do the following.
Case-IV:
a. Set the color of w as the color of the parent of x.
b. Set the color of the parent of parent of x as BLACK.
c. Set the color of the right child of w as
BLACK.

Color change
d. Left-Rotate the parent of x . Left-rotate
e. Set x as the root of the

tree. Set
x as root
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.
Flowchart for deletion operation

You might also like