Javatpoint Logo
Javatpoint Logo

Red Black Tree Java

Red Black Tree is a special type of binary search tree that has self-balancing behavior. Each node of the Red-Black Tree has an extra bit, which is always interpreted as color. In order to maintain the balancing of the Red-Black Tree during insertion, updation, and deletion, these red and black colors are used. In Red Black Tree:

  1. Each node should have either the color red or black.
  2. The root node should always be black.
  3. A red node should not have a parent and child having red color.
  4. Each path from a node to any of its descendant's NULL nodes has the same number of black nodes.
Red Black Tree Java

Algorithm to insert an element in Red Black Tree

When we insert a node in the Red-Black Tree, it always inserts with color bit 0, i.e., Red. In case when the Red-Black Tree violates its property after inserting a node, we should have to perform the following two operation.

  1. Maintaining the colors of the node.
  2. Perform the rotation either by the right to left or left to right.

The algorithm of inserting a node in the Red-Black Tree is given below.

  1. Let x and y are the root and leaf node of the Red-Black Tree.
  2. 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.
  3. 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.
  4. Repeat steps 3 until the leaf node is not reached.
  5. Make leaf node's parent as a parent of inserting node.
  6. If the leaf node element is lesser than the inserting node element, make inserting node as leftChild.
  7. Else, make inserting node as rightChild.
  8. For the right and left children of that inserting node, we assign NULL to both of them.
  9. Set bit 0, i.e., Red color to that newly inserted node.
  10. Maintain the property of the red-black 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:

  1. Perform the following steps until the parent p of the inserted node become RED.
  2. If the parent of the inserted node is the left child of the grandparent node of z, perform the following steps:
    1. Case-1:
      1. 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.
      2. Assign the grandparent node to the newly inserted node
    2. Case-2:
      1. Else if the newly inserted node is the right child of the parent node p, assign p to the newly inserted node.
      2. Perform the left-rotation for the newly inserted node.
    3. Case-3:
      1. Make parent node p as BLACK and grandparent node as RED.
      2. Performs the right-rotation for that newly inserted node.
  3. Else, perform the following steps.
    1. If the left child of z's grandparent node is RED, make both the grandparent node's children BLACK and the grandparent node RED.
    2. Assign grandparent node to the newly inserted node.
    3. 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.
    4. Make parent node p as BLACK and grandparent node as RED.
    5. Perform the left rotation for the grandparent node.
  4. Make the root node BLACK.

RedBlackTreeExample.java

Output

Red Black Tree Java
Red Black Tree Java
Red Black Tree Java
Red Black Tree Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA