Red Black Tree Smt Genap 2011-2012. Outline Red-Black Trees ◦ Motivation ◦ Definition ◦...

31
Red Red Black Black Tree Tree Smt Genap 2011-2012

Transcript of Red Black Tree Smt Genap 2011-2012. Outline Red-Black Trees ◦ Motivation ◦ Definition ◦...

RedRed BlackBlack Tree Tree

Smt Genap 2011-2012

OutlineOutlineRed-Black Trees

◦Motivation◦Definition◦Operation

Smt Genap 2011-2012

MotivationMotivationBinary Search Trees should be balanced.AVL Trees need 2 passes: top-down

insertion/deletion and bottom-up rebalancingNeed recursive implementation

Red-Black Trees need 1 pass: top-down rebalancing and insertion/deletionCan be implemented iteratively, faster

Red-Black Trees have slightly weaker balance restrictionsLess effort to maintainIn practice, worst case is similar to AVL Trees

Smt Genap 2011-2012

RedRed--BlackBlack Trees: Definition Trees: Definition Rules of Red-Black Trees:1. Every node is colored either red or black2. The root is black3. If a node is red, its children must be black

◦ consecutive red nodes are disallowed4. Every path from a node to a null

reference must contain the same number of black nodes

Convention: null nodes are black

Smt Genap 2011-2012

30

15

5 50

10

70

8560

40 55

80 9065

20

RedRed--BlackBlack Trees TreesThe insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

Smt Genap 2011-2012

RedRed--BlackBlack Trees: Trees: PropertiesProperties

Smt Genap 2011-2012

•Each path must contain the same number of black nodes. (Rule #4)

•Consecutive red nodes are not allowed. (Rule #3)

•The longest path is at most twice the length of the shortest path

RedRed--BlackBlack Trees: Trees: PropertiesProperties

B = total black nodes from root to leaf

N = total all nodes

H = height

Smt Genap 2011-2012

)1log(2)1log(

)1log()1log(2

1

2)1log()1log(

22log2log

212

1212

2

2

2

NHN

NBN

BNNB

BB

N

N

BB

BB

BB

All operations guaranteed logarithmic!

Insertion Insertion A new node must be colored red

◦Why? A new item is always inserted as a leaf in the tree If we color a new item black, then the number of

black nodes from root would be different (violate property #4)

◦ If the parent is black, no problem.◦ If the parent is red, we create two

consecutive red nodes (violate property #3) Thus, we have to do some rotating/recolouring…

Remember convention: null nodes are black

Smt Genap 2011-2012

A B

G

X

SP

C D E A B

GX

S

P

C

D E

Single RotationSingle RotationX: new nodeP: parentS: siblingG: Grandparent

Case after insertion:◦ Consecutive red (P & X)◦ Sibling of parent (S) is black◦ X is “outer node” (left-left or right-

right)

Smt Genap 2011-2012

B C

G

X

SP

A D E A B

GP

S

X

C

D E

Double RotationDouble RotationX: new nodeP: parentS: siblingG: Grandparent

Smt Genap 2011-2012

Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “inner node” (left-right or left)

Single Rotation (bottom-Single Rotation (bottom-up)up)Case after insertion:

◦Consecutive red◦Sibling of parent is red◦Outer node (left-left or right-right)

Smt Genap 2011-2012

A B

G

X

SP

C D E A B

GX

S

P

C

D E

But what if P’s parent is red? We have to keep going up the tree all the way to the root

Top-Down InsertionTop-Down InsertionThe solution: prevent S from ever

being red!Starting from the root (searching

for insertion point)◦Never allow 2 red siblings◦If we see a node X with 2 red

children, do a colour flip.

Smt Genap 2011-2012

X

C2C1

X

C1 C2

Color FlipColor Flip

◦Maintains property #4◦Possible violation of #3: if X’s parent is red!

Do single or double rotation X’s parent’s sibling can never be red!

◦Set the root to black (to maintain property #2)

Smt Genap 2011-2012

X

C2C1

X

C1 C2

A B

G

X

SP

C D E

Color Flip (2)Color Flip (2)

Smt Genap 2011-2012

A B

G

X

SP

C D E

If we do the colour flipping on the way down to the insertion point, we will never reach a condition where P & S are red!

30

15

5

10

70

8560

50

55

80 9065

20

40

Example: Insert 18Example: Insert 18

Smt Genap 2011-2012

18

30

15

5

10

70

8560

50

55

80 9065

20

40

Example: Insert 2Example: Insert 2

Smt Genap 2011-2012

18

2

30

15

2

5

70

8560

50

55

80 9065

20

40

Example: Insert 2Example: Insert 2

Smt Genap 2011-2012

1810

30

15

2

5

70

8560

50

55

80 9065

20

40

Example: Insert 45 Example: Insert 45 (Illustration)(Illustration)

Smt Genap 2011-2012

1810

45

30

15

2

5

70

8560

50

55

80 9065

20

40

Example: Insert 45 (Top-Example: Insert 45 (Top-Down Color Flip)Down Color Flip)

Smt Genap 2011-2012

1810

45

Color-flip!

30

15

2

5

70

8560

50

55

80 9065

20

40

Example: Insert 45 (Top-Example: Insert 45 (Top-Down Color Flip)Down Color Flip)

Smt Genap 2011-2012

1810

45

Color-flip!

30

15

2

5

70

8560

50

55

80 9065

20

40

Example: Insert 45 (Single Example: Insert 45 (Single Rotate)Rotate)

Smt Genap 2011-2012

1810

45

30

15

2

5 70

85

60

50

55

80 90

65

20

40

Example: Insert 45 (Single Example: Insert 45 (Single Rotate)Rotate)

Smt Genap 2011-2012

1810

45

Red-Black Tree Insertion: Red-Black Tree Insertion: ExerciseExerciseThe insertion sequence is 10, 85,

15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

Smt Genap 2011-2012

Red-Black Tree: DeletionRed-Black Tree: DeletionDeletion in BST: only leaf nodes

or nodes with one child are really deleted (Why?)

If the deleted node is red: no problem (all properties maintained).

Smt Genap 2011-2012

Leaf nodes:

Single child nodes:

Top-Down DeletionTop-Down DeletionIf node to be deleted is black

violate property #4Always ensure that the node to

be deleted is red.Top-down traversal from root

(looking for node to be deleted):

Smt Genap 2011-2012

X: visited node

P: parent

S: sibling

B

P

SX

A C D

Idea: make sure that X is red!

Possible casesPossible casesP is red (inductive invariant)X and S are black (result of property

#3)

2 cases:◦1. Both X’s children (A & B) are black◦2. X has at least one red child (A, B, or both)

Smt Genap 2011-2012

B

P

SX

A C D

Case 1: Both X’s children are Case 1: Both X’s children are blackblackDepends on children of S (C & D):

◦Both C & D are black: simply colour-flip:

Smt Genap 2011-2012

B

P

SX

A C D B

P

SX

A C D

Case 1: Both X’s children are black

Outer child of S (C) is Red: do a single rotation and recolour

B

P

SX

A C D

C

S

DP

B

X

A

Smt Genap 2011-2012

Case 1: Both X’s children are Case 1: Both X’s children are blackblackInner child of S (C) is Red: do a double

rotation and recolour

Smt Genap 2011-2012

B

P

SX

A DC

C

SP

B

X

A

D

Case 2: One/Both of X’s Case 2: One/Both of X’s children is redchildren is redRecurse down to X’s childIf we land on a red node, fine.If we land on a black node, rotate

sibling and parent:

Smt Genap 2011-2012

B

P

SX

A C

C

S

P

B

X

AD

D

SummarySummaryRed-Black trees use color as balancing

information instead of height in AVL trees.An insertion may cause a local perturbation

(two consecutive red nodes)The pertubation is either

◦ resolved locally (rotations), or◦ propagated to a higher level in the tree by

recoloring (color flip)O(1) for a rotation or color flipAt most one restructuring per insertion.O(log n) color flipsTotal time: O(log n)

Smt Genap 2011-2012