Red Black Tree
Red Black Tree
3.4 – 3.7
Team names
Majed Suhaim
Ahmed Sulaiman M Alharbi
RED-BLACK TREES
- Each path from the root to a leaf contains the same number of black nodes
bh =2
7
bh =1 3 18 bh =2
bh =1 10 22 bh =1
bh =1 8 11 26 bh =1
Rotation
A rotation is a local operation in a search tree that preserves in-order traversal
key ordering.
Bottom-Up Rebalancing for Red-Black Trees
* The idea for insertion in a red-black tree is to insert like in a binary search tree
and then reestablish the color properties through a sequence of recoloring and
rotations
The rules are as follows:
1. If other is red, color current and other black and upper red.
2. If current = upper->left
2.1 If current->right->color is black,
perform a right rotation around upper and color upper->right
red.
2.2 If current->right->color is red,
perform a left rotation around current followed by a right rotation
around upper, and color upper->right and upper->left
black and upper red.
3. If current = upper->right
3.1 If current->left->color is black,
perform a left rotation around upper and color upper->left red.
3.2 If current->left->color is red,
perform a right rotation around current followed by a left rotation
around upper, and color upper->right and upper->left
black and upper red.
* We have 3 cases for insertion
G G
P U P U
Case 2:
Double Rotate: X around P then X around G.
Recolor G and X
G X
P P
U G
X
S
U
S
Case 3:
Single Rotate P around G
Recolor P and G
P
P U
X
G
S
X
S U
Analysis of Insertion
- A red-black tree has O(log n) height
- Search for insertion location takes O(log n) time because we
visit O(log n) nodes
- Addition to the node takes O(1) time
- Rotation or recoloring takes O(log n) time because we
perform
* O(log n) recoloring, each taking O(1) time, and
* at most one rotation taking O(1) time
- Thus, an insertion in a red-black tree takes O(log n) time
• Deleting a node from a red-black tree is a bit more
complicated than inserting a node.
delete
U S
Case A:
- V’s sibling, S, is Red
Rotate S around P and recolor S & P
S
P
Rotate S around P
P
V S
S
Recolor S &
P
P
V
P
delete S
U
Case B:
- V’s sibling, S, is black and has two black children.
Recolor S to be Red
S V S
V
P
delete S
U
Case C:
- S is black
S’s RIGHT child is RED (Left child either color)
Rotate S around P
Swap colors of S and P, and color S’s Right child Black
S
P
Rotate S around P
P
S
V
P
Recolor: Swap colors of S and
P, and color S’s Right child
V Black
P
delete S
U
Case D:
- S is Black, S’s right child is Black and S’s left child is Red
i) Rotate S’s left child around S
ii) Swap color of S and S’s left child
P
P
S
V
V
P
S
Rotate S’s
left child
V
around S
S
Recolor: Swap
color of S and S’s
left child
Analysis of deletion
Top-Down Insertion
REVIEW OF BOTTOM-UP INSERTION
In B-Up insertion, “ordinary” BST insertion was used,
followed by correction of the tree on the way back up to
the root
This is most easily done recursively
Insert winds up the recursion on the way down the tree to the
insertion point
Fixing the tree occurs as the recursion unwinds
TOP-DOWN INSERTION STRATEGY
In T-Down insertion, the corrections are done while
traversing down the tree to the insertion point.
When the actual insertion is done, no further corrections
are needed, so no need to traverse back up the tree.
So, T-Down insertion can be done iteratively which is
generally faster
GOAL OF T-D INSERTION
Insertion is always done as a leaf (as in ordinary BST
insertion)
Recall from the B-Up flow chart that if the uncle of a
newly inserted node is black, we restore the RB tree
properties by one or two local rotations and recoloring – we
do not need to make changes further up the tree
GOAL (2)
Therefore, the goal of T-D insertion is to traverse from the
root to the insertion point in such a way that RB properties
are maintained, and at the insertion point, the uncle is
Black.
X (Red or Black)
Y Z
Y Z
Y Z
P P
X X
Y Z Y Z
G P
P U
G
X
X S Z
Y Z Y S U
G G
X U
P U
S X P Z
S Y
Y Z
G X
X U G
P
P Z
S Y Z U
S Y
Y Z Y Z
Case 2
P is Red
G G Rotate P P
X & P both left/right
Recolor around G G
X,Y,Z Recolor P,G X
P P
Y Z
X X
Y Z Y Z
Case 3 G G X
P is Red Recolor X,Y,Z Rotate X G
X and P are Rotate X X around G P
P
opposite children around P Recolor X, G
P
X
Y Z Y Z
Y Z
39
40
41
42
ANOTHER EXAMPLE
43
44
85
45
85
46
47
48
49
50
51
RED BLACK TREES
Top-Down Deletion
RECALL THE RULES FOR BST DELETION
1. If a node to be deleted is a leaf, just delete it.
2. If a node to be deleted has just one child, replace it
with that child
3. If a node to be deleted has two children, replace the
value of by it’s in-order predecessor’s value then
delete the in-order predecessor (a recursive step)
WHAT CAN GO WRONG?
1. If the delete node is red?
P P
X T X T
L1 L2
Case 2A3
X has 2 Black Children and T’s Right Child is Red
Rotate T around P
Recolor X, P, T and R then continue down the tree
T
P
X T P R
R
X R1 R2
L L
R1 R2
CASE 2B
X HAS AT LEAST ONE RED CHILD
Continue down the tree to the next level
If the new X is Red, continue down again
If the new X is Black (T is Red, P is Black)
Rotate T around P
Recolor P and T
Back to main case – step 2
CASE 2B DIAGRAM
P
X T
P P
X T T X
Step 4
Color the Root Black
EXAMPLE 1
DELETE 10 FROM THIS RB TREE
15
6 17
12 20
3 16
10 13 18 23
15
X 6 17
12 20
3 16
10 13 18 23
6 17
12 20
3 16
10 13 18 23
X
7
6 17
12 20
3 16
7 13 18 23
The final tree after 7 has replaced 10 and 7’s red node
deleted and (step 4) the root has been colored Black.
TREES WITH CONSTANT UPDATE
TIME AT A KNOWN LOCATION
70
Finger Trees and Level Linking
1. within each level, the intervals associated with the nodes form a
partition
of ]−∞,∞[
2. along each path from the root to a leaf, the number of nodes
between two
nodes of consecutive levels is bounded by a constant C.
2-3 tree of integers
To make it a finger tree, what you do, in theory, is reach down to the leftmost
and rightmost internal nodes of the tree – in our case, the parents of (1,2) and
(8, 9, 10). Then you pick up the tree by those two nodes, and let the rest
dangle down.
If you look at this, you’ve got a finger for the node (1,2), and a finger for the node
(8,9,10). In between them, you’ve got a bunch of stuff dangling. But if you look at
the dangling stuff: it’s a finger tree.