









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of a Red-Black Tree, a variant of a binary search tree where every node is colored either red or black. The tree must satisfy specific properties, including being a binary search tree, having no two consecutive red nodes, and having the same number of black nodes in all paths. The insertion process involves adding a new node as a leaf with red color and then recoloring nodes to maintain the properties. The deletion process is similar to that of a binary search tree but requires checking and maintaining the Red-Black Tree properties.
What you will learn
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
Red black tree is another variant of binary search tree in which every node is colored either red or black
we can define a red black tree as follows:
Red black tree is a binary search tree in which every node is colored either red or black.
struct t_red_black_node {
enum { red, black } colour;
void *item;
struct t_red_black_node *left,
*right,
*parent;
}
In red black tree the color of node is decided based on the properties of red black tree. Every red black
tree has the following properties:
2 .The root node must be colored black.
The children of red color node must be colored black. There should not be two consecutive red nodes.
In all the paths of the tree there should be same number of black color nodes.
Every new node must be inserted with red color.
Every leaf( i.e null node) must be colored black.
Example:
Following is a red black tree which is created by inserting number from 1 to 9.
The above tree is a red black tree where every node is satisfying all the properties of red black tree.
2
7 9
6
In a red black tree, every new node must be inserted with the color red. The insertion operation in red
black tree is similar to insertion operation in binary search tree. But it is inserted with a color property.
After every insertion operation, we need to check all the properties of red black tree. If all the properties
are satisfied then we go to next operation otherwise we perform the following operation to make it red
black tree.
The operations are
Recolor
Rotation
Rotation followed by recolor.
The insertion operation in red black tree is performed using the following steps:
Step 1 : Check whether tree is empty.
Step 2 : If tree is empty when insert the newnode as root node with color black an exit from the
operation.
Step 3 : If tree is not empty then insert the newnode as leaf node with color red.
Step4: If the parent of newnode is black then exit from the operation.
Step5: If the parent of newnode is r red then change the color of parent node’s sibling of newnode.
Step6: I f it is colored black or null then make suitable rotation and recolor it.
Step7: I f it is colored red then perform recolor.
Repeat the same until tree becomes red black tree.
Example:
Create a red black tree by inserting following sequence of number :-
8 , 18 , 5 , 15 , 17 , 25 , 40 , and 80
Insert ( 8 )
Tree is empty. So insert newnode as root node with black color.
Insert ( 17 )
The tree is not empty. So insert newnode with red color.
After left rotation
After right rotation and recolor
Insert ( 25 )
Tree is not empty. So insert newnode with red color.
18
88
88
18
18
Here there two consecutive red nodes 15 and
17.The newnode’s parent sibling is null. So we
need rotation. Here we need LR rotation and
recolor.
18
88
88
18
18
17
88
88
18
18
There are two consecutive red nodes 18 and
and parent’s parent is not root node. So we
use recolor and recheck.
After recolor
Insert( 40 )
Tree is not empty. So insert newnode with red color.
After LL rotation and recolor
17
88
88
18
18
After recolor operation, the tree is
satisfying all red black tree properties.
17
88
88
18
18
Here there are two consecutive red nodes
25 and 40. The newnode’s parent sibling is
null. So we need rotation and recolor.
Here we use LL rotation and recheck.
17
88
88
18
18
After LL rotation and recolor operation,
the tree satisfying all red black tree
properties.
Finally above tree is satisfying all the properties of red black tree and it is a perfect red black tree.
The deletion operation in red black tree is similar to deletion operation in BST. But after every deletion
operation we need to check with the red black tree properties. If any of the properties violated then
make suitable operations like recolor, rotation and rotation followed by recolor to make it red black
tree.
A rotation is a local operation in a search tree that preserves in-order traversal key ordering.
A x B y C
The left_rotate operation may be encoded:
left_rotate( Tree T, node x ) {
node y;
y = x->right;
/* Turn y's left sub-tree into x's right sub-tree */
x->right = y->left;
if ( y->left != NULL )
y->left->parent = x;
/* y's new parent was x's parent */
y->parent = x->parent;
25 8
/* Set the parent to point to y instead of x */
/* First see whether we're at the root */
if ( x->parent == NULL ) T->root = y;
else
if ( x == (x->parent)->left )
/* x was on the left of its parent */
x->parent->left = y;
else
/* x must have been on the right */
x->parent->right = y;
/* Finally, put x on y's left */
y->left = x;
x->parent = y;
}
Insertion is somewhat complex and involves a number of cases. Note that we start by inserting the new
node, x, in the tree just as we would for any other binary tree, using the tree_insert function. This
new node is labelled red, and possibly destroys the red-black property. The main loop moves up the
tree, restoring the red-black property.
Algorithm to Insert a New Node
following tree.
Algorithm to Maintain Red-Black Property After Insertion
Case-I:
a. If 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.
f. Right-Rotate gP.
a. 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.
b. Assign gP to newNode.
c. Else if newNode is the left child of p then, assign p to newNode and Right-Rotate
newNode.
d. Set color of p as BLACK and color of gP as RED.
e. Left-Rotate gP.
Set the root of the tree as BLACK.
rb_insert( Tree T, node x ) {
/* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
}
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
}