Data Structures

Post on 24-Feb-2016

22 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Data Structures. Lecture 4 Red -Black Trees. Haim Kaplan and Uri Zwick November 2013. Red -Black trees [Bayer 1972] [ Guibas-Sedgewick 1978]. External leaves. Red -Black trees. Binary search tree with external leaves Each node is colored red or black - PowerPoint PPT Presentation

Transcript

Data Structures

Haim Kaplan and Uri ZwickNovember 2013

Lecture 4Red-Black Trees

2

Red-Black trees[Bayer 1972] [Guibas-Sedgewick 1978]

External leaves

3

Binary search tree with external leaves

Each node is colored red or blackEach root to leaf path contains the

same number of black nodesThe parent of a red node must be blackThe root and external leaves are black

Red-Black trees

4

Red-Black trees

Paths from each node to the leaveshave the same length

5

Red-Black trees

What is the maximal height of a Red-Black tree containing n nodes?

6

Height and Black height

Black height - Number of black nodes on paths from the node to the external leaves (excluding the node)

Height 2 black height

1

2

3

4

1

1

0

0 0

0

1

2

1 1

1

2

0

0 0

0

7

Height of Red-Black trees

Lemma: The subtree of a node of black height kcontains at least 2k1 nodes

Corollary: The black height of a Red-Black treecontaining n nodes is at most log2(n+1)

Corollary: The height of a Red-Black treecontaining n nodes is at most 2 log2(n+1)

8

Height of Red-Black trees

Lemma: The subtree of a node of black height kcontains at least 2k1 nodes

k ork−1

k

k ork−1

1

0

9

Fold red nodes into their parents

Red-Black trees 2-4 trees

10

Fold red nodes into their parents

Red-Black trees 2-4 trees

11

Height of 2-4 tree = black height of Red-Black tree

Red-Black trees 2-4 trees

Red-Black trees have logarithmic height

13

Insert

14

Insert (cont.)

15

Insert (cont.)

16

Insert (cont.)

17

Rotations

y

x

B

C

A

x

y

B

A

C

Right rotate

Left rotate

19

Insert (cont.)

xy

z

yz x

====>

20

Insert (cont.)

21

Insert (cont.)

22

Insert (cont.)

23

Insert (cont.)

24

C

A D

Bz

w

Insert: Case 1a

C

A D

Bz

new z

Recolor z’s parent, uncle and grandparent

Red rule violation resolved or moved up the treeBlack heights do not change

z’s uncle w is red, z is a right child

25

C

A D

Bz

w

C

A D

Bz

y

new z

Symmetric

Insert: Case 1bz’s uncle w is red, z is a left child

26

C

A

Bz

C

A

B

z

Use a left rotation to convert to case 3Black heights do not change

Note: the roots of ,,, are black

Insert: Case 2z’s uncle w is black, z is a right child

27

C

A

B

z

Use a right rotation to resolve

Note: the roots of ,,, are black

B

CA

Insert: Case 3z’s uncle w is black, z is a left child

28

In all cases above, z’s parent is a left child

The other cases (z’s parent is a right child)are mirror images of the cases shown

All cases covered?

Case 1: z’s uncle is redCase 2: z’s uncle is black and z is a right childCase 3: z’s uncle is black and z is a left child

29

Case 1

Fixing a Red-Black Tree after insertion

Case 2

Case 3

finish

Can only loop in Case 1

30

Insert - analysisFind insertion point – O(log n) time

Insert – O(1) timeFix the tree – O(log n) time

Fixing the tree:At most 2 rotations

O(1) amortized time

Total time spend on fixing the tree while inserting n elements is O(n)

31

Amortized analysis of Fixing the tree

Inserted nodes are colored redCase 1 is the only non-terminal case

= #red nodes

2 red + 1 black 1 red + 2 blackNumber of red nodes decreases by 1

In Case 1:

Total fix-up time in n insertions = O(n)

In n insertions Case 1 can be executed at most n times!

32

Red-Black trees - Implementation

right

key

left

x parent info

color

33

Red-Black trees - Implementation

right

key

left

x parent info

color

Add a dummy root (sentinel)

The real root is the left child of the dummy root

T

The dummy root is black and has key

34

Red-Black trees - ImplementationReplace all external leaves

by another sentinel, called NULL

NULL

NULL is a black Tree-Node

Writing to NULL.parent is harmless

T

35

Transplant – Replace the subtree of x by the subtree of yReplace – Replace x by y

Basic operations

x may be the (real) root

36

Left Rotation

All special cases handled automatically!,, may be NULL. x may be the root.

x

y

x

y

37

38

Case 1

Case 2

Case 3

p parent

See also CLRSChapter 13

39

Deletions from Red-Black trees

Similar in nature to insertions

Slightly more complicated

If the node to be deleted has two children,we again delete its successor from the tree and use it to replace the node to be deleted

Let’s see some examples…

40

Delete

41

Delete

42

Delete

43

Delete

44

Delete (cont.)

45

Delete (cont.)

46

Delete (cont.)

47

Delete (cont.)

48

Delete (cont.)

49

Delete (cont.)

50

Deleting a node from a Red-Black tree

Remove the node from the tree

If it was red or had a red child we are done

Otherwise we get a node that requires “extra blackness”

Apply a transformation that either moves the “extra blackness” up the tree, or gets rid of it

An “extra blackness” at the root may be removed

52

Delete: Case 1

B

A Cx

w

C

B

x

A

x’s sibling w is red

“Extra-blackness” went downThis can only happen once

53

Delete: Case 2x’s sibling w is black,

and both children of w are black

B

A Dx

C E

w

B

A D

new x

C E

w

If B is red, it is made black and we are done

54

Delete: Case 3x’s sibling w is black,

w’s left child is red, and w’s right child is black

B

A Dx

C E

w

B

A Cx

E

D

new w

55

Delete: Case 4x’s sibling w is black,

and w’s right child is red

B

A Dx

C E

w

D

EB

CA

“Extra-blackness” disappeared!

56

Finish

Deletions from Red-Black TreeCase 1

All casesabove

Case 2b

Case 3

Case 4

Case 2r

Can only loop in Case 2b

57

Delete - analysisFind the successor (if needed) – O(log n) time

Delete – O(1) timeFix the tree – O(log n) time

Fixing the tree:At most 3 rotations

O(1) amortized time

Total time spend on fixing the tree while performing a sequence of n insert and delete operations is O(n)

58

Delete + Insert – Non-terminal cases

Delete: Case 2

Insert: Case 1a

= #( ) + 2 #( )

59

Joining two Red-Black trees

Suppose that all keys in T1 are less than x.keyand that all keys in T2 are greater than x.key

T1T2

x

Simply joining them would create a very unbalanced tree

Join(T1,x,T2)

Black rule may be violated

60

Joining two Red-Black trees

Find a black node z on the left spine of T2 with the same black height as the black height of the root of T1

T1

T2

x

Color x red

y

z

Fix red rule as in insertO(log n) time

Join(T1,x,T2)

O(bh(T2)−bh(T1)+1) time, if bh’s maintained explicitly

Assumebh(T1) ≤ bh(T2)

61

a

b

c

d

e

f

x

A

B

C

D

E

F

G

H

e

fA

B

C D

c

a

b

dE

F

GH

x

Splitting

62

Balanced and Efficient Splitting

y

x

O(log n) time!

Suppose we join T1 ,T2 ,…,Tk

where bh(T1) bh(T2) … bh(Tk)

Time is (exact argument is a little more complicated)

63

Additional dictionary operations

Select(D,i) – Return the i-th largest item in D(indices start from 0)

Rank(D,x) – Return the rank of x in D,(i.e., the number of items x is larger than)

Can we still use Red-Black trees?

Keep sub-tree sizes!

64

10

3

1 1 3 2

1

6

x.size = x.left.size + x.right.size + 1

1 1

65

Selection

Note: 0 i < n

66

a

b

c

d

e

f

x

A

B

C

D

E

F

G

H

Rank

67

Rank

Assume NULL.size=0

68

Easy to maintain sizesy

x

B

C

A

x

y

B

A

C

Right rotate

a b

c

cb

a

y.size = b.size + c.size + 1x.size = a.size + y.size + 1

69

Finger Search trees

Maintain a pointer to the minimum element

T

Select(T,k) in O(log k) time

70

Lists as Trees

a

e

b

d f

c

b e

d

c fa0 2

53

2

3

1 4

5 4

0

1

[ a b c d e ]

71

Lists as TreesMaintain the items in a tree:i-th item in node of rank i

List-Node Tree-Node

Tree-Nodes have no explicit key(Implicitly maintained ranks play the role of keys)

Retrieve(L,i) Select(L,i)

Select, Insert-Fixup and Delete-Fixupdo not use keys

72

Lists as Trees: InsertTo insert a node z in the i-th position, where i<n:

Find the current node of rank i.If it has no left child, make z its left child.

Otherwise, find its predecessor and make z its right child.

To insert a node z in the last position (i=n):Find the last node andmake z its right child

Fix the tree (Insert-Fixup)

73

Lists as Trees: Delete

Delete a node z in the same waya node is removed from a search tree

74

Implementation of lists

Circular arrays

DoublyLinked

lists

Red-Black Trees

Insert/Delete-FirstInsert/Delete-Last O(1) O(1) O(log n)

Insert/Delete(i) O(i+1) O(i+1) O(log n)

Retrieve(i) O(1) O(i+1) O(log n)

Concat O(n+1) O(1) O(log n)

75

AVL trees G.M. Adelson-Velskii and E.M. Landis (1962)

k−1 k−2

kThe depth of two siblings differs by

at most 1

Need only one extra bit per node

76

AVL trees G.M. Adelson-Velskii and E.M. Landis (1962)

k−1 k−2

k

Sk – minimal number of nodes in an AVL tree of depth k

Balance maintained through rotations

77

Splay Trees (Self-adjusting trees) Sleator and Tarjan (1983)

Do not maintain any balance!

When a node is accessed, splay it to the root

A node is splayed using a sequence of zig-zig and zig-zag steps

Amortized cost of each operation is O(log n)

Total cost of n operation is O(n log n)

Many other amazing properties

78

Zig-Zig

z

y

x

A

D

C

B

x

y

z

D

A

B

C

Rotate y-z left, then rotate x-y left

79

Zig-Zag

z

A

y

x

B

D

C

Rotate x-y right, then rotate x-z left

x

y

DC

z

BA

80

Splaying (example)

i

h

H

g

f

e

d

c

b

a

I

J

G

A

B

C

D

E F

i

h

H

g

f

e

d

a

b

c

I

J

G

A

B

F

E

DC

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

81

Splaying (example cont)

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

i

h

H

a

f g

d e

b

c

I

J

G

A

BF

E

DC

f

d

b

c

A

B

E

DC

a

h

i

I JH

g

e

GF

82

Splay Trees (Self-adjusting trees) Sleator and Tarjan (1983)

Amortized cost of each operation is O(log n)

Total cost of n operation is O(n log n)

Many other amazing properties

Some intriguing open problems

http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.htmlPlay with them yourself:

top related