Discuss the insertion operation in Red Black tree.


Q.) Discuss the insertion operation in Red Black tree.

Subject: Data Structures

I. Introduction to Red Black Tree

A Red-Black Tree is a kind of self-balancing Binary Search Tree (BST) where each node has an extra bit for denoting the color of the node, either red or black. A Red-Black Tree maintains balanced during insertions and deletions.

Properties of Red Black Tree:

  1. Every node is either red or black.
  2. The root is black. This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice versa, this rule has little effect on analysis.
  3. All leaves (NIL) are black.
  4. If a node is red, then both its children are black.
  5. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.

II. Basics of Insertion Operation in Red Black Tree

In a Red-Black Tree, the insertion operation is performed in a similar way as it is in a normal Binary Search Tree but we insert every new node as a red node. After insertion, we check whether the Red-Black Tree properties are violated or not and fix those violations.

III. Detailed Steps of Insertion Operation

Step 1: Standard BST Insertion

The new node is always inserted at the leaf. We start from the root of the tree, and based on whether the new key to be inserted is less or greater than the root, we decide whether to go left or right. If the new key to be inserted is less than the root, then we go left, else we go right. We continue this process until we hit a leaf node.

Step 2: Color the Node Red

We always color the new node red. This is because the number of black nodes on all paths from the root to the leaves is not affected by this.

Step 3: Fix Red Black Tree Properties

If the parent of the new node is black, then there's no need to do anything. However, if the parent is red, then there are several cases to consider.

IV. Cases to Fix Red Black Tree Properties

Case 1: Uncle is Red (Same as parent)

In this case, we flip the colors of the parent and uncle to black. The grandparent becomes red. Then we check if the grandparent violates any Red-Black Tree properties.

Case 2: Uncle is Black and new node is right child

In this case, we perform a left rotation on the parent. After the rotation, the former parent is now the left child of the new node. This transforms the situation into Case 3.

Case 3: Uncle is Black and new node is left child

In this case, we perform a right rotation on the grandparent. After the rotation, the former parent is now the parent of the new node and the former grandparent. We then swap the colors of the new parent and the new grandparent.

V. Program to Implement Insertion Operation in Red Black Tree

Here is a Python program that implements the insertion operation in a Red-Black Tree:

class Node:
    def __init__(self, data):
        self.data = data
        self.parent = None
        self.left = None
        self.right = None
        self.color = 1

class RedBlackTree:
    def __init__(self):
        self.NIL = Node(0)
        self.NIL.color = 0
        self.NIL.left = None
        self.NIL.right = None
        self.root = self.NIL

    def insert(self, key):
        # Ordinary Binary Search Insertion
        node = Node(key)
        node.parent = None
        node.data = key
        node.left = self.NIL
        node.right = self.NIL
        node.color = 1

        y = None
        x = self.root

        while x != self.NIL:
            y = x
            if node.data < x.data:
                x = x.left
            else:
                x = x.right

        node.parent = y
        if y is None:
            self.root = node
        elif node.data < y.data:
            y.left = node
        else:
            y.right = node

        if node.parent is None:
            node.color = 0
            return

        if node.parent.parent is None:
            return

        self.fix_insert(node)

    def fix_insert(self, k):
        while k.parent.color == 1:
            if k.parent == k.parent.parent.right:
                u = k.parent.parent.left
                if u.color == 1:
                    u.color = 0
                    k.parent.color = 0
                    k.parent.parent.color = 1
                    k = k.parent.parent
                else:
                    if k == k.parent.left:
                        k = k.parent
                        self.right_rotate(k)
                    k.parent.color = 0
                    k.parent.parent.color = 1
                    self.left_rotate(k.parent.parent)
            else:
                u = k.parent.parent.right

                if u.color == 1:
                    u.color = 0
                    k.parent.color = 0
                    k.parent.parent.color = 1
                    k = k.parent.parent
                else:
                    if k == k.parent.right:
                        k = k.parent
                        self.left_rotate(k)
                    k.parent.color = 0
                    k.parent.parent.color = 1
                    self.right_rotate(k.parent.parent)
            if k == self.root:
                break
        self.root.color = 0

VI. Examples of Insertion Operation in Red Black Tree

Let's consider an example where we insert the keys 7, 6, 5, 4, 3, 2, 1 into an initially empty Red-Black Tree.

  1. Insert 7. Since it's the first node, it will be the root and colored black.
  2. Insert 6. It's the left child of 7 and colored red.
  3. Insert 5. It's the left child of 6. Now, we have two consecutive reds. We perform a right rotation on 7 and recolor 6 and 7. The tree becomes balanced.
  4. Insert 4. It's the left child of 5 and colored red.
  5. Insert 3. It's the left child of 4. Now, we have two consecutive reds. We perform a right rotation on 6 and recolor 4 and 6. The tree becomes balanced.
  6. Insert 2. It's the left child of 3 and colored red.
  7. Insert 1. It's the left child of 2. Now, we have two consecutive reds. We perform a right rotation on 5 and recolor 2 and 5. The tree becomes balanced.

VII. Conclusion

The insertion operation in a Red-Black Tree involves standard BST insertion, coloring the new node red, and fixing any violations of the Red-Black Tree properties. It's important to maintain the properties of the Red-Black Tree during the insertion operation to ensure that the tree remains balanced. This guarantees that the tree has an upper bound of O(log n) for all major operations.

Summary

A Red-Black Tree is a self-balancing Binary Search Tree where each node has an extra bit for denoting the color of the node, either red or black. The insertion operation in a Red-Black Tree involves standard BST insertion, coloring the new node red, and fixing any violations of the Red-Black Tree properties. It's important to maintain the properties of the Red-Black Tree during the insertion operation to ensure that the tree remains balanced.

Analogy

A Red-Black Tree is like a traffic light system for a city. Each intersection (node) has a traffic light that can be either red or black. The traffic lights are strategically placed to ensure smooth traffic flow and prevent congestion. Similarly, in a Red-Black Tree, the color of each node is used to balance the tree and maintain efficient search and insertion operations.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is a Red-Black Tree?
  • A type of self-balancing Binary Search Tree
  • A type of unbalanced Binary Search Tree
  • A type of AVL Tree
  • A type of Heap