10 Red Black Trees

126
Analysis of Algorithms Red-Black Trees Andres Mendez-Vazquez October 31, 2014 1 / 97

Transcript of 10 Red Black Trees

Page 1: 10 Red Black Trees

Analysis of AlgorithmsRed-Black Trees

Andres Mendez-Vazquez

October 31, 2014

1 / 97

Page 2: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

2 / 97

Page 3: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

3 / 97

Page 4: 10 Red Black Trees

Red Black Trees: Definitions

DefinitionA Red Black Tree is a BST where each node has an extra field, its color.

Properties1 Every node is either red or black.2 The root is black.3 Every leaf (NIL) is black.4 If a node is red, then both its children are black.5 For each node, all paths from the node to descendant leaves contain

the same number of black nodes.

4 / 97

Page 5: 10 Red Black Trees

Red Black Trees: Definitions

DefinitionA Red Black Tree is a BST where each node has an extra field, its color.

Properties1 Every node is either red or black.2 The root is black.3 Every leaf (NIL) is black.4 If a node is red, then both its children are black.5 For each node, all paths from the node to descendant leaves contain

the same number of black nodes.

4 / 97

Page 6: 10 Red Black Trees

Red Black Trees: Definitions

Example

26

17 41

14 21

10 16

7 12

19

20

23

15

3

30 47

28 38

35 39

T.nil

5 / 97

Page 7: 10 Red Black Trees

Red Black Trees: Definitions

Black height bh(x)We call the number of black nodes on any path from, but not including, anode x down to a leaf the black height of the node.

6 / 97

Page 8: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

7 / 97

Page 9: 10 Red Black Trees

Lemma for the height of RBT

TheoremA RBT with n internal nodes has height at most 2 log(n + 1).

Proof: Step 1Prove that any subtree rooted at x contains at least 2bh(x) − 1internal nodes.If bh(x) = 0, then x is a leaf, then 2bh(x) − 1 = 20 − 1 = 0.Now with bh(x) > 0, we have that child[x] has height bh(x) orbh(x)− 1.

8 / 97

Page 10: 10 Red Black Trees

Lemma for the height of RBT

TheoremA RBT with n internal nodes has height at most 2 log(n + 1).

Proof: Step 1Prove that any subtree rooted at x contains at least 2bh(x) − 1internal nodes.If bh(x) = 0, then x is a leaf, then 2bh(x) − 1 = 20 − 1 = 0.Now with bh(x) > 0, we have that child[x] has height bh(x) orbh(x)− 1.

8 / 97

Page 11: 10 Red Black Trees

Proof

Case IHeight of child is bh(x).Then, the child is red, if not subtree rooted at x will have heightbh(x) + 1!Now, we have two subtrees from the child with red root...

Thus, we haveThus, each of this subtrees has height bh(x)− 1 ⇒ each treecontains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.Then the child contains at least 2bh(x)−1 − 1+ 2bh(x)−1 − 1 internalnodes.Finally, the tree rooted contains 2bh(x)−1 − 1+ 2bh(x)−1 − 1+ 1 (Onefor the node rooted at the child node)

9 / 97

Page 12: 10 Red Black Trees

Proof

Case IHeight of child is bh(x).Then, the child is red, if not subtree rooted at x will have heightbh(x) + 1!Now, we have two subtrees from the child with red root...

Thus, we haveThus, each of this subtrees has height bh(x)− 1 ⇒ each treecontains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.Then the child contains at least 2bh(x)−1 − 1+ 2bh(x)−1 − 1 internalnodes.Finally, the tree rooted contains 2bh(x)−1 − 1+ 2bh(x)−1 − 1+ 1 (Onefor the node rooted at the child node)

9 / 97

Page 13: 10 Red Black Trees

Proof

FinallyThen, the tree with root x contains at least2× 2bh(x)−1 − 1 = 2bh(x) − 1

Case IIHeight of the child is bh(x)− 1.Thus the children are black.Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes byinductive hypothesis.Thus, we have that the tree with root x contains at least2bh(x)−1 − 1+ 2bh(x)−1 − 1+ 1 internal nodes.

10 / 97

Page 14: 10 Red Black Trees

Proof

FinallyThen, the tree with root x contains at least2× 2bh(x)−1 − 1 = 2bh(x) − 1

Case IIHeight of the child is bh(x)− 1.Thus the children are black.Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes byinductive hypothesis.Thus, we have that the tree with root x contains at least2bh(x)−1 − 1+ 2bh(x)−1 − 1+ 1 internal nodes.

10 / 97

Page 15: 10 Red Black Trees

Proof

AgainThen, the tree with root x contains at least2× 2bh(x)−1 − 1 = 2bh(x) − 1

FinallyNow given that h is the height of the tree, we have by property 4 thatbh(T ) ≥ h

2 .

Then n ≥ 2bh(root) − 1 ≥ 2 h2 − 1.

Then, h ≤ 2 log(n + 1) �.

11 / 97

Page 16: 10 Red Black Trees

Proof

AgainThen, the tree with root x contains at least2× 2bh(x)−1 − 1 = 2bh(x) − 1

FinallyNow given that h is the height of the tree, we have by property 4 thatbh(T ) ≥ h

2 .

Then n ≥ 2bh(root) − 1 ≥ 2 h2 − 1.

Then, h ≤ 2 log(n + 1) �.

11 / 97

Page 17: 10 Red Black Trees

Lemma for the height of RBT

CorollaryFrom the previous theorem we conclude that SEARCH, MINIMUM,etcetera, can be implemented in O(log n).

12 / 97

Page 18: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

13 / 97

Page 19: 10 Red Black Trees

Rotations in RBT

PurposeRotations are used to mantain the structure of the RBT.

Types of rotationsThere are left and right rotations, they are inverse or each other.

14 / 97

Page 20: 10 Red Black Trees

Rotations in RBTPurposeRotations are used to mantain the structure of the RBT.

Types of rotationsThere are left and right rotations, they are inverse or each other.

y

xy

x

Left-Rotate(T,x)

Right-Rotate(T,y)

14 / 97

Page 21: 10 Red Black Trees

Example of Rotations in RBT

LEFT-ROTATE(T,x)1 y = x.right . Set y2 x.right = y.left . Turn y’s left subtree into x’s right subtree3 if y.left 6= T.nil4 y.left.p = x5 y.p = x.p . Link x’s parent to y6 if x.p == T.nil7 T.root = y8 elseif x == x.p.left9 x.p.left = y10 else x.p.right = y11 y.left = x . Put x on y’s left12 x.p = y

15 / 97

Page 22: 10 Red Black Trees

Example Left-Rotate

Step 1 - the correct right child is changed1 y = x.right . Set y2 x.right = y.left . Turn y’s left subtree into x’s right subtree

y

x

16 / 97

Page 23: 10 Red Black Trees

Example Left-Rotate

Step 2 - the parents are set correctly3. if y.left 6= T.nil4. y.left.p = x5. y.p = x.p

y

x

17 / 97

Page 24: 10 Red Black Trees

Example Left-RotateStep 36. if x.p == T.nil .Set y to be the root if x was it7. T.root = y8. elseif x == x.p.left9. x.p.left = y10. else x.p.right = y

y

x

18 / 97

Page 25: 10 Red Black Trees

Example Left-Rotate

Step 411. y.left = x . Put x on y’s left12. x.p = y

y

x

19 / 97

Page 26: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

20 / 97

Page 27: 10 Red Black Trees

Insertion Code in RBTRB-INSERT(T , z)

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED17 RB-Insert-Fixup(T.z)

21 / 97

Page 28: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessSearch Variables being Initialized

22 / 97

Page 29: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessBinary search for insertion

22 / 97

Page 30: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessChange parent of the node to beinserted

22 / 97

Page 31: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessTest to see if the Tree is empty!!!

22 / 97

Page 32: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessInsert node z in the correct left or rightchild

I if z.key < y.key ⇒ y.left = zI if z.key ≥ y.key ⇒ y.right = z

22 / 97

Page 33: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessMake z’s leafs equal to T.nil

22 / 97

Page 34: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessMake z’s color equal to RED

22 / 97

Page 35: 10 Red Black Trees

ExplanationSteps

1 y = T.nil

2 x = T.root3 while x 6=T.nil

4 y = x

5 if z.key<x.key

6 x = x.left7 else x = x.right

8 z.p = y

9 if y == T.nil

10 T.root = z11 elseif z.key < y.key

12 y.left = z

13 else y.right = z

14 z.left = T.nil15 z.right = T.nil

16 z.color = T.RED

17 RB-Insert-Fixup(T,z)

ProcessIt calls RB-Insert-Fixup on T and z.

22 / 97

Page 36: 10 Red Black Trees

Tree-Insert Invariance

Initialization:Prior to the first iteration of the loop, we started with a RBT with noviolations. Then, the algorithm insert the red node z at the bottom of theRBT, and this does not violate properties 1,3 and 5.

The RB-INSERT-FIXUP is called1 If z is the first node to be inserted, you violate the property 2. Then,

it is the only violation on the entire RBT.2 If z is not the first node to be inserted than you could be violating

property 4.

23 / 97

Page 37: 10 Red Black Trees

Tree-Insert Invariance

Initialization:Prior to the first iteration of the loop, we started with a RBT with noviolations. Then, the algorithm insert the red node z at the bottom of theRBT, and this does not violate properties 1,3 and 5.

The RB-INSERT-FIXUP is called1 If z is the first node to be inserted, you violate the property 2. Then,

it is the only violation on the entire RBT.2 If z is not the first node to be inserted than you could be violating

property 4.

23 / 97

Page 38: 10 Red Black Trees

RB-INSERT-FIXUPRB-Insert-Fixup(T,z)

1 while z.p.color == RED

2 if z.p == z.p.p.left

3 y=z.p.p.right

4 if y.color ==RED

5 z.p.color = BLACK

6 y.color = BLACK

7 z.p.p.color = RED

8 z = z.p.p

9 else if z == z.p.right

10 z = z.p

11 Left-Rotate(T, z)

12 z.p.color = BLACK

13 z.p.p.color = RED

14 Right-Rotate(T, z.p.p)

15 else (“right” and “left” exchanged)

16 T.root.color = BLACK

Case 1Case 1 z’s uncle is RED

I Change of parent and uncle’s color toBLACK

I Move problem to z’s grandfather

24 / 97

Page 39: 10 Red Black Trees

RB-INSERT-FIXUPRB-Insert-Fixup(T,z)

1 while z.p.color == RED

2 if z.p == z.p.p.left

3 y=z.p.p.right

4 if y.color ==RED

5 z.p.color = BLACK

6 y.color = BLACK

7 z.p.p.color = RED

8 z = z.p.p

9 else if z == z.p.right

10 z = z.p

11 Left-Rotate(T, z)

12 z.p.color = BLACK

13 z.p.p.color = RED

14 Right-Rotate(T, z.p.p)

15 else (“right” and “left” exchanged)

16 T.root.color = BLACK

Case 2if z is in the right child then

I Move the problem to the parent byz=z.p

I Rotate left using z as the rotationnode

24 / 97

Page 40: 10 Red Black Trees

RB-INSERT-FIXUPRB-Insert-Fixup(T,z)

1 while z.p.color == RED

2 if z.p == z.p.p.left

3 y=z.p.p.right

4 if y.color ==RED

5 z.p.color = BLACK

6 y.color = BLACK

7 z.p.p.color = RED

8 z = z.p.p

9 else if z == z.p.right

10 z = z.p

11 Left-Rotate(T, z)

12 z.p.color = BLACK

13 z.p.p.color = RED

14 Right-Rotate(T, z.p.p)

15 else (“right” and “left” exchanged)

16 T.root.color = BLACK

Case 3if z is in the left child then

I Recolor z’s parent to BLACKI recolor z’s grandparent to REDI Rotate right using the grandparent

24 / 97

Page 41: 10 Red Black Trees

RB-INSERT-FIXUPRB-Insert-Fixup(T,z)

1 while z.p.color == RED

2 if z.p == z.p.p.left

3 y=z.p.p.right

4 if y.color ==RED

5 z.p.color = BLACK

6 y.color = BLACK

7 z.p.p.color = RED

8 z = z.p.p

9 else if z == z.p.right

10 z = z.p

11 Left-Rotate(T, z)

12 z.p.color = BLACK

13 z.p.p.color = RED

14 Right-Rotate(T, z.p.p)

15 else (“right” and “left” exchanged)

16 T.root.color = BLACK

Root RecoloringAfter pushing the problem up to theroot by the while loop color the root toblack!!!

24 / 97

Page 42: 10 Red Black Trees

Maintenance in Insertion in RBTCase I z ’s uncle is red = Recolor parent and uncle to black

1 Recoloring will fix the z.parent.color == red and z.color == red andkeeps the bh property, but will move the problem upwards

2 Nevertheless, the tree rooted at A and B are RBT.3 So new z will be fixed in the next iteration keeping the RBT

properties for the subtrees.

25 / 97

Page 43: 10 Red Black Trees

Anomalies after Insertion in RBT

Case 2: z ′s uncle y is black and z is a right child.Case 3: z ′s uncle y is black and z is a left child.

1 Here simple recoloring will not work.2 We rotate first from to left using z3 Then we recolor B.color = BLACK, C.color = RED (No problem γ

and δ are black nodes)4 Then you rotate right using z.p.p to fix the bh property!!!

26 / 97

Page 44: 10 Red Black Trees

Example: Insertion in RBT

Example Tree is empty26 1741 14 21 10 712 3028

T.nil

Selection Order

27 / 97

Page 45: 10 Red Black Trees

Example: Insertion in RBT

The root becomes red!!!STEPS

1 y = T.nil2 x = T.root3 We never go into

the search loop4 z.p = y5 if y == T.nil6 T.root = z7 z.left = T.nil8 z.right = T.nil9 z.color = T.RED

26 1741 14 21 10 712

30

28

T.nil

Selection Order

z

28 / 97

Page 46: 10 Red Black Trees

Example: Insertion in RBT

Fix the problem

ONLY STEP1 T.root.color=BLACK

It happens at the end of theFix-up

26 1741 14 21 10 712

30

28

T.nil

Selection Order

z

29 / 97

Page 47: 10 Red Black Trees

Example: Insertion in RBT

Insert 7

1 Do a binarysearch to find aplace to insert

2 Parent of z is notRED ⇒ no fix-up

26 1741 14 21 10

7

12

30

28

T.nil

Selection Order

z

30 / 97

Page 48: 10 Red Black Trees

Example: Insertion in RBT

Insert 12

1 Do a binarysearch to find aplace to insert

2 Parent of z isRED!!!

26 1741 14 21 10

7

12

30

28

T.nil

Selection Order

z

31 / 97

Page 49: 10 Red Black Trees

Example: Insertion in RBT

Case 2 into Case 3

1 Re-balance first to theleftif z==z.p.right

z=z.pLeft-Rotate(T, z)

26 1741 14 21 10

712

30

28

T.nil

Selection Order

7z

32 / 97

Page 50: 10 Red Black Trees

Example: Insertion in RBT

Case 3

1 Re-color andre-balance to the rightz.p.color = BLACKz.p.p.color=REDRight-Rotate(T, z.p.p)

2 No problem to fix3 The root is already

BLACK so nothinghappens

26 1741 14 21 10

712

30

28

T.nil

Selection Order

7z

33 / 97

Page 51: 10 Red Black Trees

Example: Insertion in RBT

Insert 10

1 We insert 102 We color it to RED3 We need to fix the

problem

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

y

34 / 97

Page 52: 10 Red Black Trees

Example: Insertion in RBT

Case 1

1 if z.p == z.p.p.left2 y=z.p.p.right3 if y.color ==RED

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

y

35 / 97

Page 53: 10 Red Black Trees

Example: Insertion in RBT

Case 1

1 if y.color ==RED2 z.p.color = BLACK3 y.color = BLACK4 z.p.p.color = RED5 z = z.p.p

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

36 / 97

Page 54: 10 Red Black Trees

Example: Insertion in RBT

Case 1 You get out of the loop and...

1 T.root.color = BLACK

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

37 / 97

Page 55: 10 Red Black Trees

Example: Insertion in RBT

Insert 28

1 We insert 282 We color it to RED3 No problem to fix...

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

38 / 97

Page 56: 10 Red Black Trees

Example: Insertion in RBT

Insert 28

1 We insert 282 We color it to RED3 No problem to fix...

26 1741 14 21

10

712

30

28

T.nil

Selection Order

7

z

39 / 97

Page 57: 10 Red Black Trees

Example: Insertion in RBT

Insert 21

1 We insert 212 We color it to RED3 We need to fix...

26 1741 14

21

10

712

30

28

T.nil

Selection Order

7

z

40 / 97

Page 58: 10 Red Black Trees

Example: Insertion in RBT

Case 3

1 Re-color andre-balance to the rightz.p.color = BLACKz.p.p.color=REDRight-Rotate(T, z.p.p)

2 No problem to fix3 The root is already

BLACK so nothinghappens

26 1741 14

2110

712

30

28

T.nil

Selection Order

7

z

41 / 97

Page 59: 10 Red Black Trees

Example: Insertion in RBT

Insert 14

1 We insert 142 We color it to RED3 We need to fix...

26 1741

14

2110

712

30

28

T.nil

Selection Order

7

z

42 / 97

Page 60: 10 Red Black Trees

Example: Insertion in RBT

Case 1

1 if z.p == z.p.p.left2 y=z.p.p.right3 if y.color ==RED

26 1741

14

2110

712

30

28

T.nil

Selection Order

7

z

y

43 / 97

Page 61: 10 Red Black Trees

Example: Insertion in RBT

Case 1

1 if y.color ==RED2 z.p.color = BLACK3 y.color = BLACK4 z.p.p.color = RED5 z = z.p.p

Nothing to do after!!!

26 1741

14

2110

712

30

28

T.nil

Selection Order

7 z

44 / 97

Page 62: 10 Red Black Trees

Example: Insertion in RBTInsert 17

1 Do a binary search to find aplace to insert

2 Parent of z is RED!!!

26

17

41

14

2110

712

30

28

T.nil

Selection Order

7 z

z

45 / 97

Page 63: 10 Red Black Trees

Case 2 into 3

1 Re-balance first to the leftif z==z.p.right

z=z.pLeft-Rotate(T, z)

26

17

41

14

2110

712

30

28

T.nil

Selection Order

7

z

46 / 97

Page 64: 10 Red Black Trees

Example: Insertion in RBTCase 3

1 Re-color andre-balance to the rightz.p.color = BLACKz.p.p.color=REDRight-Rotate(T, z.p.p)

2 No problem to fix3 The root is already

BLACK so nothinghappens

26

17

41

14

2110

712

30

28

T.nil

Selection Order

7

z

46 / 97

Page 65: 10 Red Black Trees

Example: Insertion in RBTCase 3

1 Re-color andre-balance to the rightz.p.color = BLACKz.p.p.color=REDRight-Rotate(T, z.p.p)

2 No problem to fix3 The root is already

BLACK so nothinghappens

26

17

41

14

2110

712

30

28

T.nil

Selection Order

7

z

47 / 97

Page 66: 10 Red Black Trees

Example: Insertion in RBTCase 3

1 Re-color andre-balance to the rightz.p.color = BLACKz.p.p.color=REDRight-Rotate(T, z.p.p)

2 No problem to fix3 The root is already

BLACK so nothinghappens

26

17

41

1421

10

712

30

28

T.nil

Selection Order

7

z

48 / 97

Page 67: 10 Red Black Trees

Example: Insertion in RBTInsert 41

1 Put in the correctnode by binarysearch

2 Because 41’s parentis BLACK nothing tofix

26

17

4114

21

10

712

30

28

T.nil

Selection Order

7

z

49 / 97

Page 68: 10 Red Black Trees

Example: Insertion in RBTCase 1 Symmetrical

1 if z.p == z.p.p.right2 y=z.p.p.left3 if y.color ==RED4 z.p.color = BLACK5 y.color = BLACK6 z.p.p.color = RED7 z = z.p.p

26

17

4114

21

10

712

30

28

T.nil

Selection Order

7

z

50 / 97

Page 69: 10 Red Black Trees

Example: Insertion in RBTCase 1 Symmetrical

1 if z.p == z.p.p.right2 y=z.p.p.left3 if y.color ==RED4 z.p.color = BLACK5 y.color = BLACK6 z.p.p.color = RED7 z = z.p.p

26

17

4114

21

10

712

30

28

T.nil

Selection Order

7

z

51 / 97

Page 70: 10 Red Black Trees

Example: Insertion in RBTCase 2 to case 3 - Symmetrical

1 Re-balance first to the rightif z==z.p.left

z=z.pRight-Rotate(T, z)

26

17

4114

21

10

712

30

28

T.nil

Selection Order

7 z

52 / 97

Page 71: 10 Red Black Trees

Example: Insertion in RBTCase 3 - Symmetrical

1 Re-color and re-balance to therightz.p.color = BLACKz.p.p.color=REDLeft-Rotate(T, z.p.p)

2 No problem to fix3 The root is already BLACK so

nothing happens26

17

4114

21

10

712

30

28

T.nil

Selection Order

7 z

53 / 97

Page 72: 10 Red Black Trees

Example: Insertion in RBTCase 3 - Symmetrical

1 Re-color and re-balance to therightz.p.color = BLACKz.p.p.color=REDLeft-Rotate(T, z.p.p)

2 No problem to fix3 The root is already BLACK so

nothing happens26

17

4114

21

10

712

30

28

T.nil

Selection Order

7

z

54 / 97

Page 73: 10 Red Black Trees

Complexity of RB-Insert

It is easy to see that we have

O(log n) (1)

55 / 97

Page 74: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

56 / 97

Page 75: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 1Store the info of the node to bedeleted

57 / 97

Page 76: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 2If the left child is emptyStore the info of the right childMove z.right into the position ofz

57 / 97

Page 77: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 3If the right child is emptyStore the info of the left childMove z.left into the position of z

57 / 97

Page 78: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 4Find the successor of zStore the info of it: Color andright child

57 / 97

Page 79: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 5If parent of succesor is z thenset parent of x to y.right

57 / 97

Page 80: 10 Red Black Trees

Deletion in RBTRB-DELETE(T,z)

1 y = z2 y-original-color = y.color3 if z.left == T.nil4 x = z.right5 RB-Transplant(T, z, z.right)6 elseif z.right == T.nil7 x = z.left8 RB-Transplant(T, z, z.left)9 else y = Tree-Minimum(z.right)10 y-original-color = y.color11 x = y.right12 if y.p == z13 x.p = y14 else RB-Transplant(T,y,y.right)15 y.right = z.right

16 y.right.p = y

Case 6Substitute y with y.rightset y.right with z.rightset the parent of y.right with y

57 / 97

Page 81: 10 Red Black Trees

Deletion in RBT

RB-DELETE(T,z)

17. RB-Transplant(T, z, y)18. y.left = z.left19. y.left.p = y20. y.color = z.color21. if y-original-color ==BLACK

22. RB-Delete-Fixup(T,x)

Case 7Substitute z with yMake y.left to z.leftMake the parent of y.left to yMake the color of y to the colorof z

58 / 97

Page 82: 10 Red Black Trees

Deletion in RBT

RB-DELETE(T,z)

17. RB-Transplant(T, z, y)18. y.left = z.left19. y.left.p = y20. y.color = z.color21. if y-original-color ==BLACK

22. RB-Delete-Fixup(T,x)

Case 8If y-original-color == BLACKthen call RB-Delete-Fixup(T,x)After all x stores the informationof the node that:

I it was moved into the positionof z

I the node that was moved intothe position of y that movedinto the position of z

58 / 97

Page 83: 10 Red Black Trees

RB-TRANSPLANT

RB-Transplant(T, u, v)1 if u.p == T.nil2 T.root = v3 elseif u == u.p.left4 u.p.left = v5 else u.p.right = v6 v.p = u.p

59 / 97

Page 84: 10 Red Black Trees

We have the following a virtual node y to be deleted

y is a node to be removed or moved around the tree (Look at lines 1or 9 for this)

In line 1, y points to z when it has less than two children and it isremoved.In line 9, when z has two children theny = Tree −Minimum(z .right). Then, y will move to z ′s position.

Now, the color of y ′s can change therefore it gets stored iny − original − color (Lines 2, 10).

Then, when z has two children y 6= z then y moves to z ′s positionand y gets the same color than z .This can produce a violation.

60 / 97

Page 85: 10 Red Black Trees

We have the following a virtual node y to be deleted

y is a node to be removed or moved around the tree (Look at lines 1or 9 for this)

In line 1, y points to z when it has less than two children and it isremoved.In line 9, when z has two children theny = Tree −Minimum(z .right). Then, y will move to z ′s position.

Now, the color of y ′s can change therefore it gets stored iny − original − color (Lines 2, 10).

Then, when z has two children y 6= z then y moves to z ′s positionand y gets the same color than z .This can produce a violation.

60 / 97

Page 86: 10 Red Black Trees

Cont...

In lines 4, 7, and 11, x is set to point toto y ′s only child orT.nil

Since x moves to y ′s original position, the x .p is pointed to y ′sparent. Then,

if z is not the original y ′s parent x .p takes place in line 6 ofRB-Transplant.if z is the the original y ′s parent, we do not want x .p to point to itsince we are going to remove it.Then, in line 13 of RB-Delete, x .p is set to point to y then that willtake the position of z in line 17.

61 / 97

Page 87: 10 Red Black Trees

Cont...

In lines 4, 7, and 11, x is set to point toto y ′s only child orT.nil

Since x moves to y ′s original position, the x .p is pointed to y ′sparent. Then,

if z is not the original y ′s parent x .p takes place in line 6 ofRB-Transplant.if z is the the original y ′s parent, we do not want x .p to point to itsince we are going to remove it.Then, in line 13 of RB-Delete, x .p is set to point to y then that willtake the position of z in line 17.

61 / 97

Page 88: 10 Red Black Trees

Cont...

ThenIf y was originally black after taking the z .color can produce a violation,then RB-DELET-FIXUP is called.

This can happenIf y was originally red the RBT properties still hold.

62 / 97

Page 89: 10 Red Black Trees

Cont...

ThenIf y was originally black after taking the z .color can produce a violation,then RB-DELET-FIXUP is called.

This can happenIf y was originally red the RBT properties still hold.

62 / 97

Page 90: 10 Red Black Trees

Deletion in RBT

QuestionWhat if we delete a black node?

63 / 97

Page 91: 10 Red Black Trees

Explanation

We have three problems1 If y was a root and a red child becomes a the new root, we have

violated property 2.2 If both x and x .p are red, then we have violated property 4.3 Moving y around decreases the bh of a single simple path. Property 5

is violated.

64 / 97

Page 92: 10 Red Black Trees

Fix-up

HowThis could be fixed assuming that x has an “extra black.”This means that the node is “doubly black” or “red-and-black.”

ThusThe procedure RB-DELETE -FIXUP restores properties 1, 2, and 4 byusing the while loop to push the extra BLACK node up the tree until:

If we have that x is a red-and-black, we simply need to change thecolor of the node to BLACK (Line 23).If we have that x is pointing to the root, remove “extra node.”Use suitable rotations and re-colorings until x stops to be a doublyblack node.

65 / 97

Page 93: 10 Red Black Trees

Fix-up

HowThis could be fixed assuming that x has an “extra black.”This means that the node is “doubly black” or “red-and-black.”

ThusThe procedure RB-DELETE -FIXUP restores properties 1, 2, and 4 byusing the while loop to push the extra BLACK node up the tree until:

If we have that x is a red-and-black, we simply need to change thecolor of the node to BLACK (Line 23).If we have that x is pointing to the root, remove “extra node.”Use suitable rotations and re-colorings until x stops to be a doublyblack node.

65 / 97

Page 94: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

While loopBecause a violationon the bh has beencreated you need tomove x up until youhave fixed up

66 / 97

Page 95: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

Finding who you areFind which child areyouMake w the otherchild

67 / 97

Page 96: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

Case 1if x is BLACK and wis REDFix the bh problemby making wBLACK, x’s parentto RED then rotateleft using x’s parentMake w = x.p.rightmoving the problemdown.

68 / 97

Page 97: 10 Red Black Trees

Suitable Rotations and Recoloring

Case 1: x ’s sibling w is red. You keep the bh property of the othersubtrees

69 / 97

Page 98: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

Case 2Now if w.left’s colorand w.right’s color isBLACKWe do somethingsmart decrease thebh height at w bymaking w’s color toREDMove the problemfropm to x to x.p(After all thesubtree have thesame height at x.p)

70 / 97

Page 99: 10 Red Black Trees

Suitable Rotations and Recoloring

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

71 / 97

Page 100: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

Case 3If w.right’s color isBLACKWe do somethingsmart, we re-colorand do a rightrotation at wThis does notchange the RBTproperties of wbut prepare thesituation for fixingthe x height problemin case 4.

72 / 97

Page 101: 10 Red Black Trees

Suitable Rotations and Recoloring

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

73 / 97

Page 102: 10 Red Black Trees

RB-DELETE-FIXUP(T,x)1 while x 6= T.root and x.color == BLACK2 if x == x.p.left3 w = x.p.right4 if w.color == RED5 w.color = BLACK6 x.p.color = RED7 Left-Rotate(T, x.p)8 w = x.p.right9 if w.left.color == BLACK and w.right.color == BLACK10 w.color = RED11 x = x.p12 else if w.right.color == BLACK13 w.left.color = BLACK14 w.color = RED15 Right-Rotate(T, w)16 w = x.p.right17 w.color = x.p.color18 x.p.color = BLACK19 w.right.color = BLACK20 Left-Rotate(T, x.p)21 x = T.root22 else (same with “right” and “left” exchanged)

23 x.color = BLACK

Case 4We are ready to fixour problem!!! withrespect to x (Case 2and 3 where apreparation to fixthe problem)We increase theheight of the bhwith the problem, x.

74 / 97

Page 103: 10 Red Black Trees

Suitable Rotations and Recoloring

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

75 / 97

Page 104: 10 Red Black Trees

Example: Deletion in RBT

Delete 17

26

17

41

14 21

10

712

30

28

T.nil

7

z

76 / 97

Page 105: 10 Red Black Trees

Example: Deletion in RBT

Store the info about z

y = zy-original-color = y.color 26

17

41

14 21

10

712

30

28

T.nil

7

z

77 / 97

Page 106: 10 Red Black Trees

Example: Deletion in RBT

None of the children of z are T.nil, thus

else y = Tree-Minimum(z.right)y-original-color = y.color

x = y.right 26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

78 / 97

Page 107: 10 Red Black Trees

Example: Deletion in RBT

We have that y.p 6= z

else RB-Transplant(T,y,y.right)y.right = z.right

y.right.p = y 26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

79 / 97

Page 108: 10 Red Black Trees

Example: Deletion in RBT

Transplant(T, y, y.right)

elseif y == y.p.lefty.p.left = y.right

y.right.p = y.p26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

y.right.p=y.p

80 / 97

Page 109: 10 Red Black Trees

Example: Deletion in RBT

Now, we move pointers

y.right = z.right

y.right.p = y 26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

y.right.p=y

81 / 97

Page 110: 10 Red Black Trees

Example: Deletion in RBT

Transplant(z,y)

if z.p == T.nilT.root = y

y.p = z.p26

17

41

14

21

10

712

30

28

T.nil

7

z y

x

y.p = z.p

82 / 97

Page 111: 10 Red Black Trees

Example: Deletion in RBT

Move pointers

y.left = z.lefty.left.p = y

y.color = z.color 26

17

41

14

21

10

712

30

28

T.nil

7

z y

x

y.left.p = y

83 / 97

Page 112: 10 Red Black Trees

Example: Deletion in RBT

We remove z safely and we go into Delete-Fixup(T,x)

z.right = NULLz.left = NULLz.parent = NULL

26

17

41

14

21

10

712

30

28

T.nil

7

z y

x

84 / 97

Page 113: 10 Red Black Trees

Example: Deletion in RBT

We remove z safely and we go into Delete-Fixup(T,x)

Never enter into the loopSimply do x.color =BLACK

2641

14

21

10

712

30

28

T.nil

7

y

x

85 / 97

Page 114: 10 Red Black Trees

Example: Deletion in RBT

Delete 12

26

17

41

14 21

10

712

30

28

T.nil

7

z

86 / 97

Page 115: 10 Red Black Trees

Example: Deletion in RBT

None of the children of z are T.nil, thus

else y = Tree-Minimum(z.right)y-original-color = y.color

(BLACK)

x = y.right (T.NIL)26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

87 / 97

Page 116: 10 Red Black Trees

Example: Deletion in RBT

We have that y.p== z

if y.p == z

x.p = y 26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

88 / 97

Page 117: 10 Red Black Trees

Example: Deletion in RBT

Next

RB-Transplant(T, z, y)y.left = z.lefty.left.p = y

y.color = z.color26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

89 / 97

Page 118: 10 Red Black Trees

Example: Deletion in RBT

Next

RB-Transplant(T, z, y)y.left = z.lefty.left.p = y

y.color = z.color26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

90 / 97

Page 119: 10 Red Black Trees

Example: Deletion in RBT

Next

RB-Transplant(T, z, y)y.left = z.lefty.left.p = y

y.color = z.color26

17

41

14 21

10

712

30

28

T.nil

7

z

y

x

91 / 97

Page 120: 10 Red Black Trees

Example: Deletion in RBT

We remove z safely and we go into Delete-Fixup(T,x)

enter into the loopif x == x.p.right

w = x.p.left 26

17

41

14

21

10

30

28

T.nil

7

y

x

w

92 / 97

Page 121: 10 Red Black Trees

Example: Deletion in RBT

We enter into case 4

1 w.color = x.p.color

2 x.p.color = BLACK

3 w.right.color = BLACK26

17

41

14

21

10

30

28

T.nil

7

x

w

93 / 97

Page 122: 10 Red Black Trees

Example: Deletion in RBT

Rotate and move the x

1 Left-Rotate(T, x.p)

2 x = T.root 26

17

41

1421

10 30

28

T.nil

7

x

94 / 97

Page 123: 10 Red Black Trees

Applications of Red-Black Trees

Completely Fair Scheduler (CFS)It is a task scheduler which was merged into 2.6.23 release of theLinux Kernel.It is a replacement of earlier O(1) scheduler.CFS algorithm was designed to maintain balance (fairness) inproviding processor time to tasks.

Sorting using Parallel ImplementationsRunning in O(log log n) time

95 / 97

Page 124: 10 Red Black Trees

Applications of Red-Black Trees

Completely Fair Scheduler (CFS)It is a task scheduler which was merged into 2.6.23 release of theLinux Kernel.It is a replacement of earlier O(1) scheduler.CFS algorithm was designed to maintain balance (fairness) inproviding processor time to tasks.

Sorting using Parallel ImplementationsRunning in O(log log n) time

95 / 97

Page 125: 10 Red Black Trees

Outline

1 Red Black TreesDefinitionsLemma for the height of RBTRotations in RBTInsertion in RBTDeletion in RBTExercices

96 / 97

Page 126: 10 Red Black Trees

Exercices

From Cormen’s book solve13.1-113.1-313.1-513.1-713.2-213.2-313.2-413.2-513.3-213.3-413.4-213.4-4

97 / 97