43
1 Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

Embed Size (px)

DESCRIPTION

Red Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13. We assume items at the leaves Don’t show keys throughout the presentation, they are basically handled as before. Red Black trees - definition. Binary search tree each node is colored red or black such that. 1) Leaves are black - PowerPoint PPT Presentation

Citation preview

Page 1: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

1

Red Black Trees(Guibas Sedgewick 78)

CLRS: Chapter 13

Page 2: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

2

• We assume items at the leaves

• Don’t show keys throughout the presentation, they are basically handled as before

Page 3: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

5

Red Black trees - definition

Binary search tree

each node is colored red or black such that

1) Leaves are black

2) All paths from the root to an external node contain the same number of black nodes (black rule)

3) Any red node, if has a parent, has a black parent (red rule)

Page 4: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

6

Red Black trees - example

Page 5: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

7

Red Black Trees -properties

The depth is O(log n) -- prove as an excercise

==> find takes O(log n) time

How do we do the other operations and keep the trees Red-Black ?

Page 6: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

8

Insert

Page 7: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

9

Insert (cont)

Page 8: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

10

Insert (cont)

Page 9: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

11

Insert (cont)

Page 10: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

12

Insert (cont)

Page 11: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

13

Use rotations

x

y

B

C

y

Ax

B C

<===>

A

x

y

z

y

z x

===>

Page 12: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

14

Insert (cont)

xy

z

y

z x

====>

Page 13: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

15

Insert (cont)

Page 14: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

16

Insert (cont)

Page 15: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

17

Insert (cont)

Page 16: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

18

Insert (cont)

Page 17: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

19

Insert (cont)

Page 18: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

20

Insert (cont)

Page 19: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

21

Insert -- definition

Convert a leaf to a red internal node with two leaves.

This may create violation to property 2. To restore it we walk up towards the root applying one of the following cases (each case has a symmetric version)

Page 20: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

22

Insert -- non terminal casesz

y

x

A B

C D E

z

y

x

A B

C D E

z

y

x

B C

A D E

===>

(1)

(2)

y

x

B C

A D E

===>

Page 21: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

23

Insert -- terminal cases

z

y

x

A B

C D E

y

zx

A B C

z

y

x

B C

A D E

===>

(3)

(4)

x

B

x

B

===>

w

D E

w

(5)===>

w zy

A B C

D E

w

x

z

y

x

A B

C D E

w

Page 22: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

24

Insert - analysis

O(log n) time worst case, since the height is O(log n)

Suppose you start with an empty tree and do m insertions such that the point of insertion is given to you each time, how much time does it take ?

Obviously O(mlog n),

but maybe we can prove it cannot be that bad ?

Page 23: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

25

Insert - analysis

Each time we do a color-flip-step the number of red nodes decreases by one.(tree) = #red nodes

Actual(insert) = O(1) + #color-flips-steps

(insert) = O(1) - #color-flips-steps

==> amortized(insert) = O(1)

and the sequence actually takes O(m) time.

Page 24: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

26

Delete -- example

Page 25: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

27

Delete -- example (cont)

-

Page 26: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

28

Delete -- example (cont)

Page 27: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

29

Delete -- example2 (cont)

Page 28: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

30

Delete -- example2 (cont)

-

Page 29: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

31

Delete -- example2 (cont)

-

Page 30: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

32

Delete -- example2 (cont)

Page 31: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

33

Delete -- definition

Replace the parent of the external node containing the item with the sibling subtree of the deleted item

If the parent of the deleted item is black then we create a short node

To restore the black constraint we go bottom up applying one of the following cases.

Page 32: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

34

Delete -- fixing a short node

--

====>(1)

- ====>(2)

Page 33: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

35

Delete -- fixing a short node (cont)

- ====>(3) x

y

z

w x

y w

z

- ====>(4) x

y

z

wA

x

yv

v

BA

-x

y

z

w

v

B

A-

wB

Page 34: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

36

Delete -- fixing a short node (cont)

- ====>(5) x

y

z

w

yz

v

w

x v-

And apply one of the previous 3 cases.

Page 35: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

37

Delete + insert -- analysis

O(log n) time, since the height is O(log n)

Suppose you start with an empty tree and do m insertions and deletions such that the point of insertion is given to you each time, how much time does it take ?

Obviously O(mlog n),

but maybe we can prove it cannot be that bad ?

Page 36: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

38

Delete + insert - analysis

The previous potential won’t do the trick

(tree) = #red nodes

Here are the transformation that we want to release potential

Page 37: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

39

Delete + insert -- analysis

--

====>

===>

===>

Page 38: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

40

Delete + insert -- analysis

(tree) =

#( ) + 2 #( )

==> amortized(delete) = O(1)

amortized(insert) = O(1)

sequence of m delete and inserts, starting from an empty tree takes O(m) time

Page 39: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

41

Concatenation

+ =

Define the rank of a node v as the number of black nodes from v to a leaf .

Assume T1 has a black root.

Look on the left spine of T2 for a node x of the same rank as the root of T1

T1

T2

Page 40: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

42

Concatenation (cont)

+ =

T1

T2

x

Make y a child of p(x)

Continue as for insert

y

Allocate y make the root of T1 and x children of y.

Color y red

Page 41: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

43

Concatenation (analysis)

O(|r1-r2| + 1) = O(log n) worst case.

If the right point on the spine of the taller tree is given then its O(1) amortized

Page 42: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

44

Split

x

Concatenate all trees to the left of the path from the root to x to the left tree T1 (including x).

Concatenate all trees to the right of the path from the root to x to the right tree T2

Page 43: Red  Black Trees (Guibas Sedgewick 78) CLRS: Chapter 13

45

Split -- analysis.

Can prove that split takes O(log n) time.