red -black tree

66
red-black tree Lai Ah Fur

Upload: cody-byers

Post on 02-Jan-2016

15 views

Category:

Documents


1 download

DESCRIPTION

red -black tree. Lai Ah Fur. Background: AVL trees may require many restructure operations (rotations) to be performed after an element removal, (2,4) trees may require many fusing or split operations to be performed after either an insertion or removal. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: red -black tree

red-black tree

Lai Ah Fur

Page 2: red -black tree

•Background:

•AVL trees may require many restructure operations (rotations) to be performed after an element removal,

•(2,4) trees may require many fusing or split operations to be performed after either an insertion or removal.

•The red-black tree, does not have these drawbacks. It requires that only O(1) structural changes be made after an update in order to stay balanced.

Page 3: red -black tree

Property of A red-black tree• A red-black tree is a binary search tree with

nodes colored red and black in a way that satisfies the following properties:– Root Property: The root is black.– External Property: Every external node is black.– Internal Property: The children of a red node are

black.– Depth property: All the external nodes have the

same black depth, which is defined as the number of black ancestors minus one.

– The height of a red-black tree storing n items is O(log n).

Page 4: red -black tree

3

4

6 8

7 11

17

15

13

14

10

5

12

Example of A red-black tree

Page 5: red -black tree

x

y

β

α

γ

x

y

βα

y

x

βα

γ

LEFT-ROTATE(T,x)

RIGHT-ROTATE(T,y)

LEFT-ROTATE(T,x)1 y←right[x] //Set y.

2 right[x] ← left[y] //Turn y’s left subtree into x’s right subtree.

3 if left[x] ≠ nil[T]

4 then p[left[y] ] ←x // β’s father

5 p[y ] ← p[x ] //Link x’s parent to y.

6 if p[x ] = nil[T]

7 then root [T] ←y

8 else if x=left[p[x]]

9 then left[p[x]] ←y

10 else right[p[x]] ←y

11 left[y] ←x //Put x on y’s left

12 p[x ] ←y

Page 6: red -black tree

RB-INSERT(T,z) //insert z into T

1 y ← nil[T]

2 x ← root[T]

3 while x≠ nil[T]

4 do y←x

5 if key[z]< key[x]

6 then x ← left[x]

7 else x ← right [x]

8 p[z] ←y

9 if y=nil[T]

10 then root[T] ← z

11 else if key[z]<key[y]

12 then left[y] ← z

13 else right[y] ← z

14 left[z] ←nil[T]

15 right[z] ← nil[T]

16 color[z] ← RED

17 RB-INSERT-FIXUT(T,z)

Page 7: red -black tree

RB-INSERT-FIXUP(T,z)

1 while color [p[z]] =RED

2 do if p[z]=left [p[p[z]]]

3 then y ← right[p[p[z]]]

4 if color [y] =RED

5 then color p[z] ←BLACK //case 1

6 color [y ] ←BLACK //case 1

7 color [p[p[z]]] ← RED //case 1

8 z ←[p[z]] //case 1

9 else if z = right [p[z]]

10 then z← p[z] //case 2

11 LEFT-ROTATE(T,z) //case 2

12 color [p[z]] ←BLACK //case 3

13 color [p[p[z]]] ← RED // //case 3

14 RIGHT-ROTATE(T, p[p[z]]) //case 3

15 else (same as then clause

with “ right”and “left” exchanged

16 color [root [T]] ←BLACK

Page 8: red -black tree

z

1

11

14

157

5

4

2

8

z

y

Case 1 : z’s uncle y is red

1

11

14

15

5

4

2

8

y

Case 2

(a)

(b)

7

Page 9: red -black tree

Case 2

1

11

14

155

4

28

z y

Case 3

1

7

14

15

5

4

2z

7

11

8

(c)

(d)

Page 10: red -black tree

Case 1-1: z’s uncle y is red

α

α

C

D

B z

yA

β γ

δ ε

C

D

B z

A

β γ

δ ε

new z

α

C

DBz

y

A

β

γ δ ε

C

DBz

y

A

βα

γ δ ε

new z

做法 : 改變 parent, uncle, grandparent 的 color

Problem:連續雙 red α β γ δ ε :Black height 不變

Page 11: red -black tree

Case 1-2: z’s uncle y is red

α

C

D

B z

yA

β

γδ

ε ε

α

C

DB

z

y

Aβγ

δ ε

new z

做法 : 改變 parent, uncle, grandparent 的 color

Problem:連續雙 redα β γ δ ε :Black height 不變If c’s parent is red? Continue…

C

D

B z

yA

β

γδ

new z

α

C

DB

z

y

Aβγ

δ ε

Page 12: red -black tree

Case 2-1: z’s uncle y is black and z is a right child

Case 3-1: z’s uncle y is black and z is a left child

α

C

B z

yA

β γ

δ

case 3-1case 2-1

C

B

zy

A

βα

γ

δ

B

Az

βα γ δ

C

Left rotation

Change color: parent & grandparight rotation

α

C

B z

yA

β γ

δ

Case 3-2Case 2-2

B

C z

βα γ δ

Cy

B

zA zβ

γ δ

Right rotate

Page 13: red -black tree

4

(a)

(b)

(c) Case 3-2

(d)

4

7

4

7

12

4

7

12 4

7

12

15

(e) Case 1-2

+root must be black

insertion

Page 14: red -black tree

(g)

4

7

12

153

(f)

4

7

12

15

Page 15: red -black tree

5

(h) Insert 5

3 15

4 12

7

Page 16: red -black tree

5

(i)Insert 14

Case 2-2

3 15

4 12

7

14

Page 17: red -black tree

5

(j)

3 1512

4 14

7

Page 18: red -black tree

5

(k)

3 1512

4 14

7

18

Page 19: red -black tree

5

(l)

3 1512

4 14

7

18

Page 20: red -black tree

5

(m) Case 2-2

3 1512

4 14

7

18

16

Page 21: red -black tree

5

(n)

3 1612

4 14

7

1815

Page 22: red -black tree

5

(o) Insert 17

Case 1-2

3 1612

4 14

7

1815

17

Page 23: red -black tree

5

(p) Case 3-2

3 1612

4 14

7

1815

17

Page 24: red -black tree

12

(q)

1815

7 16

14

173

4

5

Page 25: red -black tree

Insertion complexity

• The insertion of a key-element item in a red-black tree storing n items can be done in O(log n) time and at most O(log n) recolorings and one trinode restructuring (a restructure operation).

Page 26: red -black tree

RB-DELETE(T,z)

1 if left[z]=nil[z] or right[z]=nil[T]

2 then y ←z

3 else z ←TREE-SUCCESSOR(z)

4 if left[y] ≠ nil[T]

5 then x← left[y]

6 else x ← right[y]

7 p[x] ← p [y]

8 if p[y]= nil[T]

9 then root [T] ← x

10 else if y=left [p[z]]

11 then left [p[z]] ← x

12 else right [p[z]] ← x

13 if y ≠z

14 then key [z] ← key [y]

15 copy y’s satellite data into z

16 if color [y] = BLACK

17 then RB-DELETE-FIXUP(T,x)

18 return y

Page 27: red -black tree

RB-DELETE FIXUP(T,x) //y 為真正被 deleted 之 node, x 是 y 的 right or left child1 While x ≠ root[T] and color[x] =BLACK

2 do if x =left [p[x]]

3 then w ← right [p[x]]

4 if color[w] = RED

5 then color[w] ← BLACK //Case 1

6 color [p[x]] ← RED //Case 1

7 LEFT-ROTATE(T,p[x]) //Case 1

8 w ← right [p[x]] //Case 1

9 if color [left[w]] = BLACK and color [right[w]]= BLACK

10 then color[w] ← RED //Case 2

11 x ← p[x] //Case 2

12 else if color [left[w]] = BLACK

13 then color [left[w]] ← BLACK //Case 3

14 color[w] ← RED //Case 3

15 RIGHT-ROTATE(T,w) //Case 3

16 w ←right [p[x]] //Case 3

17 color[w] ← color [p[x]] //Case 4

18 color [p[x]] ← BLACK //Case 4

19 color [right[w]] ← BLACK //Case 4

20 LEFT-ROTATE(T,p[x]) //Case 4

21 x ← root[T] //Case 4

22 else (same as then clause with”right”and”left”exchanged)

23 color[x] ← BLACK // 若 x is red, 改為 black, black height 即能維持

Page 28: red -black tree

B

Ax

βα

Case 1

δγ ζε

D

C E

w

D

x

E wB

A C

βα δγζεnew w

(a)

B

Ax

βα

Case 2

δγ ζε

D

C E

w

new x(b) c B

A

βα

δγ ζε

D

C E

c

:Red or black

(C)

(d)

y 為真正被 deleted 之 node, x 是 y 的 right or left child

restructure

Reduce 1 black height

recolor

Page 29: red -black tree

B

Ax

βα

Case 3

δγ ζε

D

C E

w

(c)

B

Ax

βα

Case 4

δγ ζε

D

C E

w

(d) c

cnew w

B

A

βαδ

γ

ζε

C

D

E

c

x

c’

D

EB

A C

βα δγζε

c’

c

new x=root[T]

Page 30: red -black tree

Case 1: x’s sibling w is red

Case 2: x’s sibling w is black, and both of w’s children are black

Case 3: x’s sibling w is black, w’s left child is red, and w’s right child is black

Case 4: x’s sibling w is black, and w’s right child is red

Page 31: red -black tree

•If v is a 2-node, then keep the (black) children of v as is.

•If v is a 3-node, then create a new red node w, give v’s first two (black) children to w, and make w and v;s third child be the two children of v.

•If v is a 4-node, then create two new red nodes w and z, give v’s first two (black) children to w, give v’s last two (black) children to z, and make w and z be the two children of v.

Page 32: red -black tree

12

(a) initial

1815

7 16

14

173

4

5

deletion

Page 33: red -black tree

12

(b) Delete 3

1815

7 16

14

17

4

5

Page 34: red -black tree

(c) Delete 12

1815

7 16

14

17

4

5

restructure

Page 35: red -black tree

7

(d)

1815

5 16

14

17

4 Delete 17

Page 36: red -black tree

7

(e)

1815

5 16

14

4 Delete 18

Page 37: red -black tree

7

(f)

15

5 16

14

4

Page 38: red -black tree

7

(g) After recoloring

15

5 16

14

4Delete 15

Page 39: red -black tree

7

(h)

5 16

14

4

Delete 16

Page 40: red -black tree

7

(i)

5

14

47

(j)

5

4 14

7

(k)

5

4 14

adjustment

recoloring

Page 41: red -black tree

15

13 14

6 7 8

15

13

14

(a)

(b)

(c)

14

13

or

7

86

Page 42: red -black tree

Insertion:

Case 1: The Sibling w of v is Black.

30

20

10

10

20

30

u

v

z w

u

v

zw

30

20

10

u

vz w

10

20

30

u

vzw

(a)

black

Double red

Page 43: red -black tree

20b

a c

(b) After a trinode restructuring

3010

•Take node z, its parent v, and grandparent u, and temporarily relabel them as a,b,and c, in left-to-right order, so that a, b, c will be visited in this order by an inorder tree traversal.•Replace the grandparent u with the node labeled b, and nodes a and c the children of b, keeping inorder relationships unchanged.

Page 44: red -black tree

Case 2: The Sibling w of v is Red.

10 20 30 40

… 30 …

10 20 40

30u

v w

(a)

4020

10z

30u

v w

(b)

4020

10z

… …

recoloring

Page 45: red -black tree

… 30 …

10 20

30x

y r4020

10z

40

… …

(a)

Deletion: case 1: the sibling y of r is black and Has a red child z.

Page 46: red -black tree

… 30 …

10 20

40

… …

(b)

30

20

10

x

y

zr

40

Page 47: red -black tree

… 20 …

10 30

40

… …

(c)After restructure

20b

a c3010

40

r

(a)(b)

Page 48: red -black tree

10 30 …

20

30x

yr

4020

40

(a)

10

Case 2: the sibling y of r is black and both children of y are black.

Page 49: red -black tree

20 30

30x

yr

4020

40

(b)

10 ……

10

After recoloring

Page 50: red -black tree

30

20

40

30x

yr

4020

(a)

Page 51: red -black tree

20 30

30x

yr

4020

40

(b) After recoloring

Page 52: red -black tree

20 30

(a)

… 10 …

40

… …

30x

y r4020

10z

Case 3: the sibling y of r is red.

Page 53: red -black tree

20 30

(b) After adjustment

… 10 …

40

… …

20y

z x3010

40

r

Page 54: red -black tree

Deletion complexity

• The algorithm for removing an item from a red-black tree with n items takes O(log n) time and performs O(log n) recolorings and at most one adjustment plus one additional trinode restructuring. Thus it perform at most two restructure operations.

Page 55: red -black tree

2

5

(1)Insert 2 (2)Insert 1

2

1

(3)Insert 3

2

1 3

Change Color

2

1 3

(4)Insert 7

7

2

1 3

(5)Insert 5

7

2

1 3 RL

7

2

1 5

3

(6)Insert 8

8

7

2

1 5

3

Page 56: red -black tree

(7)Insert 9

8

7

2

1 5

3

9

RR5

2 8

9731

(8)Insert 11

5

2 8

9731

11

(9)Insert 10

5

2 8

9731

11

10

RL

5

2 8

10731

119

Page 57: red -black tree

(1)Delete 7

5

2 8

10731

119

Rotate

5

2 10

11831

97

Del 7

5

2 10

11831

9

(2)Delete 9

5

2 10

11831

9

Del 9

5

2 10

11831

Page 58: red -black tree

最主要的三個判斷判斷一:爸爸是爺爺的左 or 右兒子判斷二: uncle 是不是紅的判斷三:我是爸爸的左 or 右兒子

所以會有八種可能 請往下看( 注意:例圖不是由樹根畫起

而是最低的三層節點。 )

Page 59: red -black tree

爸爸是爺爺的左兒子系列Case 1

Uncle 是紅的

Case 2

在爸爸右邊

Case 3

在爸爸左邊

Page 60: red -black tree

爸爸是爺爺的右兒子系列Case 1

Uncle 是紅的

Case 2

在爸爸右邊

Case 3

在爸爸左邊

Page 61: red -black tree

8

8 加入 5

5

8 加入 7

7

8

5

8

7

5

1 變色

Case 3

2 右轉 7

5

5

8

7

Case 2

左轉

加入 8

Page 62: red -black tree

8

7

5

Case 1

8

7

5

4 4

5 8

1 變色

2 根黑色7

4

58

7

6

4

5 8

7

6

2

Case 1 8

7

2

4 6

5

變色

加入 4

加入 2 加入 6

Page 63: red -black tree

8

7

2

4 6

5 8

7

2

4 6

5

3

加入 3

Page 64: red -black tree

1

8

7

2

4 6

5

3

Case 1

1

8

7

2

4 6

5

3

變色

Case 3

1

8

7

2

4 6

5

3

1 變色2 右轉

1

82

4

6

5

3

7

加入 1

Page 65: red -black tree

謝謝您的流覽希望有所幫助

有問題請洽[email protected]

Page 66: red -black tree

exercise

• Complete the following algorithm: RB-DELETE FIXUP(T,x) & RB-INSERT-FIXUP(T,z)

• 實作習題四 : 以 java 實作 red-black tree (similar to 習題 1)