Top Banner
1 Red Black Trees (Guibas Sedgewick 78)
48

1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

1

Red Black Trees(Guibas Sedgewick 78)

Page 2: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

2

Goal

Keep sorted lists subject to the following operations:

find(x,L)

insert(x,L)

delete(x,L)

catenate(L1,L2) : Assumes that all items in L2 are greater than all items in L1.

split(x,L) : returns two lists one with all item less than or equal to x and the other with all items greater than x.

Page 3: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

3

Binary search trees

Binary search tree:

•All internal nodes have degree = 2

•Items are at the leaves.

•All items in left subtree are smaller than all items in right subtree (symmetric order)

12

10985

4

Page 4: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

4

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 5: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

5

Red Black trees - example

Page 6: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

6

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 7: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

7

Insert

Page 8: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

8

Insert (cont)

Page 9: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

9

Insert (cont)

Page 10: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

10

Insert (cont)

Page 11: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

11

Insert (cont)

Page 12: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

12

Use rotations

x

y

B

C

y

Ax

B C

<===>

A

x

y

z

y

z x

===>

Page 13: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

13

Insert (cont)

xy

z

y

z x

====>

Page 14: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

14

Insert (cont)

Page 15: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

15

Insert (cont)

Page 16: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

16

Insert (cont)

Page 17: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

17

Insert (cont)

Page 18: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

18

Insert (cont)

Page 19: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

19

Insert (cont)

Page 20: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

20

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 21: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

21

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 22: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

22

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 23: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

23

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 24: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

24

Amortized analysis

We are interested in the worst case running time of a sequence of operations.

Example: binary counter

single operation -- increment

000000000100010000110010000101

Page 25: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

25

Amortized analysis (Cont.)

On the worst case increment takes O(k).

k = #digits

What is the complexity of a sequence of increments (on the worst case) ?

Define a potential of the counter:

Amortized(increment) = actual(increment) +

(c) = ?

Page 26: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

26

Amortized analysis (Cont.)

Amortized(increment1) = actual(increment1) + 1- 0

Amortized(increment2) = actual(increment2) + 2- 1

Amortized(incrementn) = actual(incrementn) + n- (n-1)

+

iAmortized(incrementi) = iactual(incrementi) + n- 0

iAmortized(incrementi) iactual(incrementi)

if n- 0 0

Page 27: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

27

Amortized analysis (Cont.)

Define a potential of the counter:

(c) = #(ones)

Amortized(increment) = actual(increment) +

Amortized(increment) = 1+ #(1 => 0) + 1 - #(1 => 0) = O(1)

==> Sequence of n increments takes O(n) time

Page 28: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

28

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 29: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

29

Delete -- example

Page 30: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

30

Delete -- example (cont)

-

Page 31: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

31

Delete -- example (cont)

Page 32: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

32

Delete -- example2 (cont)

Page 33: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

33

Delete -- example2 (cont)

-

Page 34: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

34

Delete -- example2 (cont)

-

Page 35: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

35

Delete -- example2 (cont)

Page 36: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

36

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 37: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

37

Delete -- fixing a short node

--

====>(1)

- ====>(2)

Page 38: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

38

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 39: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

39

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 40: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

40

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 41: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

41

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 42: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

42

Delete + insert -- analysis

--

====>

===>

===>

Page 43: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

43

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 44: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

44

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 45: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

45

Concatenation (cont)

+ =

T1

T2

x

y

Make y a child of p(x)

Continue as for insert

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

Color y red

Page 46: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

46

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 47: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

47

Split

x

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

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

Page 48: 1 Red Black Trees (Guibas Sedgewick 78). 2 Goal Keep sorted lists subject to the following operations: find(x,L) insert(x,L) delete(x,L) catenate(L1,L2)

48

Split -- analysis.

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