Rohini 15970278123
Rohini 15970278123
RED-BLACK TREES
Red-black trees are one of many search-tree schemes that are balanced to guarantee
that basic dynamic-set operations take O(lg n) time in the worst case.
A red-black tree is a binary search tree that satisûes the following red-black properties:
1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all simple paths from the node to descendant leaves contain
the same number of black nodes.
Insertion Logic
Insert the node similar to a binary tree and assign a red color to it. If the node is a
root node then change its color to black. If it is not then check the color of the parent node.
If its color is black then don’t change the color but if it is red then check the color of the
node’s uncle. If the node’s uncle is red then change the color of the node’s parent and uncle
to black and grandfather to red color. If grandfather is root then don’t change grandfather
to red color.
If the node’s uncle is black, then there are four possible cases
1. Left Left Rotation
Since the first element is the root node, the color of the node is changed as black
As Red Black tree is a Binary search tree, the new key 21 is checked with 3 and inserted
in right and as a new node it is colored as red.
32 is inserted to the right of 21 and colored as red. Two red nodes are not possible so it
needs a RR Rotation and we need to recolor to balance the tree. After rotating 21 becomes
the root node which is red. But a root node cannot be red, So we need to recolor it as black.
And 3 is the grandparent of 32 and hence recolor it as red.
15 is inserted to the right of 3 and colored as red. Two red nodes are not possible and as
the uncle node is red, change the color of parent and uncle as black.
DELETION
Deletion of a node in a red black tree takes O(lg n) time. Deleting a node from a
red-black tree is more complicated than inserting a node. The procedure for deleting a node
from a red-black tree is based on the TREE-DELETE procedure. RB-DELETE AND RB-
DELETE-FIXUP is used to perform deletion in Red-Black Tree.
RB-DELETE (T, z)
1y=z
2 y-original-color = y.color
3 if z.left = = T.nil
4 x = z.right
5 RB-TRANSPLANT (T, z, z.right ) // replace z by its right child
6 elseif z.right = = T.nil
7 x = z.left
RB-DELETE-FIXUP (T, x)
1 while x ≠ T.root and x.color == BLACK
2 if x == x.p.left // is x a left child?
3 w = x.p.right // w is x’s sibling
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 LEFT-ROTATE(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color D RED
11 x = x.p
12 else
13 if w.right.color == BLACK
14 w.left.color = BLACK
15 w.color = RED
16 RIGHT-ROTATE(T, w)
17 w = x.p.right
18 w.color = x.p.color
19 x.p.color = BLACK
20 w.right.color = BLACK
21 LEFT-ROTATE(T, x.p)
22 x = T.root
23 else // same as lines 3-22, but with “right” and “left” exchanged
24 w = x.p.left
25 if w.color == RED
26 w.color = BLACK
27 x.p.color = RED
28 RIGHT-ROTATE(T, x.p)
29 w = x.p.left
30 if w.right.color == BLACK and w.left.color == BLACK
31 w.color = RED
32 x = x.p
33 else
34 if w.left.color == BLACK
35 w.right.color = BLACK
36 w.color = RED
37 LEFT-ROTATE(T, w)
38 w = x.p.left
39 w.color = x.p.color
40 x.p.color = BLACK
41 w.left.color = BLACK
42 RIGHT-ROTATE(T, x.p)
43 x = T.root
44 x.color = BLACK
Example:
1 1
7 2 7 2
3
Delete 20: Node 20 is an internal node and has single child 30 which is red. Find the inorder
successor of the right subtree. Inorder successor is 30. So Replace the value 30 in the place
of 20 and delete leaf red node 30
1 1
7 2 7 3
Case 3: If the double black node is not a root node, then check the sibling of double
black node
If the double black nodes sibling and the children are black
Delete 20
1 1
5 2 5 3
1 3 1
Double Black Children is black and sibling 15 is also black, then
a. Remove double black
b. Add black to parent i.e) 30 is red which becomes black
c. Make the sibling as red i.e) 15 becomes red
1 1
5 3 5 3
1 n 1
5 2
1 7 1 3
15 is Double Black and its Children are black and so it becomes Double Black
5 2
1 7 3
1 7 n 3
Still Double Black exists in node 20
Check sibling and it is black and so check the root and transfer double black to parent and
make sibling red
5 2
1 7 n 3
Still Double Black exists in node 10. But it is the root. So just remove it
5 2
1 7 n 3
5 2
1 7 3
Action 2: Do the rotation towards the double black and reapply the cases.
Reapply the cases since there is double black and it follow the case 3
● Move the double black to parent 20 which is red and it becomes black
● Make the sibling 25 as red and remove the node to be deleted
Case 5: If a double black sibling is black and the children are not black i.e). one is red
and the red is near to double black
Delete 1
Double black Sibling 7 is black. So Move double black to parent 5 and make the sibling 7
as red and remove nil node
7 2 4
2 2
Still Double black exists. Now the double black node is 5 and the sibling of node 5 is
black and all children are not black. One of its children is red and it is near to Double
black.
Action 1: Swap the color of sibling 30 and sibling child 25.
7 2 4
2 2
Action 2: Do the rotation opposite to double black
This follows Case 6. Since the double black node’s sibling is black and one of its
children is red and it is far away from the double black node.
Action 1: Swap the color of parent and sibling of double black
Since the parent of double black 10 is black and the sibling 25 also black, it
doesn't make any change
1 3
Action 3: Remove the double black and mark the far red child as black