# What Is Red-Black Tree in Data Structure With Example?

//

Angela Bailey

Red-Black Tree is a self-balancing binary search tree in data structure. It is named after its two main properties: each node is either red or black, and the path from the root to any leaf contains an equal number of black nodes. In this article, we will explore the intricacies of Red-Black Trees and illustrate their usage with an example.

## Properties of Red-Black Trees:

• Red or Black Nodes: Each node in a Red-Black Tree is either red or black.
• Root and Leaf Nodes: The root node and leaf nodes (NULL or empty) are always black.
• No Double Reds: No two adjacent nodes can be red. A red node cannot have a red parent or child.
• Equal Black Height: Every path from the root to any leaf must contain the same number of black nodes.

## Operations on Red-Black Trees:

### Insertion:

The insertion operation on a Red-Black Tree involves adding a new node while maintaining its balance. Here are the steps to follow:

1. If the tree is empty, create a new black root node.
2. If not empty, insert the new node as you would in any binary search tree, coloring it red initially.
3. If violating any properties, perform necessary rotations and recoloring to restore balance. This ensures that no two adjacent nodes are red, and all paths have equal black height.

### Deletion:

The deletion operation on a Red-Black Tree involves removing a node while preserving its properties. Here are the steps to follow:

1. If the node to be deleted has no children, simply remove it from the tree.
2. If the node has one child, replace it with its child.
3. If the node has two children, find its successor (smallest value in the right subtree), replace it with the successor’s value, and recursively delete the successor node.
4. Perform necessary rotations and recoloring to restore balance if needed.

## Example:

Let’s consider an example to better understand Red-Black Trees. We will insert nodes in ascending order: 1, 2, 3, 4, and 5.

Step 1: Insert node with value 1 as the root. Since it is the first node, it is colored black.

Step 2: Insert node with value 2 as a child of node 1. Since it violates property #3 (two adjacent red nodes), we perform a rotation and recoloring to restore balance. Node with value 1 becomes black while nodes with values 2 and NULL become red.

Step 3: Insert node with value 3 as a child of node 2. This insertion does not violate any properties initially.

Step 4: Insert node with value 4 as a child of node 3. Again, this insertion does not violate any properties initially.

Step 5: Insert node with value 5 as a child of node 4. Once again, this insertion does not violate any properties initially.

The resulting Red-Black Tree after all insertions is balanced and satisfies all properties.

## Conclusion:

Red-Black Trees are a powerful data structure that ensures efficient searching, insertion, and deletion operations while maintaining balance. Their self-balancing nature makes them suitable for various applications where dynamic data needs to be organized efficiently. Understanding their properties and operations is essential for leveraging their benefits in real-world scenarios.

By incorporating the principles of Red-Black Trees into your algorithms and programs, you can optimize performance and enhance the efficiency of your applications.