Title: RedBlack Trees
1Red-Black Trees
- ??????? ???
- 0004425
- ???
- superman_at_it.soongsil.ac.kr
2Introduction
- In binary search tree,
- If its height is large, their performance may be
no good - Red-Black Trees are,
- Balance in order to guarantee that basic
dynamic-set operations take O(lgn) time in the
worst case
3Properties of RB trees
- RB tree has one extra bit of storage per node
- key, left, right, p and
- color, which can be either RED or BLACK
- The path length is more than twice, so tree is
approximately balanced
4Properties of RB trees
- External node
- If a child or the parent of a node dose not
exist, the pointer field of the node contains the
value NIL - leave
- Internal node
- key-bearing nodes
5Properties of RB trees
- A binary search tree is a RB tree if it satisfies
the following properties - 1. Every node is either red or black
- 2. Every leaf(NIL) is black
- 3. If a node is red, then both it children are
black - 4. Every simple path from a node to descendant
leaf contains the same number of black nodes
6Example of a RB tree
3
26
3
17
41
2
30
14
21
47
2
2
1
2
10
16
19
23
28
38
2
1
1
1
1
1
NIL
NIL
1
7
15
35
39
20
12
1
1
1
NIL
NIL
NIL
NIL
NIL
NIL
1
3
1
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
7A RB trees height
- Black-height bh(x)
- Number of black nodes on any path
- In property 4, all descending paths from the node
have a same number of black nodes
8A RB trees height
- A RB tree has height at most 2ln(n1)
- Subtree rooted at any node x contains at least
2bh(x)-1 - If a height of x is 0, then x must be a leaf, and
2bh(x)-10 internal nodes. - Consider a node x(positive height, two children).
- Each child has a black-height(bh(x) or bh(x)-1)
- Since height of a child of x is less than height
of x, Hypothesis to conclude that each child has
at least 2bh(x)-1-1 internal nodes - The subtree rooted at x consist at least
- (2bh(x)-1-1)(2bh(x)-1-1)1 2bh(x)-1
- Let h be the height of tree
- according to property 3, at least half nodes on
any simple path(not including root), must be
black - Consequently, the black-height of the root must
be at least h/2 - Thus, n?2h/2-1
- Taking logarithms on both sides lg(n1)?h/2, or
h?1lg(n1)
9Complexity of RB tree
- In this lemma
- The dynamic-set operations can be implemented in
O(lgn) time on RB tree
10Rotations
- Why we use Rotations
- The TREE_INSERT and TREE_DELETE takes O(lnn), but
its violate properties - To restore these properties, we must change the
colors of the nodes and also change the pointer
structure - We change the pointer structure through rotation
- local operation
- preserves the inorder key ordering
11Rotations
- When Left-Rotation on a node x, its right child y
is non NIL - When rotate left, y is the new root, with x as
ys left child and ys left child as xs right
child
y
x
Right-Rotate(T,y)
?
?
x
y
?
?
?
?
Left-Rotate(T,x)
12Left-Rotate(T,x)
- Both rotation operations run in O(1)
- Left-Rotate(T,x) Assumes that rightx?NIL
- 1 y?rightx
- 2 rightx?lefty
- 3 if lefty?NIL
- then plefty?x
- 5 py?px
- 6 if pxNIL
- then rootT?y
- else if xleftpx
- then leftpx?y
- else rightpx?y
- 11 lefty?x
- 12 px?y
13Left-Rotate(T,x)
7
x
11
4
y
6
18
9
3
19
14
2
22
17
12
Left-Rotate(T,x)
20
7
y
18
4
x
6
19
11
3
22
2
14
9
20
17
12
14Insertion
- Insertion in RB tree can be in O(lgn)
- when insert node x
- Use the Tree-Insert to insert node x into tree T
as if it were an ordinary binary search tree - color x red
- To guarantee the RB properties are preserved,
recoloring nodes and rotation - RB-Insert handles the various cases
- Because, it can arise as we fix up the modified
tree
15RB-Insert(T,x)
- Three major step
- Determine what violations of properties
- Examine the overall goal of the while loop
- Explore while loop is broken and see how they
accomplish the goal
16RB-Insert(T,x)
- RB-Insert(T,x)
- 1 Tree-Insert(T,x)
- 2 colorx?RED
- 3 while x?rootT and colorpxRED
- 4 do if pxleftppx
- then y?rightppx
- if coloryRED
- then colorpx?BLACK
- colory?BLACK
- colorppx?RED
- x?ppx
- else if xrightpx
- then x?px
- Left-Rotate(T,x)
- colorpx?BLACK
- colorppx?RED
- Right-Rotate(T,ppx)
- else(same as then clause with right and
left exchanged) - 18 colorrootT?BLACK
17RB-Insert(T,x)-case 1
new x
C
C
A
A
y
D
D
?
x
?
B
?
B
?
?
?
?
?
?
?
new x
C
C
B
B
y
D
D
?
?
x
?
?
?
A
A
?
?
?
?
?
18RB-Insert(T,x)-case 2,3
C
y
?
A
x
B
?
?
?
case 2
B
C
y
case 3
?
A
x
C
B
x
?
A
?
?
?
?
?
?
19RB-Insert(T,x)
11
11
y
14
2
14
2
x
7
15
1
7
15
1
5
8
5
8
y
case1
4
4
x
case2
11
7
y
14
11
x
7
2
x
8
15
2
14
1
5
8
5
4
1
15
case3
4
20Deletion
- it takes O(lgn) too
- sentinel-If we could ignore the boundary
conditions at leaf of the tree - dummy object that allows us to simplify boundary
conditions - sentinel NILT is represents NIL but it has all
the fields of the other elements(color field is
black and others are arbitrary values) - Deletion in RB tree, all NIL are replaced by
sentinel NILT - RB-Delete-Fixup is change colors and performs
rotations to restore properties
21RB-Delete(T,z)
RB-Delete(T,z) 1 if leftznillT or
rightznillT 2 then y?z 3 else
y?Tree-Successor(z) 4 if lefty?nillT 5
then x?lefty 6 else x?righty 7
px?py 8 if pynillT 9 then
rootT?x 10 else if yleftpy 11
then leftpy?x 12 else rightpy?x 13
if y?z 14 then keyz?keyy(if y has other
fields, copy them, too) 15 if coloryBLACK 16
then RB-Delete-Fixup(T,x) 17 return y
22RB-Delete(T,z)
- Tree-Delete and RB-Delete are like algorithms
- But all NIL in Tree-Delete have been replaced by
sentinel NILT - The test for whether x is NIL in Tree-Delete has
been removed, and the px?py is performed
unconditionally in RB-Delete if x is sentinel
NIL, its parent pointer points to the parent of
the spliced-out node y - if y is black, RB-Delete-Fixup is excuted
23RB-Delete-Fixup(T,z)
RB-Delete-Fixup(T,x) 1 while x?rootT and
colorxBLACK 2 do if xleftpx 3
then w?rightpx 4 if
colorwRED 5 then
colorw?BLACK 6
colorpx?RED 7
Left-Rotate(T,px) 8
w?rightpx 9 if colorleftwBLACK
and colorrightwBLACK 10 then
colorw?RED 11 x?px 12
else if colorrightwBLACK 13
then colorleftw?BLACK 14
colorw?RED 15
Right-Rotate(T,w) 16
w?rightpx 17
colorw?colorpx 18
colorpx?BLACK 19
colorrightw?BLACK 20
Left-Rotate(T,px) 21
x?rootT 22 else (same as then clause with
right and left exchanged) 23 colorx?BLACK
24RB-Delete(T,z)
D
B
E
case 1
new w
A
x
C
?
?
?
?
?
?
new x
c
c
B
B
w
D
D
x
A
A
case 2
E
C
E
C
?
?
?
?
?
?
?
?
?
?
?
?
25RB-Delete(T,z)
c
c
B
B
x
w
A
new w
D
x
C
A
case 3
?
?
?
E
C
?
?
D
?
?
?
?
?
E
?
?
c
c
B
D
w
D
x
B
A
case 4
E
c
c
E
C
A
C
?
?
?
?
?
?
?
?
?
?
?
?
new x rootT