You are on page 1of 5

1.

If tree is empty create a node as root node with color


black.
2. If tree is not empty create a leaf node with color Red.
3. If parent of new node is black then exit.
4. If parent of new node is Red then check the color of
parent’s sibling of new node.
A) If color is black or null then do suitable rotation
and coloring.
B) If color is Red then recolor & also check if its
parent’s parent of new node is not root node then
recolor it and recheck.

Rules That Every Red-Black Tree Follows: 


5.The root of the tree is always black.
6.There are no two adjacent red nodes (A red node cannot
have a red parent or red child).
7.Every path from a node (including root) to any of its
descendants NULL nodes has the same number of black
nodes.
B
L L
Left Rotate Parent B. Rotate Parent C.
Now apply LL rotation C Now apply RR rotation case.
case.

A B

AA D AA C A C D
L R
R L
D BD A C
BB DC CB D A
L R
R L
D Left Rotate Parent B. B
B C
Now apply LL rotation D
C
case
A C
R
B C A D
R

Left rotation of Grandfather D B


A.
Then swap the color of
Grandfather A and Parent B
Step 5: Final tree structure

The final tree will look like this

You might also like