CSC 213 – Large Scale Programming. Red-Black Tree Properties black Root Property: Root node...

41
LECTURE 22: THE ROLLING STONES, MASTERS OF THE BALANCED TREE CSC 213 – Large Scale Programming

Transcript of CSC 213 – Large Scale Programming. Red-Black Tree Properties black Root Property: Root node...

Page 1: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

LECTURE 22:THE ROLLING STONES, MASTERS OF THE BALANCED TREE

CSC 213 – Large Scale Programming

Page 2: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Red-Black Tree Properties

Root Property: Root node painted black External Property: Leaves are painted

black Internal Property: Red nodes’ children are

black Depth Property: Leaves have identical

black depth Number of black ancestors for the node

9

154

62 12

7

21

Page 3: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

Begins as BST insertion (just like splay & AVL)

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes

Page 4: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

Begins as BST insertion (just like splay & AVL)

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes Example: insert(3)

6

8

Page 5: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

Begins as BST insertion (just like splay & AVL)

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes Example: insert(3)

6

83

Page 6: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

Begins as BST insertion (just like splay & AVL)

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes Example: insert(3)

6

83

Page 7: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes If node’s parent is red, violates internal

property Must reorganize tree to remove double

red Example: insert(4)

6

83

4

Page 8: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Insertion

New node’s initial color set by where it is If node is root, paint it black Other nodes colored red when insertion

completes If node’s parent is red, violates internal

property Must reorganize tree to remove double

red Example: insert(4)makes tree

unbalanced

6

83

4

Page 9: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Red Aunt

Double red represents creation of 5-node

Perform recoloring to find if balance really off

6

83

4

Page 10: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Red Aunt

Parent & uncle painted black, grandparent red When grandparent is root, must paint it

black When easier, promote 2nd Entry to parent

Must continue check, double red can propagate

4

6

72

Page 11: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Red Aunt

Parent & uncle painted black, grandparent red When grandparent is root, must paint it

black When easier, promote 2nd Entry to parent

Must continue check, double red can propagate

4

6

724

6

72

Page 12: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Red Aunt

Parent & uncle painted black, grandparent red When grandparent is root, must paint it

black When easier, promote 2nd Entry to parent

Must continue check, double red can propagate

4

6

724

6

72

Page 13: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Black Aunt

Poorly balanced tree causes this double red Restore tree balance to use AVL tree

restructuring Preserves overall balance of the tree6

83

4

Page 14: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Black Aunt

Rebalance tree using AVL tree restructuring

Recolors nodes also, but changes are very small

4

6

72

Page 15: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red With Black Aunt

Rebalance tree using AVL tree restructuring

Recolors nodes also, but changes are very small

4

6

72 4

6

7

2

Page 16: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red Restructuring

4 different restructures needed to remedy Differ in how node, parent, & grandparent

related Identical result no matter where we start4

6

77

4

6

7

6

44

7

6

Page 17: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Double Red Restructuring

4 different restructures needed to remedy Differ in how node, parent, & grandparent

related Identical result no matter where we start4

6

77

4

6

7

6

44

7

6

4 7

6

Page 18: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Deletion

Start with normal BST deletion If Entry in red node or leafs’s sibling

red Leaf’s sibling is painted black

Example: remove(1)6

3 8

41

Page 19: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Deletion

Start with normal BST deletion If Entry in red node or leafs’s sibling

red Leaf’s sibling is painted black

Example: remove(1)6

3 8

41

Page 20: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Deletion

Start with normal BST deletion If Entry in red node or leafs’s sibling

red Leaf’s sibling is painted black

Example: remove(1)6

3 8

4

Page 21: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Deletion

Start with normal BST deletion If Entry in red node or leafs’s sibling

red Leaf’s sibling is painted black

Example: remove(1)6

3 8

4

Page 22: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

What’s Blacker Than Black?

If removed Entry & leaf’s sibling already black Paint sibling double black This is an illegal state – violates internal

property Example: remove(8)6

3 8

4

Page 23: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

What’s Blacker Than Black?

If removed Entry & leaf’s sibling already black Paint sibling double black This is an illegal state – violates internal

property Example: remove(8)6

3 8

4

Page 24: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

What’s Blacker Than Black?

If removed Entry & leaf’s sibling already black Paint sibling double black This is an illegal state – violates internal

property Example: remove(8)6

3

4

Page 25: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

What’s Blacker Than Black?

If removed Entry & leaf’s sibling already black Paint sibling double black This is an illegal state – violates internal

property Example: remove(8) causes double

black6

3

4

Page 26: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Remedying Double Black

Case 1: sibling is black with red child Reorder nodes using AVL tree restructure

Case 2: sibling and its children are black Equal to (2,4) tree underflow, so recolor

nodes

Page 27: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Remedying Double Black

Case 1: sibling is black with red child Reorder nodes using AVL tree restructure

Case 2: sibling and its children are black Just a bad balance, so just recolor nodes

Case 3: sibling is red Adjust subtree to better balance tree Once complete apply case 1 or case 2

Page 28: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Black Sibling With Red Niece Solve double black using AVL

restructuring

9

6 10

8

Page 29: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Black Sibling With Red Niece Solve double black using AVL

restructuring

9

6

8

Page 30: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Black Sibling With Red Niece Solve double black using AVL

restructuring

9

6

8

8

6 9

Page 31: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling & Children are Black Solve double black recoloring

parent & sibling

5

10

9

6

Page 32: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling & Children are Black Solve double black recoloring

parent & sibling

5

9

6

Page 33: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling & Children are Black Solve double black recoloring

parent & sibling

5

9

6

5

9

6

Page 34: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling & Children are Black Solve double black recoloring

parent & sibling

5

9

6

5

9

6

Page 35: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling is Red

Adjusting double black stalls for time Transforms situation into something we can

fix

9

5 10

4

Page 36: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling is Red

Adjusting double black stalls for time Transforms situation into something we can

fix

9

5 10

4

Page 37: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling is Red

Adjusting double black stalls for time Transforms situation into something we can

fix

9

5

4

Page 38: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling is Red

Adjusting double black stalls for time Transforms situation into something we can

fix

9

5

4

Page 39: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Sibling is Red

Adjusting double black stalls for time Transforms situation into something we can

fix Once completed, re-examine double black

node9

5

4

5

4 9

Page 40: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

Do the Activity

Page 41: CSC 213 – Large Scale Programming. Red-Black Tree Properties black  Root Property: Root node painted black black  External Property: Leaves are painted.

For Next Lecture

Weekly assignment available to test skills Due at regular time Tuesday after break Talk to me if struggling on problems

Last idea from Map/Dictionary after break What are Sets? Why are we getting to

them now? It may not appear related, but how are they

used? Reminder: lab phase #2 due Monday

after break