Red Black Tree
Red black tree is a self balanced binary search tree invented by Rudolf Bayer in 1972. It is also called as 'symmetric binary B tree'. It has good worst case running time for its operations. It performs all the operations such as searching, insertion, deletion, in O(log n ) time where n is the number of nodes in the tree. In Red black tree, no data is stored at the leaves.
Properties of Red Black Tree
Operations on Red Black Tree
Performing Traversing and searching (read-only) operations in Red Black Tree is similar to Binary search tree. However, insertion and deletion operations require the modifications in the tree structure and we may need to rearrange the tree.
In red black tree, there are some basic terms that are used to simulate a tree with a human family.
Grandfather (G) of a node (N) refers to the parent of parent(N). The C code to find the grandfather of a node n is given as follows.
Uncle node of a node n is the sibling of N's parent. The C code to find the uncle of a given node N is given as follows.
Inserting a new node in red black tree is similar to Binary search tree except the difference that here, the new node is inserted as an interior red node having two blank black leaf nodes. Here, we must notice that the color of the new node is red.
A new node may violate the properties of Red Black tree.
To restore the properties of the Red black tree after insertion, we check for certain cases and we need to look for the case that turns up after insertion.
Case 1: New Node is added as the root of the tree
If the parent of the new node is NULL, the new node will be considered as the root node of the tree. Due to the properties of red black tree, the root node of the tree will always be black, therefore if the new node of the tree doesn't have any parent, we need to re-colour it as black.
Case 2: Parent of new node is black
In this case, both children of red node are black therefore property 4 is not invalidated. The path towards the leaves will contain the same number of black node since the new node is red.
Case 3: Parent and uncle of new node are red
Inserting a new node having parent and uncle of red color, may violate the property that states, every path from root to the leaves will contain the same number of black nodes.
In order to restore the Red Black tree property, we must recolor the parent P and uncle U of N to black and make the grandfather of N to red.
However, It may violate the property which states that the root of the tree will always be black. Therefore, Call the first step of the process on the tree obtained in the third step, recursively. This will make the grandfather of N to black.
The C Code to insert a node in case 3 is defined as follows.
Case 4: Parent (P) is red but uncle(U) is black, new node (n) is inserted as the right child of P, P is the left child of G:
In this case, N is left rotated and becomes the left child of G, here P becomes the left child of N. This state of Tree violate the property i.e. both the children of a red node must be black. In order to restore the property, we must call step 5 in order to right rotate P, so that P can become the new root node of the tree having N and G, its left and right children respectively. Now, all the property holds and the tree becomes balanced Red black tree.
Case 5:Parent (P) is red but uncle(U) is black, new node (N) is inserted as the left child of P, P is the left child of G:
In this case, the P is right rotated to become the new root node of the tree, having N and G as its left and right child respectively. The procedure is described in the following image.
The C Code illustrating Case 5 is described as follows.
A node is deleted in the same way as it is deleted in Binary search tree. However, we perform rotations and recolouring in order to maintain the Red black tree properties.
There are various cases of deletion depending upon the different color of sibling of the node being deleted. However, deleting a node from a red black tree may violate the black-height property. There are the following steps to delete a node in Red black tree:
Perform BST delete operation
In BST Delete, there are various cases depending upon the number of children the node have. If it is the leaf node, It will be deleted simply. If it contains one children, It can simply be replaced with its child.
However, if it contains two children, we need to find out its in-order successor or predecessor and replace that with the node to be deleted in order to let the node be the leaf node so that we can delete the node according to case 1.
Case 1: N is the root
In this case, one black node is removed from every path and the new root is black, therefore no property is violated and therefore no need of any rotation.
Case 2: Sibling S is Red
This is the case where sibling S of new node is Red, therefore we will left rotate the sibling S, in order to make the node S, the new root of the tree, having P and R as its left and right children respectively.
The C code is shown as follows.
Case 3: Parent (P), Sibling (S) and its children are black
Repaint S with red color therefore, all the paths passing through S will now have one less black node. Therefore, the property that states, All the paths from root to leaves will contain the same number of black nodes, is violated. To fix this problem, rebalancing procedure of case 1 is called recursively on the resultant tree produced in case 3.
Case 4: S and its children are black, P is red.
In this case, interchange the colors of P and S to make the P (root of the tree) black and sibling S red. It will not affect the number of black nodes on the path going through N, making up for the deleted black node on the path.
on the paths that are passing through N. The C Code is given as follows.
Case 5: N is the left child of P, S is black, S's left child is red, S's right child is black.
This case needs right rotation on S so that the left child of S can become the Parent of S with R as its right child. The colors of S is interchanges with its parent. The black height of the tree will not be affected but N will now have a black sibling with its right child as red therefore we must consider into case 6.
Case 6: S is black, S's right child is red, N is left child of its parent P.
The left rotation is applied to P to make S, the parent of P and S's right child R. The color of P and S are interchanged. The Right child of S is coloured black.