SlideShare a Scribd company logo
1 of 221
Download to read offline
Analysis of Algorithms
Red-Black Trees
Andres Mendez-Vazquez
February 29, 2016
1 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
2 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
3 / 149
Images/ITESM.p
Well Balanced Trees
Do you remember AVL (Adelson-Velskii and Landis) Trees?
Quite nice recursive methods for balancing the tree!!!
It is based on an height Invariant
At any node in the tree, the heights of the left and right subtrees
differs by at most 1.
4 / 149
Images/ITESM.p
Well Balanced Trees
Do you remember AVL (Adelson-Velskii and Landis) Trees?
Quite nice recursive methods for balancing the tree!!!
It is based on an height Invariant
At any node in the tree, the heights of the left and right subtrees
differs by at most 1.
4 / 149
Images/ITESM.p
Thus, it is necessary to add an extra field to the Node
Structure
Structure of a Node
Structure 1: Struct Node
1 Key key
2 int height
3 Value val
4 Node Left, Right
5 / 149
Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
8 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
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 paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
Images/ITESM.p
Red-Black Trees
Example
26
17 41
14 21
10 16
7 12
19
20
23
15
3
30 47
28 38
35 39
T.nil
10 / 149
Images/ITESM.p
Height on a Red Black Tree
Black Height bh(x)
We call the number of black nodes on any path from, but not including, a
node x down to a leaf the black height of the node.
11 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
12 / 149
Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
14 / 149
Images/ITESM.p
Examples for bh(x) = 0
Case I
x
T.NIL
15 / 149
Images/ITESM.p
Examples for bh(x) = 0
Case II - There are other, but they are similar
x
T.NIL
16 / 149
Images/ITESM.p
Then
Then, we have
Thus 2bh(x) − 1 = 20 − 1 = 0.
Now with bh(x) > 0, we have that child[x] has height bh(x) or
bh(x) − 1.
17 / 149
Images/ITESM.p
Then
Then, we have
Thus 2bh(x) − 1 = 20 − 1 = 0.
Now with bh(x) > 0, we have that child[x] has height bh(x) or
bh(x) − 1.
17 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
18 / 149
Images/ITESM.p
Thus, we have the following
Example
x
19 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
Images/ITESM.p
Lemma for the height of Red-Black Trees
Corollary
From the previous theorem we conclude that SEARCH, MINIMUM,
etcetera, can be implemented in O(log n).
23 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
24 / 149
Images/ITESM.p
Rotations in Red-Black Trees
Purpose
Rotations are used to maintain the structure of the Red-Black Trees.
Types of rotations
There are left and right rotations, they are inverse to each other.
25 / 149
Images/ITESM.p
Rotations in Red-Black Trees
Purpose
Rotations are used to maintain the structure of the Red-Black Trees.
Types of rotations
There are left and right rotations, they are inverse to each other.
y
x
y
x
Left-Rotate(T,x)
Right-Rotate(T,y)
25 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
Images/ITESM.p
Example Left-Rotate
Step 1 - the correct right child is changed
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
y
x
27 / 149
Images/ITESM.p
Example Left-Rotate
Step 2 - the parents are set correctly
3. if y.left = T.nil
4. y.left.p = x
5. y.p = x.p
y
x
28 / 149
Images/ITESM.p
Example Left-Rotate
Step 3
6. if x.p == T.nil Set y to be the root if x was it
7. T.root = y
8. elseif x == x.p.left
9. x.p.left = y
10. else x.p.right = y
y
x
29 / 149
Images/ITESM.p
Example Left-Rotate
Step 4
11. y.left = x Put x on y’s left
12. x.p = y
y
x
30 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
31 / 149
Images/ITESM.p
First than anything
Something Notable
You still have a Binary Search Tree!!!
There
How do you do insertion in a Binary Search Tree?
32 / 149
Images/ITESM.p
First than anything
Something Notable
You still have a Binary Search Tree!!!
There
How do you do insertion in a Binary Search Tree?
32 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
33 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
First
Search Variables being Initialized.
34 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x =
x.left
7. else x =
x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Second
Binary search for insertion.
35 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Third
Change parent of the node to be inserted.
36 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Fourth
Test to see if the Tree is empty!!!
37 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Fifth
Insert node z in the correct left or right child:
if z.key < y.key ⇒ y.left = z
if z.key ≥ y.key ⇒ y.right = z
38 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Sixth
Make z’s leafs equal to T.nil.
39 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Seventh
Make z’s color equal to RED.
40 / 149
Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Eight
Call RB-Insert-Fixup!!!
41 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
42 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
44 / 149
Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 1
z’s uncle is RED
Change of parent and uncle’s color to
BLACK
Move problem to z’s grandfather
45 / 149
Images/ITESM.p
Maintenance in Insertion in Red-Black Trees
Case I - z’s uncle is red
z
yA
B
C
D
46 / 149
Images/ITESM.p
Maintenance in Insertion in Red-Black Trees
Recolor parent and uncle to black
new z
A
B
C
D
47 / 149
Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
Images/ITESM.p
The Symmetric Case
We have
z
y
A
B
C
D
49 / 149
Images/ITESM.p
The Symmetric Case
We have
new z
A
B
C
D
50 / 149
Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 2
if z is in the right child then
Move the problem to the parent by
making z = z.p
Rotate left using z as the rotation
node
51 / 149
Images/ITESM.p
Insertion in Red-Black Trees
Case 2 - z’s uncle y is black and z is a right child
z
y
A
B
C
CASE 2
52 / 149
Images/ITESM.p
Thus
First
Here simple recoloring will not work.
Rotate
We rotate first from to left using z.p.
This moves case 2 toward case 3
To get ready for the final fix-up.
53 / 149
Images/ITESM.p
Thus
First
Here simple recoloring will not work.
Rotate
We rotate first from to left using z.p.
This moves case 2 toward case 3
To get ready for the final fix-up.
53 / 149
Images/ITESM.p
Thus
First
Here simple recoloring will not work.
Rotate
We rotate first from to left using z.p.
This moves case 2 toward case 3
To get ready for the final fix-up.
53 / 149
Images/ITESM.p
From Case 2 to Case 3
Example
z
y
A
B
C
z
y
A
B
CASE 2 CASE 3
C
54 / 149
Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 3
if z is in the left child then
Recolor z’s parent to BLACK
recolor z’s grandparent to RED
Rotate right using the grandparent
55 / 149
Images/ITESM.p
Then
We do the following
Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ
are black nodes)
Finally
Then, you rotate right using z.p.p to fix the black height property!!
56 / 149
Images/ITESM.p
Then
We do the following
Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ
are black nodes)
Finally
Then, you rotate right using z.p.p to fix the black height property!!
56 / 149
Images/ITESM.p
From Case 3 to final recoloring
Case 3 - It allows to fix the bh locally
z
y
A
B
CASE 3
B
A C
C
57 / 149
Images/ITESM.p
Finally
Root Recoloring
After pushing the problem up to the root by the while loop color the root
to BLACK!!!
We have then
A B
Root
A B
Root
58 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
59 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Example Tree is empty
26 1741 14 21 10 712 3028
T.nil
Selection Order
60 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
The root becomes red!!!
STEPS
1 y = T.nil
2 x = T.root
3 We never go into
the search loop
4 z.p = y
5 if y == T.nil
6 T.root = z
7 z.left = T.nil
8 z.right = T.nil
9 z.color = T.RED
26 1741 14 21 10 712
30
28
T.nil
Selection Order
z
61 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Fix the problem
ONLY STEP
1 T.root.color=BLACK
It happens at the end of the
Fix-up
26 1741 14 21 10 712
30
28
T.nil
Selection Order
z
62 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 7
1 Do a binary
search to find a
place to insert
2 Parent of z is not
RED ⇒ no fix-up
26 1741 14 21 10
7
12
30
28
T.nil
Selection Order
z
63 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 12
1 Do a binary
search to find a
place to insert
2 Parent of z is
RED!!!
26 1741 14 21 10
7
12
30
28
T.nil
Selection Order
z
64 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 2 into Case 3
1 Re-balance first to the
left
if z==z.p.right
z=z.p
Left-Rotate(T, z)
26 1741 14 21 10
712
30
28
T.nil
Selection Order
7z
65 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26 1741 14 21 10
712
30
28
T.nil
Selection Order
7z
66 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 10
1 We insert 10
2 We color it to RED
3 We need to fix the
problem
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
y
67 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if z.p == z.p.p.left
2 y=z.p.p.right
3 if y.color ==RED
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
y
68 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if y.color ==RED
2 z.p.color = BLACK
3 y.color = BLACK
4 z.p.p.color = RED
5 z = z.p.p
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
69 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 You get out of the loop and...
1 T.root.color = BLACK
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
70 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 28
1 We insert 28
2 We color it to RED
3 No problem to fix...
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
71 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 28
1 We insert 28
2 We color it to RED
3 No problem to fix...
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
72 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 21
1 We insert 21
2 We color it to RED
3 We need to fix...
26 1741 14
21
10
712
30
28
T.nil
Selection Order
7
z
73 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26 1741 14
2110
712
30
28
T.nil
Selection Order
7
z
74 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 14
1 We insert 14
2 We color it to RED
3 We need to fix...
26 1741
14
2110
712
30
28
T.nil
Selection Order
7
z
75 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if z.p == z.p.p.left
2 y=z.p.p.right
3 if y.color ==RED
26 1741
14
2110
712
30
28
T.nil
Selection Order
7
z
y
76 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if y.color ==RED
2 z.p.color = BLACK
3 y.color = BLACK
4 z.p.p.color = RED
5 z = z.p.p
Nothing to do after!!!
26 1741
14
2110
712
30
28
T.nil
Selection Order
7 z
77 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 17
1 Do a binary search to find a
place to insert
2 Parent of z is RED!!!
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7 z
z
78 / 149
Images/ITESM.p
Case 2 into 3
1 Re-balance first to the left
if z==z.p.right
z=z.p
Left-Rotate(T, z)
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
79 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
79 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
80 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
81 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 41
1 Put in the correct
node by binary
search
2 Because 41’s parent
is BLACK nothing to
fix
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
82 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 Symmetrical
1 if z.p == z.p.p.right
2 y=z.p.p.left
3 if y.color ==RED
4 z.p.color = BLACK
5 y.color = BLACK
6 z.p.p.color = RED
7 z = z.p.p
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
83 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 Symmetrical
1 if z.p == z.p.p.right
2 y=z.p.p.left
3 if y.color ==RED
4 z.p.color = BLACK
5 y.color = BLACK
6 z.p.p.color = RED
7 z = z.p.p
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
84 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 2 to case 3 - Symmetrical
1 Re-balance first to the right
if z==z.p.left
z=z.p
Right-Rotate(T, z)
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7 z
85 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3 - Symmetrical
1 Re-color and re-balance to the
right
z.p.color = BLACK
z.p.p.color=RED
Left-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already BLACK so
nothing happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7 z
86 / 149
Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3 - Symmetrical
1 Re-color and re-balance to the
right
z.p.color = BLACK
z.p.p.color=RED
Left-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already BLACK so
nothing happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
87 / 149
Images/ITESM.p
Complexity of RB-Insert
It is easy to see that we have
O(log n) (1)
88 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
89 / 149
Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
Images/ITESM.p
Thus
Example
z
y
x
MOVING
91 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 1
Store the info of the node to be
deleted
92 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 2
If the left child is empty
Store the info of the right child
Move z.right into the position of
z
93 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 3
If the right child is empty
Store the info of the left child
Move z.left into the position of z
94 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 4
Find the successor of z
Store the info of it: Color and
right child
95 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 5
If parent of succesor is z then
set parent of x to y
96 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
ø
Case 6
Substitute y with y.right
set y.right with z.right
set the parent of y.right to y
97 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
17. RB-Transplant(T, z, y)
18. y.left = z.left
19. y.left.p = y
20. y.color = z.color
21. if y-original-color ==BLACK
22. RB-Delete-Fixup(T,x)
Case 7
Substitute z with y
Make y.left to z.left
Make the parent of y.left to y
Make the color of y to the color
of z
98 / 149
Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
17. RB-Transplant(T, z, y)
18. y.left = z.left
19. y.left.p = y
20. y.color = z.color
21. if y-original-color ==BLACK
22. RB-Delete-Fixup(T,x)
Case 8
If y-original-color == BLACK
then call RB-Delete-Fixup(T,x)
After all x points to the node
that:
It is moved into the position
of y.
Where y was moved into the
position of z.
99 / 149
Images/ITESM.p
Where RB-Transplant
RB-Transplant(T, u, v)
1 if u.p == T.nil
2 T.root = v
3 elseif u == u.p.left
4 u.p.left = v
5 else u.p.right = v
6 v.p = u.p
100 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
101 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
Images/ITESM.p
The node x gets position y
Then
If y was originally black after taking the z.color can produce a violation,
then RB-Delete-Fixup is called.
This can happen
If y was originally red the Red-Black Trees properties still hold.
106 / 149
Images/ITESM.p
The node x gets position y
Then
If y was originally black after taking the z.color can produce a violation,
then RB-Delete-Fixup is called.
This can happen
If y was originally red the Red-Black Trees properties still hold.
106 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Question
What if we removed a black node?
107 / 149
Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
Images/ITESM.p
Fix-up
How?
This could be fixed assuming that x has an “extra black.”
Meaning
This means that the node is “doubly black” or “red-and-black.”
109 / 149
Images/ITESM.p
Fix-up
How?
This could be fixed assuming that x has an “extra black.”
Meaning
This means that the node is “doubly black” or “red-and-black.”
109 / 149
Images/ITESM.p
Example
We have then
x
Doubly Black Case
Virtual
Black
x
Red-and-Black Case
Virtual
Black
110 / 149
Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
112 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
While loop
Because a violation
on the bh, you need
to move x up until
the problem is fixed
up.
113 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Finding who you are
Find which child are
you
Make w the other
child
114 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 1
if x is BLACK and w
is RED
Fix the bh problem
by making w
BLACK, x’s parent
to RED then rotate
left using x’s parent
Make w = x.p.right
moving the problem
down.
115 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 1 - x’s sibling w is red. You keep the bh property of the other
subtrees
x wA
B
C
D
E
116 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 1 - x’s sibling w is red. You keep the bh property of the other
subtrees
x new wA
B
C
D
E
117 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 2
Now if w.left’s color
and w.right’s color is
BLACK
We do something
smart decrease the
bh height at w by
making w’s color to
RED
Move the problem
fropm to x to x.p
(After all the
subtree have the
same height at x.p)
118 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 2 - x’s sibling w is black, and both of w’s children are black.
x wA
C
D
E
B
Note: The Node with half and half colors has the meaning that it
can be red or black.
119 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 2 - x’s sibling w is black, and both of w’s children are black.
new x
A
C
D
E
B
120 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 3
If w.right’s color is
BLACK
We do something
smart, we re-color
and do a right
rotation at w
This does not
change the
Red-Black Trees
properties of w
but prepare the
situation for fixing
the x height problem
in case 4.
121 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 3 - x’s sibling w is black, w’s left child is red, and w’s right child
is black.
x A
C
D
E
B
w
122 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 3: x’s sibling w is black, w’s left child is red, and w’s right child
is black.
x A C
D
E
B
new w
123 / 149
Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
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 = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 4
We are ready to fix
our problem!!! with
respect to x (Case 2
and 3 where a
preparation to fix
the problem)
We increase the
height of the bh
with the problem, x.
124 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 4: x’s sibling w is black, and w’s right child is red.
x A
E
B
w
C
D
125 / 149
Images/ITESM.p
Suitable Rotations and Recoloring
Case 4: x’s sibling w is black, and w’s right child is red.
new x=T.root
A
EB
C
D
126 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
127 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Delete 17
26
17
41
14 21
10
712
30
28
T.nil
7
z
128 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Store the info about z
y = z
y-original-color = y.color 26
17
41
14 21
10
712
30
28
T.nil
7
z
129 / 149
Images/ITESM.p
Deletion in Red-Black Trees
None of the children of z are T.nil, thus
else y = Tree-Minimum(z.right)
y-original-color = y.color
x = y.right 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
130 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We have that y.p= z
else RB-Transplant(T,y,y.right)
y.right = z.right
y.right.p = y 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
131 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Transplant(T, y, y.right)
elseif y == y.p.left
y.p.left = y.right
y.right.p = y.p
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
y.right.p=y.p
132 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Now, we move pointers
y.right = z.right
y.right.p = y 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
y.right.p=y
133 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Transplant(z,y)
if z.p == T.nil
T.root = y
y.p = z.p
26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
y.p = z.p
134 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Move pointers
y.left = z.left
y.left.p = y
y.color = z.color 26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
y.left.p = y
135 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
z.right = NULL
z.left = NULL
z.parent = NULL
26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
136 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
Never enter into the loop
Simply do x.color =
BLACK
26
41
14
21
10
712
30
28
T.nil
7
y
x
137 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Delete 12
26
17
41
14 21
10
712
30
28
T.nil
7
z
138 / 149
Images/ITESM.p
Deletion in Red-Black Trees
None of the children of z are T.nil, thus
else y = Tree-Minimum(z.right)
y-original-color = y.color
(BLACK)
x = y.right (T.NIL)
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
139 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We have that y.p== z
if y.p == z
x.p = y 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
140 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
141 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
142 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
143 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
enter into the loop
if x == x.p.right
w = x.p.left
26
17
41
14
21
10
30
28
T.nil
7
y
x
w
144 / 149
Images/ITESM.p
Deletion in Red-Black Trees
We enter into case 4
1 w.color = x.p.color
2 x.p.color = BLACK
3 w.right.color = BLACK
26
17
41
14
21
10
30
28
T.nil
7
x
w
145 / 149
Images/ITESM.p
Deletion in Red-Black Trees
Rotate and move the x
1 Left-Rotate(T, x.p)
2 x = T.root 26
17
41
14
21
10 30
28
T.nil
7
x
146 / 149
Images/ITESM.p
Applications of Red-Black Trees
Completely Fair Scheduler (CFS)
It is a task scheduler which was merged into 2.6.23 release of the
Linux Kernel.
It is a replacement of earlier O(1) scheduler.
CFS algorithm was designed to maintain balance (fairness) in
providing processor time to tasks.
Sorting using Parallel Implementations
Running in O(log log n) time
147 / 149
Images/ITESM.p
Applications of Red-Black Trees
Completely Fair Scheduler (CFS)
It is a task scheduler which was merged into 2.6.23 release of the
Linux Kernel.
It is a replacement of earlier O(1) scheduler.
CFS algorithm was designed to maintain balance (fairness) in
providing processor time to tasks.
Sorting using Parallel Implementations
Running in O(log log n) time
147 / 149
Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
148 / 149
Images/ITESM.p
Exercises
From Cormen’s book solve
13.1-1
13.1-3
13.1-5
13.1-7
13.2-2
13.2-3
13.2-4
13.2-5
13.3-2
13.3-4
13.4-2
13.4-4
149 / 149

More Related Content

What's hot (20)

Red black trees
Red black treesRed black trees
Red black trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
Red black trees and their properties
Red black trees and their propertiesRed black trees and their properties
Red black trees and their properties
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Lec14
Lec14Lec14
Lec14
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
lecture 14
lecture 14lecture 14
lecture 14
 
Avl trees
Avl treesAvl trees
Avl trees
 
Trees
TreesTrees
Trees
 

Viewers also liked

Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofJaqueline Vallejo
 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREEnikhilarora2211
 
Struktur data 06 (red black tree)
Struktur data 06 (red black tree)Struktur data 06 (red black tree)
Struktur data 06 (red black tree)Sunarya Marwah
 
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)nikhilarora2211
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahniHitesh Wagle
 
Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashingAmi Ranjit
 
Best for b trees
Best for b treesBest for b trees
Best for b treesDineshRaaja
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashingJeet Poria
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 

Viewers also liked (20)

Algebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic ProofAlgebra 1 2.8 Algebraic Proof
Algebra 1 2.8 Algebraic Proof
 
Insertion in RED BLACK TREE
Insertion in RED BLACK TREEInsertion in RED BLACK TREE
Insertion in RED BLACK TREE
 
Struktur data 06 (red black tree)
Struktur data 06 (red black tree)Struktur data 06 (red black tree)
Struktur data 06 (red black tree)
 
Red black trees
Red black treesRed black trees
Red black trees
 
Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
 
Logic gates with laws
Logic gates with lawsLogic gates with laws
Logic gates with laws
 
B-Tree
B-TreeB-Tree
B-Tree
 
B tree long
B tree longB tree long
B tree long
 
B tree
B treeB tree
B tree
 
B+ Tree
B+ TreeB+ Tree
B+ Tree
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
 
Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
 
B Trees
B TreesB Trees
B Trees
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
 
B tree
B treeB tree
B tree
 
b+ tree
b+ treeb+ tree
b+ tree
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 

Similar to 10 Red-Black Trees (11)

Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black trees
Red black treesRed black trees
Red black trees
 
Tree
TreeTree
Tree
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
Persistent Search Trees
Persistent Search TreesPersistent Search Trees
Persistent Search Trees
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxjasonsedano2
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfNaveenVerma126
 
How to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfHow to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfRedhwan Qasem Shaddad
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Apollo Techno Industries Pvt Ltd
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS Bahzad5
 
The relationship between iot and communication technology
The relationship between iot and communication technologyThe relationship between iot and communication technology
The relationship between iot and communication technologyabdulkadirmukarram03
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdfMadan Karki
 
Modelling Guide for Timber Structures - FPInnovations
Modelling Guide for Timber Structures - FPInnovationsModelling Guide for Timber Structures - FPInnovations
Modelling Guide for Timber Structures - FPInnovationsYusuf Yıldız
 
Power System electrical and electronics .pptx
Power System electrical and electronics .pptxPower System electrical and electronics .pptx
Power System electrical and electronics .pptxMUKULKUMAR210
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging systemgokuldongala
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....santhyamuthu1
 
CSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxCSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxssusera0771e
 
IT3401-WEB ESSENTIALS PRESENTATIONS.pptx
IT3401-WEB ESSENTIALS PRESENTATIONS.pptxIT3401-WEB ESSENTIALS PRESENTATIONS.pptx
IT3401-WEB ESSENTIALS PRESENTATIONS.pptxSAJITHABANUS
 
Gender Bias in Engineer, Honors 203 Project
Gender Bias in Engineer, Honors 203 ProjectGender Bias in Engineer, Honors 203 Project
Gender Bias in Engineer, Honors 203 Projectreemakb03
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid BodyAhmadHajasad2
 
Tachyon 100G PCB Performance Attributes and Applications
Tachyon 100G PCB Performance Attributes and ApplicationsTachyon 100G PCB Performance Attributes and Applications
Tachyon 100G PCB Performance Attributes and ApplicationsEpec Engineered Technologies
 
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchrohitcse52
 

Recently uploaded (20)

nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptx
 
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdfSummer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
Summer training report on BUILDING CONSTRUCTION for DIPLOMA Students.pdf
 
Litature Review: Research Paper work for Engineering
Litature Review: Research Paper work for EngineeringLitature Review: Research Paper work for Engineering
Litature Review: Research Paper work for Engineering
 
How to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfHow to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdf
 
Lecture 2 .pdf
Lecture 2                           .pdfLecture 2                           .pdf
Lecture 2 .pdf
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
 
The relationship between iot and communication technology
The relationship between iot and communication technologyThe relationship between iot and communication technology
The relationship between iot and communication technology
 
Technical Management of cement industry.pdf
Technical Management of cement industry.pdfTechnical Management of cement industry.pdf
Technical Management of cement industry.pdf
 
Modelling Guide for Timber Structures - FPInnovations
Modelling Guide for Timber Structures - FPInnovationsModelling Guide for Timber Structures - FPInnovations
Modelling Guide for Timber Structures - FPInnovations
 
Power System electrical and electronics .pptx
Power System electrical and electronics .pptxPower System electrical and electronics .pptx
Power System electrical and electronics .pptx
 
solar wireless electric vechicle charging system
solar wireless electric vechicle charging systemsolar wireless electric vechicle charging system
solar wireless electric vechicle charging system
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
 
CSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptxCSR Managerial Round Questions and answers.pptx
CSR Managerial Round Questions and answers.pptx
 
IT3401-WEB ESSENTIALS PRESENTATIONS.pptx
IT3401-WEB ESSENTIALS PRESENTATIONS.pptxIT3401-WEB ESSENTIALS PRESENTATIONS.pptx
IT3401-WEB ESSENTIALS PRESENTATIONS.pptx
 
Gender Bias in Engineer, Honors 203 Project
Gender Bias in Engineer, Honors 203 ProjectGender Bias in Engineer, Honors 203 Project
Gender Bias in Engineer, Honors 203 Project
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
 
Tachyon 100G PCB Performance Attributes and Applications
Tachyon 100G PCB Performance Attributes and ApplicationsTachyon 100G PCB Performance Attributes and Applications
Tachyon 100G PCB Performance Attributes and Applications
 
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai searchChatGPT-and-Generative-AI-Landscape Working of generative ai search
ChatGPT-and-Generative-AI-Landscape Working of generative ai search
 

10 Red-Black Trees

  • 1. Analysis of Algorithms Red-Black Trees Andres Mendez-Vazquez February 29, 2016 1 / 149
  • 2. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 2 / 149
  • 3. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 3 / 149
  • 4. Images/ITESM.p Well Balanced Trees Do you remember AVL (Adelson-Velskii and Landis) Trees? Quite nice recursive methods for balancing the tree!!! It is based on an height Invariant At any node in the tree, the heights of the left and right subtrees differs by at most 1. 4 / 149
  • 5. Images/ITESM.p Well Balanced Trees Do you remember AVL (Adelson-Velskii and Landis) Trees? Quite nice recursive methods for balancing the tree!!! It is based on an height Invariant At any node in the tree, the heights of the left and right subtrees differs by at most 1. 4 / 149
  • 6. Images/ITESM.p Thus, it is necessary to add an extra field to the Node Structure Structure of a Node Structure 1: Struct Node 1 Key key 2 int height 3 Value val 4 Node Left, Right 5 / 149
  • 7. Images/ITESM.p Observations Due to the balancing methods AVL Trees requiere to have a O (log2 n) rotations. AVL Trees operations cost O (log2 n). There is a problem about the height How much memory it is necessary to allocate for the height field in massive binary search trees? 6 / 149
  • 8. Images/ITESM.p Observations Due to the balancing methods AVL Trees requiere to have a O (log2 n) rotations. AVL Trees operations cost O (log2 n). There is a problem about the height How much memory it is necessary to allocate for the height field in massive binary search trees? 6 / 149
  • 9. Images/ITESM.p Observations Due to the balancing methods AVL Trees requiere to have a O (log2 n) rotations. AVL Trees operations cost O (log2 n). There is a problem about the height How much memory it is necessary to allocate for the height field in massive binary search trees? 6 / 149
  • 10. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 11. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 12. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 13. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 14. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 15. Images/ITESM.p Furthermore Arguments in favor of AVL Trees Search is O (log N) since AVL trees are always balanced. Insertion and deletions are also O (log N). Arguments against using AVL trees Difficult to program and debug. The dynamic space nature of the balancing (Height) factor. Asymptotically faster but re-balancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 7 / 149
  • 16. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 8 / 149
  • 17. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 18. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 19. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 20. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 21. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 22. Images/ITESM.p Definitions Definition A Red Black Tree is a Binary Search Tree where each node has an extra field, its color. 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 paths from the node to descendant leaves contain the same number of black nodes. 9 / 149
  • 23. Images/ITESM.p Red-Black Trees Example 26 17 41 14 21 10 16 7 12 19 20 23 15 3 30 47 28 38 35 39 T.nil 10 / 149
  • 24. Images/ITESM.p Height on a Red Black Tree Black Height bh(x) We call the number of black nodes on any path from, but not including, a node x down to a leaf the black height of the node. 11 / 149
  • 25. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 12 / 149
  • 26. Images/ITESM.p Lemma for the height of Red-Black Trees Theorem A Red-Black Trees with n internal nodes has height at most 2 log(n + 1). Proof: Step 1 Prove that any subtree rooted at x contains at least 2bh(x) − 1 internal nodes. If bh(x) = 0, then 13 / 149
  • 27. Images/ITESM.p Lemma for the height of Red-Black Trees Theorem A Red-Black Trees with n internal nodes has height at most 2 log(n + 1). Proof: Step 1 Prove that any subtree rooted at x contains at least 2bh(x) − 1 internal nodes. If bh(x) = 0, then 13 / 149
  • 28. Images/ITESM.p Lemma for the height of Red-Black Trees Theorem A Red-Black Trees with n internal nodes has height at most 2 log(n + 1). Proof: Step 1 Prove that any subtree rooted at x contains at least 2bh(x) − 1 internal nodes. If bh(x) = 0, then 13 / 149
  • 29. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 14 / 149
  • 30. Images/ITESM.p Examples for bh(x) = 0 Case I x T.NIL 15 / 149
  • 31. Images/ITESM.p Examples for bh(x) = 0 Case II - There are other, but they are similar x T.NIL 16 / 149
  • 32. Images/ITESM.p Then Then, we have Thus 2bh(x) − 1 = 20 − 1 = 0. Now with bh(x) > 0, we have that child[x] has height bh(x) or bh(x) − 1. 17 / 149
  • 33. Images/ITESM.p Then Then, we have Thus 2bh(x) − 1 = 20 − 1 = 0. Now with bh(x) > 0, we have that child[x] has height bh(x) or bh(x) − 1. 17 / 149
  • 34. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 18 / 149
  • 35. Images/ITESM.p Thus, we have the following Example x 19 / 149
  • 36. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 37. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 38. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 39. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 40. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 41. Images/ITESM.p Proof Case I Height of child is bh(x). Then, the child is red, if not subtree rooted at x will have height bh(x) + 1! Now, we have two subtrees from the child with red root...(The children are black) Thus, we have Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal nodes. Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One for the node rooted at the child node) 20 / 149
  • 42. Images/ITESM.p Proof Finally Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Case II Height of the child is bh(x) − 1. Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Thus, we have that the tree with root x contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes. 21 / 149
  • 43. Images/ITESM.p Proof Finally Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Case II Height of the child is bh(x) − 1. Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Thus, we have that the tree with root x contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes. 21 / 149
  • 44. Images/ITESM.p Proof Finally Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Case II Height of the child is bh(x) − 1. Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Thus, we have that the tree with root x contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes. 21 / 149
  • 45. Images/ITESM.p Proof Finally Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Case II Height of the child is bh(x) − 1. Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by inductive hypothesis. Thus, we have that the tree with root x contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes. 21 / 149
  • 46. Images/ITESM.p Proof Again Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Finally Now given that h is the height of the tree, we have by property 4 that bh(T) ≥ h 2 . Then n ≥ 2bh(root) − 1 ≥ 2 h 2 − 1. Then, h ≤ 2 log(n + 1) . 22 / 149
  • 47. Images/ITESM.p Proof Again Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Finally Now given that h is the height of the tree, we have by property 4 that bh(T) ≥ h 2 . Then n ≥ 2bh(root) − 1 ≥ 2 h 2 − 1. Then, h ≤ 2 log(n + 1) . 22 / 149
  • 48. Images/ITESM.p Proof Again Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Finally Now given that h is the height of the tree, we have by property 4 that bh(T) ≥ h 2 . Then n ≥ 2bh(root) − 1 ≥ 2 h 2 − 1. Then, h ≤ 2 log(n + 1) . 22 / 149
  • 49. Images/ITESM.p Proof Again Then, the tree with root x contains at least 2 × 2bh(x)−1 − 1 = 2bh(x) − 1 Finally Now given that h is the height of the tree, we have by property 4 that bh(T) ≥ h 2 . Then n ≥ 2bh(root) − 1 ≥ 2 h 2 − 1. Then, h ≤ 2 log(n + 1) . 22 / 149
  • 50. Images/ITESM.p Lemma for the height of Red-Black Trees Corollary From the previous theorem we conclude that SEARCH, MINIMUM, etcetera, can be implemented in O(log n). 23 / 149
  • 51. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 24 / 149
  • 52. Images/ITESM.p Rotations in Red-Black Trees Purpose Rotations are used to maintain the structure of the Red-Black Trees. Types of rotations There are left and right rotations, they are inverse to each other. 25 / 149
  • 53. Images/ITESM.p Rotations in Red-Black Trees Purpose Rotations are used to maintain the structure of the Red-Black Trees. Types of rotations There are left and right rotations, they are inverse to each other. y x y x Left-Rotate(T,x) Right-Rotate(T,y) 25 / 149
  • 54. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 55. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 56. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 57. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 58. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 59. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 60. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 61. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 62. Images/ITESM.p Example of Rotations in Red-Black Trees LEFT-ROTATE(T,x) 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree 3 if y.left = T.nil 4 y.left.p = x 5 y.p = x.p Link x’s parent to y 6 if x.p == T.nil 7 T.root = y 8 elseif x == x.p.left 9 x.p.left = y 10 else x.p.right = y 11 y.left = x Put x on y’s left 12 x.p = y 26 / 149
  • 63. Images/ITESM.p Example Left-Rotate Step 1 - the correct right child is changed 1 y = x.right Set y 2 x.right = y.left Turn y’s left subtree into x’s right subtree y x 27 / 149
  • 64. Images/ITESM.p Example Left-Rotate Step 2 - the parents are set correctly 3. if y.left = T.nil 4. y.left.p = x 5. y.p = x.p y x 28 / 149
  • 65. Images/ITESM.p Example Left-Rotate Step 3 6. if x.p == T.nil Set y to be the root if x was it 7. T.root = y 8. elseif x == x.p.left 9. x.p.left = y 10. else x.p.right = y y x 29 / 149
  • 66. Images/ITESM.p Example Left-Rotate Step 4 11. y.left = x Put x on y’s left 12. x.p = y y x 30 / 149
  • 67. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 31 / 149
  • 68. Images/ITESM.p First than anything Something Notable You still have a Binary Search Tree!!! There How do you do insertion in a Binary Search Tree? 32 / 149
  • 69. Images/ITESM.p First than anything Something Notable You still have a Binary Search Tree!!! There How do you do insertion in a Binary Search Tree? 32 / 149
  • 70. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 33 / 149
  • 71. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) First Search Variables being Initialized. 34 / 149
  • 72. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Second Binary search for insertion. 35 / 149
  • 73. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Third Change parent of the node to be inserted. 36 / 149
  • 74. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Fourth Test to see if the Tree is empty!!! 37 / 149
  • 75. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Fifth Insert node z in the correct left or right child: if z.key < y.key ⇒ y.left = z if z.key ≥ y.key ⇒ y.right = z 38 / 149
  • 76. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Sixth Make z’s leafs equal to T.nil. 39 / 149
  • 77. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Seventh Make z’s color equal to RED. 40 / 149
  • 78. Images/ITESM.p Insertion Code in Red-Black Trees RB-INSERT(T, z) 1. y = T.nil 2. x = T.root 3. while x=T.nil 4. y = x 5. if z.key<x.key 6. x = x.left 7. else x = x.right 8. z.p = y 9. if y == T.nil 10. T.root = z 11. elseif z.key < y.key 12. y.left = z 13. else y.right = z 14. z.left = T.nil 15. z.right = T.nil 16. z.color = T.RED 17. RB-Insert-Fixup(T.z) Eight Call RB-Insert-Fixup!!! 41 / 149
  • 79. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 42 / 149
  • 80. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 81. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 82. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 83. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 84. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 85. Images/ITESM.p Tree-Insert Invariance Initialization: Prior to the first iteration of the loop, we started with a Red-Black Trees with no violations. Then, the algorithm insert the red node z at the bottom of the Red-Black Trees. This does not violate properties 1,3 and 5. The RB-Insert-Fixup is called because the following possible violations 1 If z is the first node to be inserted, you violate the property 2. 2 Then, it is the only violation on the entire Red-Black Trees. 3 If z is not the first node to be inserted than you could be violating property 4. 43 / 149
  • 86. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 44 / 149
  • 87. Images/ITESM.p RB-Insert-Fixup RB-Insert-Fixup(T,z) 1 while z.p.color == RED 2 if z.p == z.p.p.left 3 y=z.p.p.right 4 if y.color ==RED 5 z.p.color = BLACK 6 y.color = BLACK 7 z.p.p.color = RED 8 z = z.p.p 9 else if z == z.p.right 10 z = z.p 11 Left-Rotate(T, z) 12 z.p.color = BLACK 13 z.p.p.color = RED 14 Right-Rotate(T, z.p.p) 15 else (“right” and “left” exchanged) 16 T.root.color = BLACK Case 1 z’s uncle is RED Change of parent and uncle’s color to BLACK Move problem to z’s grandfather 45 / 149
  • 88. Images/ITESM.p Maintenance in Insertion in Red-Black Trees Case I - z’s uncle is red z yA B C D 46 / 149
  • 89. Images/ITESM.p Maintenance in Insertion in Red-Black Trees Recolor parent and uncle to black new z A B C D 47 / 149
  • 90. Images/ITESM.p Thus Observations 1 Recoloring will fix the z.parent.color == red and keeps the bh property 1 It will move the problem upwards. 2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees. 3 So the new z will move the problem upward for the next iteration. 48 / 149
  • 91. Images/ITESM.p Thus Observations 1 Recoloring will fix the z.parent.color == red and keeps the bh property 1 It will move the problem upwards. 2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees. 3 So the new z will move the problem upward for the next iteration. 48 / 149
  • 92. Images/ITESM.p Thus Observations 1 Recoloring will fix the z.parent.color == red and keeps the bh property 1 It will move the problem upwards. 2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees. 3 So the new z will move the problem upward for the next iteration. 48 / 149
  • 93. Images/ITESM.p Thus Observations 1 Recoloring will fix the z.parent.color == red and keeps the bh property 1 It will move the problem upwards. 2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees. 3 So the new z will move the problem upward for the next iteration. 48 / 149
  • 94. Images/ITESM.p The Symmetric Case We have z y A B C D 49 / 149
  • 95. Images/ITESM.p The Symmetric Case We have new z A B C D 50 / 149
  • 96. Images/ITESM.p RB-Insert-Fixup RB-Insert-Fixup(T,z) 1 while z.p.color == RED 2 if z.p == z.p.p.left 3 y=z.p.p.right 4 if y.color ==RED 5 z.p.color = BLACK 6 y.color = BLACK 7 z.p.p.color = RED 8 z = z.p.p 9 else if z == z.p.right 10 z = z.p 11 Left-Rotate(T, z) 12 z.p.color = BLACK 13 z.p.p.color = RED 14 Right-Rotate(T, z.p.p) 15 else (“right” and “left” exchanged) 16 T.root.color = BLACK Case 2 if z is in the right child then Move the problem to the parent by making z = z.p Rotate left using z as the rotation node 51 / 149
  • 97. Images/ITESM.p Insertion in Red-Black Trees Case 2 - z’s uncle y is black and z is a right child z y A B C CASE 2 52 / 149
  • 98. Images/ITESM.p Thus First Here simple recoloring will not work. Rotate We rotate first from to left using z.p. This moves case 2 toward case 3 To get ready for the final fix-up. 53 / 149
  • 99. Images/ITESM.p Thus First Here simple recoloring will not work. Rotate We rotate first from to left using z.p. This moves case 2 toward case 3 To get ready for the final fix-up. 53 / 149
  • 100. Images/ITESM.p Thus First Here simple recoloring will not work. Rotate We rotate first from to left using z.p. This moves case 2 toward case 3 To get ready for the final fix-up. 53 / 149
  • 101. Images/ITESM.p From Case 2 to Case 3 Example z y A B C z y A B CASE 2 CASE 3 C 54 / 149
  • 102. Images/ITESM.p RB-Insert-Fixup RB-Insert-Fixup(T,z) 1 while z.p.color == RED 2 if z.p == z.p.p.left 3 y=z.p.p.right 4 if y.color ==RED 5 z.p.color = BLACK 6 y.color = BLACK 7 z.p.p.color = RED 8 z = z.p.p 9 else if z == z.p.right 10 z = z.p 11 Left-Rotate(T, z) 12 z.p.color = BLACK 13 z.p.p.color = RED 14 Right-Rotate(T, z.p.p) 15 else (“right” and “left” exchanged) 16 T.root.color = BLACK Case 3 if z is in the left child then Recolor z’s parent to BLACK recolor z’s grandparent to RED Rotate right using the grandparent 55 / 149
  • 103. Images/ITESM.p Then We do the following Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ are black nodes) Finally Then, you rotate right using z.p.p to fix the black height property!! 56 / 149
  • 104. Images/ITESM.p Then We do the following Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ are black nodes) Finally Then, you rotate right using z.p.p to fix the black height property!! 56 / 149
  • 105. Images/ITESM.p From Case 3 to final recoloring Case 3 - It allows to fix the bh locally z y A B CASE 3 B A C C 57 / 149
  • 106. Images/ITESM.p Finally Root Recoloring After pushing the problem up to the root by the while loop color the root to BLACK!!! We have then A B Root A B Root 58 / 149
  • 107. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 59 / 149
  • 108. Images/ITESM.p Example: Insertion in Red-Black Trees Example Tree is empty 26 1741 14 21 10 712 3028 T.nil Selection Order 60 / 149
  • 109. Images/ITESM.p Example: Insertion in Red-Black Trees The root becomes red!!! STEPS 1 y = T.nil 2 x = T.root 3 We never go into the search loop 4 z.p = y 5 if y == T.nil 6 T.root = z 7 z.left = T.nil 8 z.right = T.nil 9 z.color = T.RED 26 1741 14 21 10 712 30 28 T.nil Selection Order z 61 / 149
  • 110. Images/ITESM.p Example: Insertion in Red-Black Trees Fix the problem ONLY STEP 1 T.root.color=BLACK It happens at the end of the Fix-up 26 1741 14 21 10 712 30 28 T.nil Selection Order z 62 / 149
  • 111. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 7 1 Do a binary search to find a place to insert 2 Parent of z is not RED ⇒ no fix-up 26 1741 14 21 10 7 12 30 28 T.nil Selection Order z 63 / 149
  • 112. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 12 1 Do a binary search to find a place to insert 2 Parent of z is RED!!! 26 1741 14 21 10 7 12 30 28 T.nil Selection Order z 64 / 149
  • 113. Images/ITESM.p Example: Insertion in Red-Black Trees Case 2 into Case 3 1 Re-balance first to the left if z==z.p.right z=z.p Left-Rotate(T, z) 26 1741 14 21 10 712 30 28 T.nil Selection Order 7z 65 / 149
  • 114. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Right-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 1741 14 21 10 712 30 28 T.nil Selection Order 7z 66 / 149
  • 115. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 10 1 We insert 10 2 We color it to RED 3 We need to fix the problem 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z y 67 / 149
  • 116. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 1 if z.p == z.p.p.left 2 y=z.p.p.right 3 if y.color ==RED 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z y 68 / 149
  • 117. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 1 if y.color ==RED 2 z.p.color = BLACK 3 y.color = BLACK 4 z.p.p.color = RED 5 z = z.p.p 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z 69 / 149
  • 118. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 You get out of the loop and... 1 T.root.color = BLACK 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z 70 / 149
  • 119. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 28 1 We insert 28 2 We color it to RED 3 No problem to fix... 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z 71 / 149
  • 120. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 28 1 We insert 28 2 We color it to RED 3 No problem to fix... 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z 72 / 149
  • 121. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 21 1 We insert 21 2 We color it to RED 3 We need to fix... 26 1741 14 21 10 712 30 28 T.nil Selection Order 7 z 73 / 149
  • 122. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Right-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 1741 14 2110 712 30 28 T.nil Selection Order 7 z 74 / 149
  • 123. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 14 1 We insert 14 2 We color it to RED 3 We need to fix... 26 1741 14 2110 712 30 28 T.nil Selection Order 7 z 75 / 149
  • 124. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 1 if z.p == z.p.p.left 2 y=z.p.p.right 3 if y.color ==RED 26 1741 14 2110 712 30 28 T.nil Selection Order 7 z y 76 / 149
  • 125. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 1 if y.color ==RED 2 z.p.color = BLACK 3 y.color = BLACK 4 z.p.p.color = RED 5 z = z.p.p Nothing to do after!!! 26 1741 14 2110 712 30 28 T.nil Selection Order 7 z 77 / 149
  • 126. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 17 1 Do a binary search to find a place to insert 2 Parent of z is RED!!! 26 17 41 14 2110 712 30 28 T.nil Selection Order 7 z z 78 / 149
  • 127. Images/ITESM.p Case 2 into 3 1 Re-balance first to the left if z==z.p.right z=z.p Left-Rotate(T, z) 26 17 41 14 2110 712 30 28 T.nil Selection Order 7 z 79 / 149
  • 128. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Right-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 17 41 14 2110 712 30 28 T.nil Selection Order 7 z 79 / 149
  • 129. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Right-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 17 41 14 2110 712 30 28 T.nil Selection Order 7 z 80 / 149
  • 130. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Right-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 81 / 149
  • 131. Images/ITESM.p Example: Insertion in Red-Black Trees Insert 41 1 Put in the correct node by binary search 2 Because 41’s parent is BLACK nothing to fix 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 82 / 149
  • 132. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 Symmetrical 1 if z.p == z.p.p.right 2 y=z.p.p.left 3 if y.color ==RED 4 z.p.color = BLACK 5 y.color = BLACK 6 z.p.p.color = RED 7 z = z.p.p 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 83 / 149
  • 133. Images/ITESM.p Example: Insertion in Red-Black Trees Case 1 Symmetrical 1 if z.p == z.p.p.right 2 y=z.p.p.left 3 if y.color ==RED 4 z.p.color = BLACK 5 y.color = BLACK 6 z.p.p.color = RED 7 z = z.p.p 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 84 / 149
  • 134. Images/ITESM.p Example: Insertion in Red-Black Trees Case 2 to case 3 - Symmetrical 1 Re-balance first to the right if z==z.p.left z=z.p Right-Rotate(T, z) 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 85 / 149
  • 135. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 - Symmetrical 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Left-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 86 / 149
  • 136. Images/ITESM.p Example: Insertion in Red-Black Trees Case 3 - Symmetrical 1 Re-color and re-balance to the right z.p.color = BLACK z.p.p.color=RED Left-Rotate(T, z.p.p) 2 No problem to fix 3 The root is already BLACK so nothing happens 26 17 41 14 21 10 712 30 28 T.nil Selection Order 7 z 87 / 149
  • 137. Images/ITESM.p Complexity of RB-Insert It is easy to see that we have O(log n) (1) 88 / 149
  • 138. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 89 / 149
  • 139. Images/ITESM.p The Main Idea Here, we use the idea of removing By pushing in its place its successor. Therefore we have a problem If the successor of a node is the node y. And y is black. We have removed a black node from a path. 90 / 149
  • 140. Images/ITESM.p The Main Idea Here, we use the idea of removing By pushing in its place its successor. Therefore we have a problem If the successor of a node is the node y. And y is black. We have removed a black node from a path. 90 / 149
  • 141. Images/ITESM.p The Main Idea Here, we use the idea of removing By pushing in its place its successor. Therefore we have a problem If the successor of a node is the node y. And y is black. We have removed a black node from a path. 90 / 149
  • 142. Images/ITESM.p The Main Idea Here, we use the idea of removing By pushing in its place its successor. Therefore we have a problem If the successor of a node is the node y. And y is black. We have removed a black node from a path. 90 / 149
  • 144. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y Case 1 Store the info of the node to be deleted 92 / 149
  • 145. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y Case 2 If the left child is empty Store the info of the right child Move z.right into the position of z 93 / 149
  • 146. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y Case 3 If the right child is empty Store the info of the left child Move z.left into the position of z 94 / 149
  • 147. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y Case 4 Find the successor of z Store the info of it: Color and right child 95 / 149
  • 148. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y Case 5 If parent of succesor is z then set parent of x to y 96 / 149
  • 149. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 1 y = z 2 y-original-color = y.color 3 if z.left == T.nil 4 x = z.right 5 RB-Transplant(T, z, z.right) 6 elseif z.right == T.nil 7 x = z.left 8 RB-Transplant(T, z, z.left) 9 else y = Tree-Minimum(z.right) 10 y-original-color = y.color 11 x = y.right 12 if y.p == z 13 x.p = y 14 else RB-Transplant(T,y,y.right) 15 y.right = z.right 16 y.right.p = y ø Case 6 Substitute y with y.right set y.right with z.right set the parent of y.right to y 97 / 149
  • 150. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 17. RB-Transplant(T, z, y) 18. y.left = z.left 19. y.left.p = y 20. y.color = z.color 21. if y-original-color ==BLACK 22. RB-Delete-Fixup(T,x) Case 7 Substitute z with y Make y.left to z.left Make the parent of y.left to y Make the color of y to the color of z 98 / 149
  • 151. Images/ITESM.p Deletion in Red-Black Trees RB-DELETE(T,z) 17. RB-Transplant(T, z, y) 18. y.left = z.left 19. y.left.p = y 20. y.color = z.color 21. if y-original-color ==BLACK 22. RB-Delete-Fixup(T,x) Case 8 If y-original-color == BLACK then call RB-Delete-Fixup(T,x) After all x points to the node that: It is moved into the position of y. Where y was moved into the position of z. 99 / 149
  • 152. Images/ITESM.p Where RB-Transplant RB-Transplant(T, u, v) 1 if u.p == T.nil 2 T.root = v 3 elseif u == u.p.left 4 u.p.left = v 5 else u.p.right = v 6 v.p = u.p 100 / 149
  • 153. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 101 / 149
  • 154. Images/ITESM.p A virtual node y to be removed or moved around Case 1 In line 1, y is removed when it points to z and has less than two children. Case 2 In line 9, y is moved around when z has two children Because y=Tree-Minimum(z.right). Then y will move to z’s position. 102 / 149
  • 155. Images/ITESM.p A virtual node y to be removed or moved around Case 1 In line 1, y is removed when it points to z and has less than two children. Case 2 In line 9, y is moved around when z has two children Because y=Tree-Minimum(z.right). Then y will move to z’s position. 102 / 149
  • 156. Images/ITESM.p A virtual node y to be removed or moved around Case 1 In line 1, y is removed when it points to z and has less than two children. Case 2 In line 9, y is moved around when z has two children Because y=Tree-Minimum(z.right). Then y will move to z’s position. 102 / 149
  • 157. Images/ITESM.p A virtual node y to be removed or moved around Case 1 In line 1, y is removed when it points to z and has less than two children. Case 2 In line 9, y is moved around when z has two children Because y=Tree-Minimum(z.right). Then y will move to z’s position. 102 / 149
  • 158. Images/ITESM.p A virtual node y to be removed or moved around Now, the color of y’s can change Therefore, it gets stored in y-original-color (Lines 2, 10). Then, when z has two children Then y moves to z’s position and y gets the same color than z. This can produce a violation. 103 / 149
  • 159. Images/ITESM.p A virtual node y to be removed or moved around Now, the color of y’s can change Therefore, it gets stored in y-original-color (Lines 2, 10). Then, when z has two children Then y moves to z’s position and y gets the same color than z. This can produce a violation. 103 / 149
  • 160. Images/ITESM.p A virtual node y to be removed or moved around Now, the color of y’s can change Therefore, it gets stored in y-original-color (Lines 2, 10). Then, when z has two children Then y moves to z’s position and y gets the same color than z. This can produce a violation. 103 / 149
  • 161. Images/ITESM.p The node x gets position y In lines 4, 7, and 11, x is set to point to to y’s only child or T.nil Since x is going to move to y’s original position The x.p is pointed to y’s parent. Unless z is y’s original parent The assignment of x.p takes place in line 6 of RB-Transplant. Observe that when RB-Transplant is called in lines 5, 8, or 14, the second parameter passed is the same as x. 104 / 149
  • 162. Images/ITESM.p The node x gets position y In lines 4, 7, and 11, x is set to point to to y’s only child or T.nil Since x is going to move to y’s original position The x.p is pointed to y’s parent. Unless z is y’s original parent The assignment of x.p takes place in line 6 of RB-Transplant. Observe that when RB-Transplant is called in lines 5, 8, or 14, the second parameter passed is the same as x. 104 / 149
  • 163. Images/ITESM.p The node x gets position y In lines 4, 7, and 11, x is set to point to to y’s only child or T.nil Since x is going to move to y’s original position The x.p is pointed to y’s parent. Unless z is y’s original parent The assignment of x.p takes place in line 6 of RB-Transplant. Observe that when RB-Transplant is called in lines 5, 8, or 14, the second parameter passed is the same as x. 104 / 149
  • 164. Images/ITESM.p The node x gets position y In lines 4, 7, and 11, x is set to point to to y’s only child or T.nil Since x is going to move to y’s original position The x.p is pointed to y’s parent. Unless z is y’s original parent The assignment of x.p takes place in line 6 of RB-Transplant. Observe that when RB-Transplant is called in lines 5, 8, or 14, the second parameter passed is the same as x. 104 / 149
  • 165. Images/ITESM.p The node x gets position y In lines 4, 7, and 11, x is set to point to to y’s only child or T.nil Since x is going to move to y’s original position The x.p is pointed to y’s parent. Unless z is y’s original parent The assignment of x.p takes place in line 6 of RB-Transplant. Observe that when RB-Transplant is called in lines 5, 8, or 14, the second parameter passed is the same as x. 104 / 149
  • 166. Images/ITESM.p The node x gets position y if z is not the original y’s parent We do not want x.p to point to it since we are going to remove it. Then In line 13 of RB-Delete, x.p is set to point to y. Finally y will take the position of z in line 17. 105 / 149
  • 167. Images/ITESM.p The node x gets position y if z is not the original y’s parent We do not want x.p to point to it since we are going to remove it. Then In line 13 of RB-Delete, x.p is set to point to y. Finally y will take the position of z in line 17. 105 / 149
  • 168. Images/ITESM.p The node x gets position y if z is not the original y’s parent We do not want x.p to point to it since we are going to remove it. Then In line 13 of RB-Delete, x.p is set to point to y. Finally y will take the position of z in line 17. 105 / 149
  • 169. Images/ITESM.p The node x gets position y Then If y was originally black after taking the z.color can produce a violation, then RB-Delete-Fixup is called. This can happen If y was originally red the Red-Black Trees properties still hold. 106 / 149
  • 170. Images/ITESM.p The node x gets position y Then If y was originally black after taking the z.color can produce a violation, then RB-Delete-Fixup is called. This can happen If y was originally red the Red-Black Trees properties still hold. 106 / 149
  • 171. Images/ITESM.p Deletion in Red-Black Trees Question What if we removed a black node? 107 / 149
  • 172. Images/ITESM.p Explanation We have three problems 1 If y was a root and a RED child becomes the new root, we have violated property 2. 2 If both x and x.p are RED, then we have violated property 4. 3 Moving y around decreases the black-height on a section of the Red Black Tree. 4 Thus, Property 5 is violated. 108 / 149
  • 173. Images/ITESM.p Explanation We have three problems 1 If y was a root and a RED child becomes the new root, we have violated property 2. 2 If both x and x.p are RED, then we have violated property 4. 3 Moving y around decreases the black-height on a section of the Red Black Tree. 4 Thus, Property 5 is violated. 108 / 149
  • 174. Images/ITESM.p Explanation We have three problems 1 If y was a root and a RED child becomes the new root, we have violated property 2. 2 If both x and x.p are RED, then we have violated property 4. 3 Moving y around decreases the black-height on a section of the Red Black Tree. 4 Thus, Property 5 is violated. 108 / 149
  • 175. Images/ITESM.p Explanation We have three problems 1 If y was a root and a RED child becomes the new root, we have violated property 2. 2 If both x and x.p are RED, then we have violated property 4. 3 Moving y around decreases the black-height on a section of the Red Black Tree. 4 Thus, Property 5 is violated. 108 / 149
  • 176. Images/ITESM.p Fix-up How? This could be fixed assuming that x has an “extra black.” Meaning This means that the node is “doubly black” or “red-and-black.” 109 / 149
  • 177. Images/ITESM.p Fix-up How? This could be fixed assuming that x has an “extra black.” Meaning This means that the node is “doubly black” or “red-and-black.” 109 / 149
  • 178. Images/ITESM.p Example We have then x Doubly Black Case Virtual Black x Red-and-Black Case Virtual Black 110 / 149
  • 179. Images/ITESM.p How is this done? Thus The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by using the while loop to push the extra BLACK node up the tree. Until If we have that x is a red-and-black, we simply need to change the color of the node to BLACK (Line 23). Then, use Rotations Use suitable rotations and re-colorings until x stops to be a doubly black node. If we have that x is pointing to the root, remove “extra node.” 111 / 149
  • 180. Images/ITESM.p How is this done? Thus The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by using the while loop to push the extra BLACK node up the tree. Until If we have that x is a red-and-black, we simply need to change the color of the node to BLACK (Line 23). Then, use Rotations Use suitable rotations and re-colorings until x stops to be a doubly black node. If we have that x is pointing to the root, remove “extra node.” 111 / 149
  • 181. Images/ITESM.p How is this done? Thus The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by using the while loop to push the extra BLACK node up the tree. Until If we have that x is a red-and-black, we simply need to change the color of the node to BLACK (Line 23). Then, use Rotations Use suitable rotations and re-colorings until x stops to be a doubly black node. If we have that x is pointing to the root, remove “extra node.” 111 / 149
  • 182. Images/ITESM.p How is this done? Thus The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by using the while loop to push the extra BLACK node up the tree. Until If we have that x is a red-and-black, we simply need to change the color of the node to BLACK (Line 23). Then, use Rotations Use suitable rotations and re-colorings until x stops to be a doubly black node. If we have that x is pointing to the root, remove “extra node.” 111 / 149
  • 183. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 112 / 149
  • 184. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK While loop Because a violation on the bh, you need to move x up until the problem is fixed up. 113 / 149
  • 185. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK Finding who you are Find which child are you Make w the other child 114 / 149
  • 186. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK Case 1 if x is BLACK and w is RED Fix the bh problem by making w BLACK, x’s parent to RED then rotate left using x’s parent Make w = x.p.right moving the problem down. 115 / 149
  • 187. Images/ITESM.p Suitable Rotations and Recoloring Case 1 - x’s sibling w is red. You keep the bh property of the other subtrees x wA B C D E 116 / 149
  • 188. Images/ITESM.p Suitable Rotations and Recoloring Case 1 - x’s sibling w is red. You keep the bh property of the other subtrees x new wA B C D E 117 / 149
  • 189. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK Case 2 Now if w.left’s color and w.right’s color is BLACK We do something smart decrease the bh height at w by making w’s color to RED Move the problem fropm to x to x.p (After all the subtree have the same height at x.p) 118 / 149
  • 190. Images/ITESM.p Suitable Rotations and Recoloring Case 2 - x’s sibling w is black, and both of w’s children are black. x wA C D E B Note: The Node with half and half colors has the meaning that it can be red or black. 119 / 149
  • 191. Images/ITESM.p Suitable Rotations and Recoloring Case 2 - x’s sibling w is black, and both of w’s children are black. new x A C D E B 120 / 149
  • 192. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK Case 3 If w.right’s color is BLACK We do something smart, we re-color and do a right rotation at w This does not change the Red-Black Trees properties of w but prepare the situation for fixing the x height problem in case 4. 121 / 149
  • 193. Images/ITESM.p Suitable Rotations and Recoloring Case 3 - x’s sibling w is black, w’s left child is red, and w’s right child is black. x A C D E B w 122 / 149
  • 194. Images/ITESM.p Suitable Rotations and Recoloring Case 3: x’s sibling w is black, w’s left child is red, and w’s right child is black. x A C D E B new w 123 / 149
  • 195. Images/ITESM.p RB-DELETE-FIXUP(T,x) 1 while x = T.root and x.color == BLACK 2 if x == x.p.left 3 w = x.p.right 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 = RED 11 x = x.p 12 else if w.right.color == BLACK 13 w.left.color = BLACK 14 w.color = RED 15 Right-Rotate(T, w) 16 w = x.p.right 17 w.color = x.p.color 18 x.p.color = BLACK 19 w.right.color = BLACK 20 Left-Rotate(T, x.p) 21 x = T.root 22 else (same with “right” and “left” exchanged) 23 x.color = BLACK Case 4 We are ready to fix our problem!!! with respect to x (Case 2 and 3 where a preparation to fix the problem) We increase the height of the bh with the problem, x. 124 / 149
  • 196. Images/ITESM.p Suitable Rotations and Recoloring Case 4: x’s sibling w is black, and w’s right child is red. x A E B w C D 125 / 149
  • 197. Images/ITESM.p Suitable Rotations and Recoloring Case 4: x’s sibling w is black, and w’s right child is red. new x=T.root A EB C D 126 / 149
  • 198. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 127 / 149
  • 199. Images/ITESM.p Deletion in Red-Black Trees Delete 17 26 17 41 14 21 10 712 30 28 T.nil 7 z 128 / 149
  • 200. Images/ITESM.p Deletion in Red-Black Trees Store the info about z y = z y-original-color = y.color 26 17 41 14 21 10 712 30 28 T.nil 7 z 129 / 149
  • 201. Images/ITESM.p Deletion in Red-Black Trees None of the children of z are T.nil, thus else y = Tree-Minimum(z.right) y-original-color = y.color x = y.right 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 130 / 149
  • 202. Images/ITESM.p Deletion in Red-Black Trees We have that y.p= z else RB-Transplant(T,y,y.right) y.right = z.right y.right.p = y 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 131 / 149
  • 203. Images/ITESM.p Deletion in Red-Black Trees Transplant(T, y, y.right) elseif y == y.p.left y.p.left = y.right y.right.p = y.p 26 17 41 14 21 10 712 30 28 T.nil 7 z y x y.right.p=y.p 132 / 149
  • 204. Images/ITESM.p Deletion in Red-Black Trees Now, we move pointers y.right = z.right y.right.p = y 26 17 41 14 21 10 712 30 28 T.nil 7 z y x y.right.p=y 133 / 149
  • 205. Images/ITESM.p Deletion in Red-Black Trees Transplant(z,y) if z.p == T.nil T.root = y y.p = z.p 26 17 41 14 21 10 712 30 28 T.nil 7 z y x y.p = z.p 134 / 149
  • 206. Images/ITESM.p Deletion in Red-Black Trees Move pointers y.left = z.left y.left.p = y y.color = z.color 26 17 41 14 21 10 712 30 28 T.nil 7 z y x y.left.p = y 135 / 149
  • 207. Images/ITESM.p Deletion in Red-Black Trees We remove z safely and we go into Delete-Fixup(T,x) z.right = NULL z.left = NULL z.parent = NULL 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 136 / 149
  • 208. Images/ITESM.p Deletion in Red-Black Trees We remove z safely and we go into Delete-Fixup(T,x) Never enter into the loop Simply do x.color = BLACK 26 41 14 21 10 712 30 28 T.nil 7 y x 137 / 149
  • 209. Images/ITESM.p Deletion in Red-Black Trees Delete 12 26 17 41 14 21 10 712 30 28 T.nil 7 z 138 / 149
  • 210. Images/ITESM.p Deletion in Red-Black Trees None of the children of z are T.nil, thus else y = Tree-Minimum(z.right) y-original-color = y.color (BLACK) x = y.right (T.NIL) 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 139 / 149
  • 211. Images/ITESM.p Deletion in Red-Black Trees We have that y.p== z if y.p == z x.p = y 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 140 / 149
  • 212. Images/ITESM.p Deletion in Red-Black Trees Next RB-Transplant(T, z, y) y.left = z.left y.left.p = y y.color = z.color 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 141 / 149
  • 213. Images/ITESM.p Deletion in Red-Black Trees Next RB-Transplant(T, z, y) y.left = z.left y.left.p = y y.color = z.color 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 142 / 149
  • 214. Images/ITESM.p Deletion in Red-Black Trees Next RB-Transplant(T, z, y) y.left = z.left y.left.p = y y.color = z.color 26 17 41 14 21 10 712 30 28 T.nil 7 z y x 143 / 149
  • 215. Images/ITESM.p Deletion in Red-Black Trees We remove z safely and we go into Delete-Fixup(T,x) enter into the loop if x == x.p.right w = x.p.left 26 17 41 14 21 10 30 28 T.nil 7 y x w 144 / 149
  • 216. Images/ITESM.p Deletion in Red-Black Trees We enter into case 4 1 w.color = x.p.color 2 x.p.color = BLACK 3 w.right.color = BLACK 26 17 41 14 21 10 30 28 T.nil 7 x w 145 / 149
  • 217. Images/ITESM.p Deletion in Red-Black Trees Rotate and move the x 1 Left-Rotate(T, x.p) 2 x = T.root 26 17 41 14 21 10 30 28 T.nil 7 x 146 / 149
  • 218. Images/ITESM.p Applications of Red-Black Trees Completely Fair Scheduler (CFS) It is a task scheduler which was merged into 2.6.23 release of the Linux Kernel. It is a replacement of earlier O(1) scheduler. CFS algorithm was designed to maintain balance (fairness) in providing processor time to tasks. Sorting using Parallel Implementations Running in O(log log n) time 147 / 149
  • 219. Images/ITESM.p Applications of Red-Black Trees Completely Fair Scheduler (CFS) It is a task scheduler which was merged into 2.6.23 release of the Linux Kernel. It is a replacement of earlier O(1) scheduler. CFS algorithm was designed to maintain balance (fairness) in providing processor time to tasks. Sorting using Parallel Implementations Running in O(log log n) time 147 / 149
  • 220. Images/ITESM.p Outline 1 Red-Black Trees The Search for Well Balanced Threes Red-Black Trees Lemma for the height of Red-Black Trees Base Case of Induction Induction Rotations in Red-Black Trees Insertion in Red-Black Trees Insertion Code The Insertion Invariance The Fixup Code Example Deletion in Red-Black Trees A Virtual Node y The Code To Fix the Violations Example of Deletion in Red-Black Trees 2 Exercises Something for you to do 148 / 149
  • 221. Images/ITESM.p Exercises From Cormen’s book solve 13.1-1 13.1-3 13.1-5 13.1-7 13.2-2 13.2-3 13.2-4 13.2-5 13.3-2 13.3-4 13.4-2 13.4-4 149 / 149