Red-Black Trees-Again: Rank (X) X X X Rank (External Node) 0
Red-Black Trees-Again: Rank (X) X X X Rank (External Node) 0
rank(x) = # black pointers on path from x to an external node. Same as #black nodes (excluding x) from x to an external node. rank(external node) = 0.
An Example
3
10
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
1 1
35
45
1 60 0
0 0 0 0 0
0 0 0
25
0 0
Properties Of rank(x) 3
10
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
Properties Of rank(x) 3
10
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
p(x) exists => rank(x) <= rank(p(x)) <= rank(x) + 1. g(x) exists => rank(x) < rank(g(x)).
Red-Black Tree
A binary search tree is a red-black tree iff integer ranks can be assigned to its nodes so as to satisfy the stated 4 properties of rank.
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
(p(x),x) is a red pointer iff rank(x) = rank(p(x)). (p(x),x) is a black pointer iff rank(x) = rank(p(x)) 1.
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
Given rank(root) and node/pointer colors, remaining ranks may be computed on way down.
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
2 1 1
1 3
2
7 40
1 1
5
2 30 1
20
45
1
1
25
35
1 60
0
0 0
0
0
0 0
Join(S,m,B)
Input
Dictionary S of pairs with small keys. Dictionary B of pairs with big keys. An additional pair m. All keys in S are smaller than m.key. All keys in B are bigger than m.key.
Output
A dictionary that contains all pairs in S and B plus the pair m. Dictionaries S and B may be destroyed.
O(1) time.
p(x) S a x b
p(x) m x a b B
S
a
x
x b a b
Follow left child pointers from root of B to first node x whose rank equals rank(S). Similar to case when rank(S) > rank(B).
Split(k)
Inverse of join. Obtain
S dictionary of pairs with key < k. B dictionary of pairs with key > k. m pair with key = k (if present).
a
C
B
b D E d m f g
B
C b D E d m f g a
A
C a D E d m f g
A
a D E
A
a
e
f
m g
A
a
A
a
S=f
B
B=g
a
C
b D
c
E
d m
e
f
S=f
B
B=g
a
C
b D
c
E
S=f
B
B=g
a
C
S = join(e, E, S)
b
c
E
D d
S=f
B
B =g
a
C
S = join(e, E, S)
b
B = join(B, D, d)
D d
S=f
B
B =g
a
C
S = join(e, E, S)
b
B = join(B, D, d) S = join(c, C, S)
S=f
B
b
B =g
S = join(e, E, S)
B = join(B, D, d) S = join(c, C, S) B = join(B, B, b)
S=f
B =g
S = join(e, E, S)
B = join(B, D, d) S = join(c, C, S) B = join(B, B, b)
S = join(a, A, S)
Complexity Of Split
O(log n)