Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
CSE 2320 Notes 10: Red-Black Trees
(Last updated 10/7/06 6:59 PM) CLRS, 13.1-13.4, along with the test 2 cheat sheet STRUCTURAL PROPERTIES A red-black tree is a binary search tree whose height is logarithmic in the number of keys stored. 1. Every node is colored red or black. (Colors are only examined during insertion and deletion) 2. Every “leaf” (the sentinel) is colored black. 3. Both children of a red node are black. 4. Every simple path from a child of node X to a leaf has the same number of black nodes. This number is known as the black-height of X (bh(X)). These are not stored. Example:
= red = black
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
0 0 0 0
0 0
0 0 0 0
0 0
0 0
0 0 0 0
1 1
1
1 1
1
1
1 1
1
2
2
2
2
32
3
Observations: 1. A red-black tree with n internal nodes (“keys”) has height at most 2 lg(n+1). 2. If a node X is not a leaf and its sibling is a leaf, then X must be red. 3. There may be many ways to color a binary search tree to make it a red-black tree. 4. If the root is colored red, then it may be switched to black without violating structural properties.
2
ROTATIONS Technique for rebalancing in most balanced binary search tree schemes. Takes Θ(1) time.
A
B
C
a b c d
Right rotation at B(AKA rotating edge BA)
Left rotation at B(AKA rotating edge BC)
B
C
c d
A
a
b
B
A
ba
C
c
d
3 INSERTION 1. Start with unbalanced insert of a “data leaf” (both children are the sentinel). 2. Color of new node is _________. 3. May violate structural property 3. Leads to three cases, along with symmetric versions. The x pointer points at a red node whose parent might also be red. Case 1:
= red = black
A
A’
C
B
x
a b
c d
k
k
k
k
k+1
A
A’
C
B
a b
c d
k
k+1
k k
k+1
x
Case 2:
= red = black
A
B
D
C
x
b c
d e
k
k
k
k-1
k+1
a
D
C
d e
k
k-1
k+1
B
Ax
ba
k
k
c
4 Case 3:
= red = black
D
C
d e
k
k-1
k+1
B
Ax
ba
k
k
c
A
B
ba
k
k
k+1
C
D
d e
k-1
k
c
Example 1:
= red = black
40
20 60
10 30 50 70 Insert 15
40
20 60
10 30 50 70
15x
40
20 60
10 30 50 70
15
x1
5 Insert 13
40
20 60
10 30 50 70
15
13
2
x
40
20 60
10 30 50 70
13
15x
3
40
20 60
13 30 50 70
1510 Insert 75
1
40
20 60
13 30 50 70
1510 75x
40
20 60
13 30 50 70
1510 75
x
Insert 14
1
40
20 60
13 30 50 70
1510 75
14x
40
20 60
13 30 50 70
1510 75
14
x
1
40
20 60
13 30 50 70
1510 75
14
x Reset to black
6 Example 2:
140
40
20
10 30
100
60 120
50 110 13080
70 90
150
160
170
Insert 75
140
40
20
10 30
100
60 120
50 110 13080
70 90
150
160
170
75x
7
140
40
20
10 30
100
60 120
50 110 13080
70 90
150
160
170
75
x
1
140
40
20
10 30
100
60 120
50 110 13080
70 90
150
160
170
75
x
1
8
2
140
40
20
10 30
100
60
120
50
110 130
80
70 90
150
160
170
75
x
3
40
20
10 30
100
60
50 80
70 90
75
140
150
160
170
120
110 130
DELETION Start with one of the unbalanced deletion cases:
1. Deleted node is a “data leaf”.
a. Splice around to sentinel. b. Color of deleted node? Red ⇒ Done Black ⇒ Set temporary “double black” pointer (x) at sentinel. Determine which of four rebalancing cases applies.
2. Deleted node is parent of one “data leaf”.
a. Splice around to “data leaf” b. Color of deleted node? Red ⇒ Not possible Black ⇒ “data leaf” must be red. Change its color to black.
9 3. Node with key-to-delete is parent of two “data nodes”.
a. Steal key and data from successor (but not the color). b. Delete successor using the appropriate one of the previous two cases.
Case 1:
= red = black
A
B
Dx
b
c d e
k-2
k
k
k+1
a
A
C E
f
k-1 k-1
D
B
x
b c d
e
k-1
k
k
k+1
E
CA
f
k-1k-2
a Case 2:
= red = 0 = black = 1
A
B
Dx
b
c d e
k-2
k
k-1
k+color(B)
a
A
C E
f
k-2 k-2
= either
A
B
D
x
b
c d e
k-2
k-1
k-1
k+color(B)
a
A
C E
f
k-2 k-2
10 Case 3:
= red = 0 = black = 1
A
B
Dx
b
c d e
k-2
k
k-1
k+color(B)
a
A
C E
f
k-1 k-2
= either
A
B
D
x
b c
d
e
k-2
k
k-1
k+color(B)
a
A C
E
f
k-1
k-2
Case 4:
= red = 0 = black = 1
A
B
Dx
b
c d e
k-2+color(C)
k+color(C)
k-1+color(C)
k+color(B)+color(C)
a
A
C E
f
k-1 k-1+color(C)
= either
A
B
D
b
e
k+color(D)+color(C)
a
A
E
f
c d
C
k-1k-2+color(C)
k-1+color(C)
k+color(C)
k-1+color(C)
Example 3:
40
20 60
10 30 50 70
120
100 140
90 110 130 150
80
11 Delete 50
40
20 60
10 30 70
120
100 140
90 110 130 150
80
x
40
20 60
10 30 70
120
100 140
90 110 130 150
80
x
2
2 40
20 60
10 30 70
120
100 140
90 110 130 150
80
x
240
20 60
10 30 70
120
100 140
90 110 130 150
80x
If x reaches the root, then done. Only place in tree where this happens. Delete 60
12
40
20
10 30
70
120
100 140
90 110 130 150
80
Delete 70
40
20
10 30
120
100 140
90 110 130 150
80
x
1
40
20
10
30
120
100 140
90 110 130 150
80
x
2
40
20
10
30
120
100 140
90 110 130 150
80
x
If x reaches a red node, then change color to black and done. Delete 10
40
20
30
120
100 140
90 110 130 150
80
x
3
40
20
30
120
100 140
90 110 130 150
80
x
4
40
30 120
100 140
90 110 130 150
80
20
Delete 40
13
30 120
100 140
90 110 130 150
80
20
x
2 30 120
100 140
90 110 130 150
80
20
x
1
30
120
100
140
90 110
130 150
80
20
x
2
30
120
100
140
90 110
130 150
80
20
x
Delete 120
30 100
140
90 110
130
150
80
20
x
2
30 100
140
90 110
130
150
80
20
x
3
30
100 140
90
110
130
15080
20
x 4
140110
130
150
30
100
90
80
20
Delete 100
140
130
150
30 90
80
20
4
110
x
140
15030 90
80
20
110
130