Red-Black Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Red-Black Trees

Description:

Red-Black Trees Again. rank(x) = # black pointers on path from x to an external node. ... Dictionary B of pairs with big keys. An additional pair m. All keys in ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 35
Provided by: CISE9
Learn more at: https://www.cise.ufl.edu
Category:
Tags: big | black | red | trees

less

Transcript and Presenter's Notes

Title: Red-Black Trees


1
Red-Black TreesAgain
  • 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.

2
An Example
3
2
2
1
1
1
2
1
1
1
1
1
1
3
Properties Of rank(x)
  • rank(x) 0 for x an external node.
  • rank(x) 1 for x parent of external node.

4
Properties Of rank(x)
  • p(x) exists gt rank(x) lt rank(p(x)) lt rank(x)
    1.
  • g(x) exists gt rank(x) lt rank(g(x)).

5
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.

6
Relationship Between rank() And Color
  • (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.

7
Relationship Between rank() And Color
  • Root is black.
  • Other nodes
  • Red iff pointer from parent is red.
  • Black iff pointer from parent is black.

8
Relationship Between rank() And Color
  • Given rank(root) and node/pointer colors,
    remaining ranks may be computed on way down.

9
rank(root) tree height
  • Height lt 2 rank(root).

10
rank(root) tree height
  • No external nodes at levels 1, 2, , rank(root).

11
rank(root) tree height
  • No external nodes at levels 1, 2, , rank(root).
  • So, nodes gt S1 lt i lt rank(root) 2i -1 2
    rank(root) 1.
  • So, rank(root) lt log2(n1).
  • So, height(root) lt 2log2(n1).

12
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.

13
Join Binary Search Trees
  • O(1) time.

14
Join Red-black Trees
  • When rank(S) rank(B), use binary search tree
    method.
  • rank(root) rank(S) 1 rank(B) 1.

15
rank(S) gt rank(B)
  • Follow right child pointers from root of S to
    first node x whose rank equals rank(B).

16
rank(S) gt rank(B)
  • If there are now 2 consecutive red
    pointers/nodes, perform bottom-up rebalancing
    beginning at m.
  • O(rank(S) rank(B)).

17
rank(S) lt rank(B)
  • Follow left child pointers from root of B to
    first node x whose rank equals rank(S).
  • Similar to case when rank(S) gt rank(B).

18
Split(k)
  • Inverse of join.
  • Obtain
  • S dictionary of pairs with key lt k.
  • B dictionary of pairs with key gt k.
  • m pair with key k (if present).

19
Split A Binary Search Tree
20
Split A Binary Search Tree
B
A
C
b
a
D
c
d
E
m
e
f
g
21
Split A Binary Search Tree
A
B
C
b
a
D
c
d
E
m
e
f
g
22
Split A Binary Search Tree
A
B
b
a
C
D
c
d
E
m
e
f
g
23
Split A Binary Search Tree
A
B
b
a
D
C
d
c
E
m
e
f
g
24
Split A Binary Search Tree
A
B
b
a
D
C
d
c
E
e
m
f
g
25
Split A Binary Search Tree
A
B
b
a
D
C
d
g
c
E
e
f
m
26
Split A Red-Black Tree
  • Previous strategy does not split a red-black tree
    into two red-black trees.
  • Must do a search for m followed by a traceback to
    the root.
  • During the traceback use the join operation to
    construct S and B.

27
Split A Red-Black Tree
28
Split A Red-Black Tree
S f
B g
29
Split A Red-Black Tree
S f
B g
S join(e, E, S)
30
Split A Red-Black Tree
S f
B g
A
B
a
S join(e, E, S)
C
b
B join(B, D, d)
D
c
d
31
Split A Red-Black Tree
S f
B g
A
B
a
S join(e, E, S)
C
b
B join(B, D, d)
S join(c, C, S)
c
32
Split A Red-Black Tree
S f
B g
A
B
a
S join(e, E, S)
b
B join(B, D, d)
S join(c, C, S)
B join(B, B, b)
33
Split A Red-Black Tree
S f
B g
A
a
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)
34
Complexity Of Split
  • O(log n)
Write a Comment
User Comments (0)
About PowerShow.com