You are on page 1of 61

Red-Black Trees

Red-Black tree
Recall binary search tree
Key values in the left subtree <= the node value
Key values in the right subtree >= the node value
Operations:
insertion, deletion
Search, maximum, minimum, successor,
predecessor.
O(h), h is the height of the tree.
Red-Black Tree
A red-black tree is a binary search tree with the
following properties:
Red-black properties:
1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4.All simple paths from any node x to a
descendant leaf have the same number of black
nodes = black - height(x).

This data structure requires an extra one-bit color


field in each node.
Red-Black Trees
Constraints on the coloring and connection of nodes ensure that
no root to leaf path is more than twice as long as any other, so
tree is approximately balanced.
Each RBT node contains fields left, right, parent, color, and key.

L PARENT R
I
E KEY G
F H
T COLOR T
Red-Black Tree

1. Every Node is either Red or Black


Red-Black Tree

2. Root and the Leaves NIL’s are Black


Red-Black Tree

3. If the Node Is Red then its Parent is Black


Red-Black Tree
4. All simple paths from any node x to a
descendant leaf have the same number
of black nodes = black-height (x).
Red-Black Tree

1. Every Node is either Red or Black


2. Root and the Leaves NIL’s are Blank
3. If the Node Is Red then its Parent is Black
4. All simple paths from any node x to a descendant leaf have the same number of
black nodes = black-height (x).
Red-Black Tree
Height and Black Height
3

2 6

4 9

5 11

7
Height Lemma
A red-black tree with n internal nodes has height ≤ 2 lg(n+1).

Proof:
1) any node with height h has black-height ≥ h/2.
2) the subtree rooted at any node x contains ≥ 2bh(x) – 1 internal
nodes.
Proof by induction on height of x.

Let h and b be the height and black-height of the root,


respectively.
By the above 2 claims,
n ≥ 2b – 1 ≥ 2h/2 – 1
Adding 1 to both sides and taking logs gives
lg(n+1) ≥ h/2, which implies
h ≤ 2 lg(n+1).
Height Lemma
Height Lemma
Height Lemma
Height Lemma
Height Lemma
Height Lemma
Height Lemma
Height Lemma
We have h′≥ h/2, since at most half the leaves on any path
are red.
The number of leaves in each tree is n + 1
n + 1 ≥ 2h'
lg(n + 1) ≥ h' ≥ h/2
h ≤ 2 lg(n + 1).
Operations
Corollary:
The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR
all run in O(lg n) time on a red-black tree with n nodes.

The operations INSERT and DELETE cause


modifications to the red-black tree:
the operation itself,
color changes,
restructuring the links of the tree via“rotations”.
Rotations
Restructuring a red-black tree can be done with only one
operation (and its symmetric version) – rotation.
moves a subtree from one side to another.
Won’t upset the binary-search-tree property.
A rotation can be performed in O(1) time – Since Constant
Number of Pointers are modified
Example Of Rotation
Insert Into RB tree
Steps Involved..
Start by doing regular binary-search-
tree insertion
RB-INSERT ends by coloring the new
node z red.
Then calls RB-INSERT-FIXUP because
we could have violated a red-black
property.
4

2 5
Binary Search Tree

1 3
Insert Into RB Tree
Violation to the RB Tree Characteristics – Because
of the Insertion…The Violation can be on any of the
below RB Tree properties..

1. Every node is either red or black.


2. The root is black.
3. Every leaf (nil[T]) is black.
4. If a node is red, then both its children are black. (Hence
no two reds in a row on a simple path from the root to a
leaf.)
5. For each node, all paths from the node to descendant
leaves contain the same number of black nodes.
Insert Into RB Tree Algorithm
Insert the node ’15’ in the Below RB Tree……………
Insert Into RB Tree Algorithm

Violation of the “Red-black properties:”


1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4.All simple paths from any node x to a descendant leaf have the same number
of black nodes = black -height(x).
Insert Into RB Tree Algorithm

Violation of the “Red-black properties:”


1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4.All simple paths from any node x to a descendant leaf have the same number
of black nodes = black -height(x).
Insert Into RB Tree Algorithm

Violation of the “Red-black properties:”


1. Every node is either red or black.
2. The root and leaves (NIL’s) are black.
3. If a node is red, then its parent is black.
4.All simple paths from any node x to a descendant leaf have the same number
of black nodes = black -height(x).
Insert Into RB Tree Algorithm
Insert Into RB Tree Algorithm
Insert Into RB Tree Algorithm
Insert Into RB Tree Algorithm
Insert Into RB Tree Algorithm
Insert Into RB Tree Algorithm

Case 1: ‘z’s uncle ‘y’ is Red

Case 2: ‘z’s uncle ‘y’ is Black and ‘z’ is a Right


Child

Case 3: ‘z’s uncle ‘y’ is Black and ‘z’ is a Left


Child
Insert Into RB Tree Algorithm
Case 1: ‘z’s Uncle/Aunt ‘y’ is Red

Recolor
z

z
Insert Into RB Tree Algorithm
Case 2: ‘z’s Uncle/Aunt ‘y’ is Black and ‘z’ is a Right Child

C
y y
B
z z A
Insert Into RB Tree Algorithm
Case 3: ‘z’s uncle ‘y’ is Black and ‘z’ is a Left Child

z
Analysis of Insert operation in
a RB Tree
O(lg n) time to get through RB-INSERT up to the call of RB-
INSERT-FIXUP.
Within RB-INSERT-FIXUP :
Each iteration takes O(1) time.
Each iteration is either the last one or it moves z up 2 levels.
O(lg n) levels -> O(lg n) time.
Also note that there are at most 2 rotations overall.

Thus, insertion into a red-black tree takes O(lg n) time.


One More Example

Insert nodes in the sequence in the RB Tree – 50 40 30 15 20 10

40

20 50

15 30

10
One More Example

11 Steps:

BST Insert
2 14 Case 1
Case 2
Case 3
1 7 15

5 8

Insert node “4”


One More Example

2 11

1 5
8 14

4 15

Solution
Deletion
Deletion, like insertion, should preserve all the
RB properties.
The properties that may be violated depends on
the color of the deleted node.
» Red – OK. Why?
» Black?
Steps Involved are:
» Do regular BST deletion.
» Fix any violations of RB properties that may result.
Violation of RB Properties
If the node supposed to be deleted “Red-black properties:”
‘y ‘ is black, we could have 1. Every node is either red or
violations of redblack black.
properties:
2. The root is black.
Prop. 1. OK.
3. Every leaf (nil[T]) is black.
Prop. 2. If y is the root and x is red,
then the root has become red. 4. If a node is red, then both
its children are black.
(Hence no two reds in a
Prop. 3. OK.
row on a simple path from
the root to a leaf.)
Prop. 4. Violation if p[y] and x are
both red.
5. For each node, all paths
from the node to
Prop. 5. Any path containing y now descendant leaves contain
has 1 fewer the same number of black
black node. nodes.
Violation of RB Properties
RB Delete Fixup Algorithm
Case -1 “x’s Sibling ‘w’ is RED

1. ‘w’ must have black children.


2. Make w black and p[x] red.
3. Then left rotate on p[x].
4. New sibling of x was a child of w before rotation -> must be black.
Go immediately to case 2, 3, or 4.
Case -2

1. Take 1 black off x (-> singly black) and off w (-> red).
Move that black to p[x]
2. Do the next iteration with p[x] as the new x.

INFO: If entered this case from case 1, then p[x] was red ->
new x is red & black -> color attribute of new x is RED -> loop
terminates. Then new x is made black in the last line.
Case -3

1. Make w red and w’s left child black.


2. Then right rotate on w.
3. New sibling w of x is black with a red right child -> case 4.
Case -4

1. Make w be p[x]’s color (c).


2. Make p[x] black and w’s right child black.
3. Then left rotate on p[x].
4. Remove the extra black on x (-> x is now singly black)
without violating any red-black properties.
All done. Setting x to root causes the loop to terminate
Example

15

10

5 12

3 7 11 13 Valid and
2 4 6 9 unaffected
Right subtree
Delete 11 from RB Tree
Example

15

10

5 12 Valid and unaffected


Right subtree

3 7 11 13
x w Step 1 - Case 2
2 4 6 9

Delete 11 from RB Tree


Example

15

10

5 12 Valid and unaffected


x Right subtree

3 7 11 13
Step 1 - Case 2
2 4 6 9

Delete 11 from RB Tree


Example

15

10

5 12 Valid and unaffected


x Right subtree

3 7 11 13
Step 2 - Case 1
2 4 6 9

Delete 11 from RB Tree


Example

15

10

5 w 12 Valid and unaffected


x Right subtree

3 7 11 13
Step 2 - Case 1
2 4 6 9

Delete 11 from RB Tree


Example

15

Right Rotate 10

5 w 12 Valid and unaffected


x Right subtree

3 7 11 13
Step 2 - Case 1
2 4 6 9

Delete 11 from RB Tree


Example

15

3 10 Valid and unaffected


Right subtree

2 4 7 w 12 x

Step 2 - Case 2
6 9 11 13

Delete 11 from RB Tree


Example

15

3 10 x Valid and unaffected


Right subtree

2 4 7 12
Step 2 - Case 2
6 9 11 13

Delete 11 from RB Tree


Example

15

3 10 x Valid and unaffected


Right subtree

2 4 7 12
Step 3 –
6 9 13 if ‘x’ is Red –
Come out of While Loop
And Recolor ‘x’ to Black

Delete 11 from RB Tree


Analysis of the Delete
Operation in RB Tree
O(lg n) time to get through RB-DELETE up to
the call of RB-DELETE-FIXUP.

Within RB-DELETE-FIXUP :
Case 2 is the only case in which more iterations
occur.
x moves up 1 level.
Hence, O(lg n) itrations.
Each of cases 1, 3, and 4 has 1 rotation leads to
≤ 3 rotations in all.
Hence, O(lg n) time.

You might also like