by Thomas A. Anastasio
Red-Black Trees
Definition: A red-black tree is a binary search tree where:
Every node is either red or black. Each NULL pointer is considered to be a black node If a node is red, then both of its children are black. Every path from a node to a leaf contains the same number of black nodes.
Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting n.
3
Theorem 1 Any red-black tree with root x, has at least n = 2bh(x) 1 internal nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.
Theorem 2 In a red-black tree, at least half the nodes on any path from the root to a leaf must be black.
Proof If there is a red node on the path, there must be a corresponding black node.
Theorem 3 In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf.
Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem 2, a least the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path
9
Theorem 4 A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h
10
Bottom Up Insertion
Insert node as usual in BST Color the Node RED What Red-Black property may be violated?
Every node is Red or Black Leaf nodes are Black NULLS If node is Red, both children must be Black Every path from node to descendant leaf must contain the same number of Blacks
11
Bottom Up Insertion
Insert node; Color it RED; X is pointer to it Cases
0: X is the root -- color it black 1: Both parent and uncle are red -- color parent and uncle black, color grandparent red, point X to grandparent, check new situation 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent
12
P
X
U
G P
P
S X
U
X P G
S U
14
P
X S
U
P X G
S
15
11
2
7 5 8
14
15
Black node
Red node
16
Insertion Practice
Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree
17
Red-Black Trees
Bottom-Up Deletion
Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a case 1 or case 2 will be done (leaf or just one child). If deleted node, U, is a leaf, think of deletion as replacing with the NULL pointer, V. If U had one child, V, think of deletion as replacing U with V. 2. What can go wrong??
Terminology
The node just deleted was U The node that replaces it is V, which has an extra unit of blackness The parent of V is P The sibling of V is S
Black Node Red Node Red or Black and dont care
NOT a terminal case One of the other cases will now apply All other cases apply when S is Black
Case 1 Diagram
P V+ S V+ S
Rotate
S P
Recolor
P
V+
Case 2 diagram
P
V+
Recolor and absorb
P+
S
Case 3 diagrams
P V+ S V S
Rotate
P
V
Recolor
Case 4 Diagrams
P V+ S P
P
Rotate
V+ S
V+
Recolor
65
50 10 70
80 60 90
62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
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. That way we may have to rotate and recolor, but not propagate back up the tree
If a new node is inserted as a child of Y or Z, there is no problem since the new nodes parent is black
If new node is inserted as child of Y or Z, its uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.
Top-Down Traversal
X Y Z
As we traverse down the tree and encounter this case, we recolor and possible do some rotations.
There are 3 cases.
Remember the goal to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.
X
Y Z Y
X
Z
Case 2
Xs Parent is Red (so Grandparent is Black) and X and P are both left/right children
Rotate P around G Color P black Color G red
Note that Xs uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- Xs Parent and Xs uncle)
Case 2 diagrams
G
P X Y Z S Y U
P X
Z S U G
Case 3
Xs Parent is Red (so Grandparent is Black) and X and P are opposite children
Rotate P around G Color P black Color G red
Again note that Xs uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- Xs Parent and Xs uncle)
Case 3 Diagrams (1 of 2)
G
P S Y X Z S U P Y X Z
G
U
Case 3 Diagrams (2 of 2)
G X P Z U P Z
X
G
An exercise insert F
D
T
J E K
Case 2 P is Red X & P both left/right P X Y Case 3 P is Red X and P are opposite children X Y
Recolor X,Y,Z X Z Y
G P
P G X Z
G
P Recolor X,Y,Z Rotate X around P Y Z
G
X P Z Rotate X around G Recolor X, G P
X
G
Top-Down is iterative
Restructure the tree on the way down so we dont have to go back up
Terminology
Matching Weiss text section 12.2
X is the node being examined T is Xs sibling P is Xs (and Ts) parent R is Ts right child L is Ts left child
This discussion assumes X is the left child of P. As usual, there are left-right symmetric cases.
Basic Strategy
As we traverse the tree, we change every node we visit, X, to Red. When we change X to Red, we know
P is also Red (we just came from there) T is black (since P is Red, its children are Black)
Case 2A2
X has 2 Black Children and Ts Left Child is Red
Rotate L around T, then L around P Recolor X and P then continue down the tree
P
X T P X
L2 L1 L2
L
L1
Case 2A3
X has 2 Black Children and Ts Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree
P X T R X L
R1 R2
T P
R1
R
R2
Case 2B Diagram
P
X T
If move to Black child (2B2) If move to the Red child (2B1) Rotate T around P; Recolor P and T Move down again Back to step 2, the main case
Step 3
Eventually, find the node to be deleted a leaf or a node with one non-null child that is a leaf.
Delete the appropriate node as a Red leaf
Step 4
Color the Root Black
3
10 7
12
13
16
18
20 23
Step 1 Root has 2 Black children. Color Root Red Descend the tree, moving X to 6
Example 1 (contd)
15
X
3
17
12
10 7 13
16
18
20 23
One of Xs children is Red (case 2B). Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10.
Example 1 (contd)
15 6 17
3
10 7
12
16
13 18
20 23
Step 3 -Since 10 is the node to be deleted, replace its value with the value of its only child (7) and delete 7s red node
Example 1 (contd)
15 6 3 7 12 16 17 20
13
18
23
The final tree after 7 has replaced 10 and 7s red node deleted and (step 4) the root has been colored Black.
3
2 4 10
12
13
16
20
Step 1 the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2
Example 2 (contd)
X
6 15 17
3
2 4 10
12
13
16
20
X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.
Example 2 (contd)
15 6
17
3
2
T
4
X
10
12
13
16
20
X has 2 Black children. Xs sibling (3) also has 2 black children. Case 2A1 recolor X, P, and T and continue down the tree, arriving at 10.
Example 2 (contd)
15 6 3 2 4 17
P X
10
12
16 13
20
X is now the leaf to be deleted, but its Black, so back to step 2. X has 2 Black children and T has 2 Black children case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black
Example 2 Solution
15 6 3 2 4 12 13 16 17 20
5
3 2 4 7 6 9 11
12
13
Step 1 root has 2 Black children. Color Root red. Set X to appropriate child of root (10)
Example 3 (contd)
15 10
X
12
5
3 2 4 7 6 9 11
13
X has one Red child (case 2B) Traverse down the tree, arriving at 12.
Example 3 (contd)
15 10
5
3 2 4
T
7 6 9
X
11
12
13
Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2
Example 3 (contd)
15 5
P
10
3 2 4
7 6
T
9 11
X
12 13
Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal
Example 3 (contd)
15 5 10 3 2 4 7 6 9 11
P
12
13
Having traversed down the tree, we arrive at 11, the leaf to be deleted, but its Black, so back to step 2. X and T both have two Black children. Recolor X, P and T. Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black
Example 3 Solution
15 5 10 3 2 4 7 6 9 12 13