10.SearchTrees 3 RBtree
10.SearchTrees 3 RBtree
6
v
3 8
z
4
1
From (2,4) to Red-Black Trees
A red-black tree is a representation of a (2,4) tree by means of a binary tree
whose nodes are colored red or black
In comparison with its associated (2,4) tree, a red-black tree has
n same logarithmic time performance
n simpler implementation with a single node type
4 3 5 2 6 7
4 5 3 6
3 OR 5 2 7
2
Red-Black Trees
A red-black tree can also be defined as a binary search tree that
satisfies the following properties:
n Root Property: the root is black
n External Property: every leaf is black
n Internal Property: the children of a red node are black (red rule)
n Depth Property: all the leaves have the same black depth (path rule)
n (Question) How is balancing enforced here?
4 15
2 6 12 21
3
Red Black Tree?
19
12 35
50
0
75
-10
-5 135
-8 100
-6 80
4
Red Back Tree?
19
12 35
3 16
0
What if we attach a child
to node 0?
5
Implications
Root Property: the root is black
External Property: every leaf is black
Internal Property: the children of a red node are black (red rule)
Depth Property: all the leaves have the same black depth (path rule)
1. If a red node has any children, it must have two children and they must be
black
n Why? Depth property
2. If a black node has only one “real” child then it must be a ”last” red node
n If the child is black?
n If the child is not the last red?
Why?
n From “red rule” and “path rule”
shortest path = only black nodes
longest path = inserting a red node between two black nodes
7
Height of a Red-Black Tree
Theorem: A red-black tree storing n entries has height O(log n)
Proof:
n Omitted
The search algorithm for a binary search tree is the same as that
for a binary search tree
8
Insertion
9
Insertion
To perform operation put(k, o), we execute the insertion algorithm for binary
search trees and color red the newly inserted node z unless it is the root
n We preserve the root, external, and depth properties
n If the parent v of z is black, we also preserve the internal property and we are done
n Else (v is red ) we have a double red (i.e., a violation of the internal property),
which requires a reorganization of the tree
n Goal: Removing double read without breaking the depth property
6 6
v v
3 8 3 8
z z
4
10
Remedying a Double Red
Consider a double red with child z and parent v, and let w be the sibling of v
w 4 v 4 v
w
2 7 2 7
z z
6 6
4 6 7 2 4 6 7
.. 2 ..
11
Restructuring
A restructuring remedies a child-parent double red when the parent red node
has a black sibling
It is equivalent to restoring the correct replacement of a 4-node
The internal property is restored and the other properties are preserved
z
4 6
w v v
2 7 4 7
z w
6 2
4 6 7 4 6 7
.. 2 .. .. 2 ..
12
Restructuring (cont.)
There are four restructuring configurations depending on whether the double
red nodes are left or right children
2 6 6 2
6 2 4 4
4 4 2 6
2 6
13
Recoloring
A recoloring remedies a child-parent double red when the parent red node has
a red sibling
The parent v and its sibling w become black and the grandparent u becomes
red, unless it is the root
It is equivalent to performing a split on a 5-node
The double red violation may propagate to the grandparent u
4 v 4 v
w w
2 7 2 7
z z
6 6
… 4 …
2 4 6 7
2 6 7
14
w v
RST
w v
RCL
w v
RST
w v
RCL 15
w v
RST
w v
RCL RST
16
Analysis of Insertion
17
RB-Tree: Deletion
18
Deletion: Example 1
To perform operation erase(k), we first execute the deletion algorithm for
binary search trees
10 10
1
5 30 5 35
3
-5 7 20 38 -5 7 20 38
null null null null null null 35 null null null null null null null 35 null
10
Just delete the copied 35, and
5 35 color the remaining node
in black. Then, we are done.
-5 7 20 38
null null null null null null null null Implication:
If the node to be deleted is red,
removing it is fine
19
Deletion: Example 2
To perform operation erase(k), we first execute the deletion algorithm for
binary search trees
10 10
5 30 5 32
-5 7 20 38 -5 7 20 38
null null null null null null 32 41 null null null null null null 32 41
delete 30 null 35 null null Copy inorder successor null 35 null null
10 20
-10 30 -10 30
null null
20 38 null null
20 38
null null null null null null null null
20
-10 30 Delete 20.
null null null 38 Problem: A path of only 2 blacks
null null
null null
22
Questions
l How to handle “double black nodes”
l It’s really, really complex, but if you concentrate, then you can follow it.
23
Deletion: Algorithm Overview (1)
First, remove v and w, and make r a 10 delete 30
child of x 5 30
-5 7 20 38
null null null null null null 35 null
If either of v or r was red, we
null null
color r black and we are done
(Examples 1 and 2) 10
5 35
x
-5 7 20 38
Else (v and r were both black) null null null null null null 35 v null
we color r double black, which null null
r
w
is a violation of the internal
10
property requiring a
5 35
reorganization of the tree
-5 7 20 38
(Examples 3)
null null null null null null null null
24
Deletion: Algorithm Overview (2)
First, remove v and w, and make r a 10
delete 30
child of x 5 30
-5 7 20 38
null null null null null null 32 41
If either of v or r was red, we null 35 null null
(Examples 1 and 2) 10
5 32 x
-5 7 20 38
Else (v and r were both black) null null null null
v
null null 32 41
25
Deletion: Algorithm Overview (2)
First, remove v and w, and make r a child of x
If either of v or r was red, we color r black and we are done (Examples 1 and 2)
(Let’s call this Case 0)
Else (v and r were both black) we color r double black, which is a violation of the
internal property requiring a reorganization of the tree (Examples 3)
26
Recall: Example 3. Notations again!
What about deleting a node with a black child?
10 20
x
-10 30 -10 30
v
null null
20 38 null null
20 38
null null null null null null null null
w r
27
Handling Double Black Nodes: Case 1
Case 1: The sibling y of r is black, and has a red child z
n We perform a restructuring, and we are done
29
Handling Double Black Nodes: Case 2
Case 2: The sibling y of r is black, and y’s both children are
black
n We perform a recoloring
n Case 2-2: x (r’s parent) is black
30
Handling Double Black Nodes: Case 3
Case 3: The sibling y of r is red
n We perform adjustment
w If y is the right child of x, then let z be the right child of y
w If y is the left child of x, then let z be the left child of y
n Case 3-1: z is the left child of y
Perform restructuring
Make y be the parent of x
Color y black and x red
(double black not yet solved)
n Case 3-2: z is the right child
à The sibling of r is black (why?)
of y à Similarly, we apply à Case 1 or Case 2 applies
31
Double Black Node Handling: Summary
The algorithm for remedying a double black node r with sibling y considers
three cases
Case 1: y is black and has a red child
n We perform a restructuring, and we are done
Case 2: y is black and its children are both black
n We perform a recoloring, which may propagate up the double black violation
Case 3: y is red
n We perform an adjustment, equivalent to choosing a different representation of a
3-node, after which either Case 1 or Case 2 applies
32
Example: Remove 3
w r r
33
Example: Remove 12
x
x
v
y
w r r
z
34
Example: Remove 17
v is red àCase 0
35
Example: Remove 18
x x
y v y
r
w r
36
Example: Remove 15
37
Example: Remove 16
x
y x
r
z y r
y is red à Case 3
y is the left child of x, thus z is node 4
(left child of y) à Case 3-1
Adjustment à node 14 becomes double
black à new y (sibling of x)
y has both black children, and x is red
n à Case 2-1, recoloring, then we’re done
38
Questions?