Red Black Tree Java
Red Black Tree is a special type of binary search tree that has selfbalancing behavior. Each node of the RedBlack Tree has an extra bit, which is always interpreted as color. In order to maintain the balancing of the RedBlack Tree during insertion, updation, and deletion, these red and black colors are used. In Red Black Tree:
 Each node should have either the color red or black.
 The root node should always be black.
 A red node should not have a parent and child having red color.
 Each path from a node to any of its descendant's NULL nodes has the same number of black nodes.
Algorithm to insert an element in Red Black Tree
When we insert a node in the RedBlack Tree, it always inserts with color bit 0, i.e., Red. In case when the RedBlack Tree violates its property after inserting a node, we should have to perform the following two operation.
 Maintaining the colors of the node.
 Perform the rotation either by the right to left or left to right.
The algorithm of inserting a node in the RedBlack Tree is given below.
 Let x and y are the root and leaf node of the RedBlack Tree.
 Check whether the root node is empty or not. If the root node is not empty, the inserting node will be added as a root node with color bit 1, i.e., black.
 Else, we will compare the root node element with the inserting node element. If it is greater than the root node element, traverse through the right subtree else, traverse the left subtree.
 Repeat steps 3 until the leaf node is not reached.
 Make leaf node's parent as a parent of inserting node.
 If the leaf node element is lesser than the inserting node element, make inserting node as leftChild.
 Else, make inserting node as rightChild.
 For the right and left children of that inserting node, we assign NULL to both of them.
 Set bit 0, i.e., Red color to that newly inserted node.
 Maintain the property of the redblack tree if violated by performing rotation and changing color bits.
Algorithm to maintain Red Black Tree after insertion
If the inserting node violets the property of the Red Block Tree, we need to maintain it by using the following algorithm:
 Perform the following steps until the parent p of the inserted node become RED.
 If the parent of the inserted node is the left child of the grandparent node of z, perform the following steps:
 Case1:
 When the color of the right child of z's grandparent is RED, make both the grandparent node's children BLACK and make the grandparent as RED.
 Assign the grandparent node to the newly inserted node
 Case2:
 Else if the newly inserted node is the right child of the parent node p, assign p to the newly inserted node.
 Perform the leftrotation for the newly inserted node.
 Case3:
 Make parent node p as BLACK and grandparent node as RED.
 Performs the rightrotation for that newly inserted node.
 Else, perform the following steps.
 If the left child of z's grandparent node is RED, make both the grandparent node's children BLACK and the grandparent node RED.
 Assign grandparent node to the newly inserted node.
 Else if the newly inserted node is the left child of the parent p, assign the parent node to the newly inserted node and perform the right rotation for that new node.
 Make parent node p as BLACK and grandparent node as RED.
 Perform the left rotation for the grandparent node.
 Make the root node BLACK.
RedBlackTreeExample.java
Output
