Red-black tree is a binary search tree which garanties searching, insertion and deletion in logarithmic time. As all extensions of BST it has additional properties which helps to keep tree balanced:

- A node has color either black or red.
- The root node is black.
- All leaves are black and don’t contain data. Sometimes to reduce memory consumption a single node performs the role of all leaves.
- Every red node must have two black child. This implies that on any path there can’t be two consecutive red nodes or in other words a red node can’t have a red child.
- Every path from a node to a descendant leaf contains the same number of black nodes. This number is called black height.

The best way to get the idea of these properties is an example. Let’s say we have inserted three elements in a plain vanilla binary search tree:

In order to be a red-black tree we need to fix all property violations that we have. The first property states that every node must be colored, so let’s mark all nodes as black and fix violation of the first property.

The second property is not violated now and we can skip it. The third property is violated because we don’t have empty leaves, so we must add missing leaves:

Since we don’t have the red nodes, the fourth property is not violated. But, the last property is violated and here is two possible cases to fix last violation:

In both cases I did left rotation on node one.

##### Operations

We’ll consider only insert and delete operations since all other operations are very similar to the binary search tree operations. Also, I found AVL tree balancing more natural than RB tree, but this is only my own opinion.

Apart of the terms which was described in binary search tree topic there are two more terms:

- Uncle.
- Grandparent.

You can find these relatives on the diagram below:

###### Insert

There is a small difference with BST insert: the inserted node must be colored in red and left and right leaves shall be connected to the sentinel node (in my implementation I use one NIL node for all leaves).

Once a new node is inserted we need to restore balance in the tree. To do that we have to focus on the properties which can be violated right after insertion:

[table ai=”1/No.”] Property,Can be violated, ReasonEvery node is either red or black.,No,We’ve inserted a red node so the property is not violated.

The root node is black.,Yes,If a new inserted node is root.

All leaves are black and don’t contain data.,No,Because a new node already has two empty leaf nodes.

Every red node must have two black child.,Yes,Possible violation if parent is red.

Every path from a node to a descendant leaf contains the same number of black nodes.,No,Because we’ve inserted a red node.

[/table]

Only two properties can be violated, to fix it we can use following algorithm:

*Pre-conditions for balancing:*

- A new node
**X**is inserted using binary search tree insert operation. **X**is red.**X**has two NIL children.

*Algorithm:*

- If
**X**is not root and**X**‘s parent is red go to step # 1.1.- If
**X**‘s parent and uncle are both red then go to step 1.1.1.- Make parent and uncle black.
- Make grandparent red.
- Set grandparent to
**X**. - Go to step 1.

- If grandparent exists go to step 1.2.1.
- If
**X**is right child of parent and parent is left child of grandparent go to step 1.2.1.1.- Make left rotation on parent.
- Set parent to
**X**. - Go to step 2.3.

- If
**X**is left child of parent and parent is right child of grandparent go to step 1.2.2.1.- Make right rotation on parent.
- Set parent to
**X**. - Go to step 2.3.

- Make parent black.
- Make grandparent red.
- If
**X**is left child of parent and parent is left child of grandparent then rotate right on grandparent otherwise rotate left on grand parent. - Go to step 1.

- If

- If
- Make root black.
- End.

###### Delete

Deletion is most difficult part, I spent a lot of time to understand and implement it. It’s difficult because we can remove a black node and this causes problems with black height. In case if we remove a red node the black height remains the same.

*Pre conditions for balancing:*

- Deleted node was black.
- Let
**X**be a node from which balancing is starting. - If deleted node was a leaf then set parent of deleted node to
**X**. - If node had at most one non-leaf child then
**X**is equal to this this child. - If node had two non-leaf children then
**X**must be equal to the right NIL node of deleted node.

*Ok, let’s get party started:*

- If
**X**is not root and black go to 1.1.- If
**X**is left child go to 1.1.1.- If sibling is red go to 1.1.1.1.
- Make sibling black
- Make parent red.
- Rotate left on parent.

- If sibling’s children are black go to 1.1.2.1.
- Make sibling red.
- Set
**X**equal to**X**‘s parent. - Go to step 1.

- If sibling right child is black go to 1.1.3.1.
- Make sibling’s left child black.
- Make sibling red.
- Rotate right on sibling.

- Make sibling color equal to
**X**‘s parent color. - Make
**X**‘s parent black. - Make sibling right child black.
- Rotate left on
**X**‘s parent. - Set
**X**equal to root. - Go to step 1.

- If sibling is red go to 1.1.1.1.
- If
**X**is right child go to 1.2.1 .- The same steps that below 1.1 but left changed to right and vise versa.

- Go to step 1.

- If
- Make root black.
- End.

###### Implementation

You can find my implementation of the red-black tree on GitHub: https://github.com/alexvolov/algorithms-and-data-structures/blob/master/src/main/java/com/alexvolov/ads/ds/impl/RedBlackTree.java.

##### AVL tree vs. Red-black tree

On the internet you can find information and proves on why AVL trees are more rigidly balanced than red-black trees. Anyway, as a result of this in worst cases RBT’s searching is little bit slower, but insertion and deletion operations require lesser rotations.

In practice red-black trees are commonly used, for instance, Java TreeMap class and Linux kernel use this type of the tree.