Red-Black Tree Insertion

This lesson will cover the insertion operation in Red-Black trees, discussing all the four insertion cases.

Insertion in Red-Black Tree

Here is a high-level description of the algorithm involved in inserting a value in a Red-Black Tree:

  1. Insert the given node using the standard BST Insertion technique that we studied earlier and color it Red.

  2. If the given node is the root, then change its color to Black

  3. If the given node is not the root, then we will have to perform some operations to make the Tree follow the Red-Black property.

Rebalancing the Tree

There are two ways to balance an unbalanced tree:

  1. Recoloring Nodes
  2. Rotating Nodes (left or right)

But before the details are explained, let’s define the structure of the Red-Black Tree and some nodes relative to the given node, which is the node that we inserted in the Tree.

  • Node C – the newly inserted node
  • Node P – the parent
...
Access this course and 1400+ top-rated courses and projects.