# Red-Black Tree

#### In this tutorial, you will learn what a red-black tree is. Also, you will find working examples of various operations performed on a red-black tree in C.

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

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:

### 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:
2. If y has a left subtree, assign x as the parent of the left subtree of y. Assign x as the parent of the left subtree of y
3. If the parent of x is `NULL`, make y as the root of the tree.
4. Else if x is the left child of p, make y as the left child of p.
5. Else assign y as the right child of p.
6. Make y as the parent of x.

### 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:
2. If x has a right subtree, assign y as the parent of the right subtree of x. Assign y as the parent of the right subtree of x
3. If the parent of y is `NULL`, make x as the root of the tree.
4. Else if y is the right child of its parent p, make x as the right child of p.
5. Else assign x as the left child of p.
6. Make x as the parent of y.

### 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.
2. 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.
2. 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

### Algorithm to insert a node

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

1. Let y be the leaf (ie. `NIL`) and x be the root of the tree.
2. Check if the tree is empty (ie. whether x is `NIL`). If yes, insert newNode as a root node and color it black.
3. 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.
4. Assign the parent of the leaf as a parent of newNode.
5. If leafKey is greater than newKey, make newNode as rightChild.
6. Else, make newNode as leftChild.
7. Assign `NULL` to the left and rightChild of newNode.
8. Assign RED color to newNode.
9. 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 the insertion of a newNode violates this property.

1. Do the following while the parent of newNode p is RED.
2. If p is the left child of grandParent gP of z, do the following.
Case-I:

1. If the color of the right 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.
Case-II:
3. Else if newNode is the right child of p then, assign p to newNode.
4. Left-Rotate newNode.
Case-III:
5. Set color of p as BLACK and color of gP as RED.
6. Right-Rotate gP.
3. Else, do the following.
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.
4. Set the root of the tree as BLACK.

### 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.

### Algorithm to delete a node

1. Save the color of nodeToBeDeleted in origrinalColor.
2. If the left child of nodeToBeDeleted is `NULL`
1. Assign the right child of nodeToBeDeleted to x.
2. Transplant nodeToBeDeleted with x.
3. Else if the right child of nodeToBeDeleted is `NULL`
1. Assign the left child of nodeToBeDeleted into x.
2. Transplant nodeToBeDeleted with x.
4. 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.
5. 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 recoloring are performed.

The 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.
2. If the right child of parent 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.
3. Left-Rotate the parent of x.
4. Assign the rightChild of the parent of x to w.
3. 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.
4. 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
3. Right-Rotate w.
4. Assign the rightChild of the parent of x to w.
5. 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 x as BLACK.
3. Set the color of the right child of w as BLACK.
4. Left-Rotate the parent of x.
5. Set x as the root of the tree.
3. Else the same as above with right changed to left and vice versa.
4. Set the color of x as BLACK.

Please refer to insertion and deletion operations for more explanation with examples.

## C Examples

``````// Implementing Red-Black Tree in C

#include <stdio.h>
#include <stdlib.h>

enum nodeColor {
RED,
BLACK
};

struct rbNode {
int data, color;
};

struct rbNode *root = NULL;

// Create a red-black tree
struct rbNode *createNode(int data){
struct rbNode *newnode;
newnode = (struct rbNode *)malloc(sizeof(struct rbNode));
newnode->data = data;
newnode->color = RED;
return newnode;
}

// Insert an node
void insertion(int data){
struct rbNode *stack, *ptr, *newnode, *xPtr, *yPtr;
int dir, ht = 0, index;
ptr = root;
if (!root) {
root = createNode(data);
return;
}

stack[ht] = root;
dir[ht++] = 0;
while (ptr != NULL) {
if (ptr->data == data) {
printf("Duplicates Not Allowed!!n");
return;
}
index = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
dir[ht++] = index;
}
stack[ht - 1]->link[index] = newnode = createNode(data);
while ((ht >= 3) && (stack[ht - 1]->color == RED)) {
if (dir[ht - 2] == 0) {
if (yPtr != NULL && yPtr->color == RED) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 0) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
}
xPtr = stack[ht - 2];
xPtr->color = RED;
yPtr->color = BLACK;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
} else {
if ((yPtr != NULL) && (yPtr->color == RED)) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 1) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
}
xPtr = stack[ht - 2];
yPtr->color = BLACK;
xPtr->color = RED;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
}
}
root->color = BLACK;
}

// Delete a node
void deletion(int data){
struct rbNode *stack, *ptr, *xPtr, *yPtr;
struct rbNode *pPtr, *qPtr, *rPtr;
int dir, ht = 0, diff, i;
enum nodeColor color;

if (!root) {
printf("Tree not availablen");
return;
}

ptr = root;
while (ptr != NULL) {
if ((data - ptr->data) == 0)
break;
diff = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
dir[ht++] = diff;
}

if ((ptr == root) && (ptr->link == NULL)) {
free(ptr);
root = NULL;
} else if (ptr == root) {
free(ptr);
} else {
}
} else {
color = xPtr->color;
xPtr->color = ptr->color;
ptr->color = color;

if (ptr == root) {
root = xPtr;
} else {
stack[ht - 1]->link[dir[ht - 1]] = xPtr;
}

dir[ht] = 1;
stack[ht++] = xPtr;
} else {
i = ht++;
while (1) {
dir[ht] = 0;
stack[ht++] = xPtr;
break;
xPtr = yPtr;
}

dir[i] = 1;
stack[i] = yPtr;
if (i > 0)
stack[i - 1]->link[dir[i - 1]] = yPtr;

if (ptr == root) {
root = yPtr;
}

color = yPtr->color;
yPtr->color = ptr->color;
ptr->color = color;
}
}

if (ht < 1)
return;

if (ptr->color == BLACK) {
while (1) {
pPtr = stack[ht - 1]->link[dir[ht - 1]];
if (pPtr && pPtr->color == RED) {
pPtr->color = BLACK;
break;
}

if (ht < 2)
break;

if (dir[ht - 2] == 0) {

if (!rPtr)
break;

if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;

if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 0;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;

}

rPtr->color = RED;
} else {
rPtr->color = RED;
qPtr->color = BLACK;
rPtr = stack[ht - 1]->link = qPtr;
}
rPtr->color = stack[ht - 1]->color;
stack[ht - 1]->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
break;
}
} else {
if (!rPtr)
break;

if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;

if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 1;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;

}
rPtr->color = RED;
} else {
rPtr->color = RED;
qPtr->color = BLACK;
rPtr = stack[ht - 1]->link = qPtr;
}
rPtr->color = stack[ht - 1]->color;
stack[ht - 1]->color = BLACK;
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
break;
}
}
ht--;
}
}
}

// Print the inorder traversal of the tree
void inorderTraversal(struct rbNode *node){
if (node) {
printf("%d  ", node->data);
}
return;
}

// Driver code
int main(){
int ch, data;
while (1) {
printf("1. Insertiont2. Deletionn");
printf("3. Traverset4. Exit");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to insert:");
scanf("%d", &data);
insertion(data);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &data);
deletion(data);
break;
case 3:
inorderTraversal(root);
printf("n");
break;
case 4:
exit(0);
default:
printf("Not availablen");
break;
}
printf("n");
}
return 0;
}``````

## Red-Black Tree Applications

1. To implement finite maps
2. To implement Java packages: `java.util.TreeMap` and `java.util.TreeSet`
3. To implement Standard Template Libraries (STL) in C++: multiset, map, multimap
4. In Linux Kernel

# Special 95% discount

## 2000+ Applied Machine Learning & Data Science Recipes

### Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R ## Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  `