lecture 39: the rolling stones, masters of the balanced tree
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 39: The Rolling Stones, Masters of the Balanced Tree. (2,4) Trees: Pro & Con. Cons: Cannot reuse BST code , since it is not binary tree Pros: (2,4) Trees balance without rotations Fewer balancing cases than AVL or splay trees. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/1.jpg)
LECTURE 39:THE ROLLING STONES, MASTERS OF THE BALANCED TREE
CSC 213 – Large Scale Programming
![Page 2: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/2.jpg)
(2,4) Trees: Pro & Con
Cons: Cannot reuse BST code, since it is not
binary treePros: (2,4) Trees balance without rotations Fewer balancing cases than AVL or splay
trees
![Page 3: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/3.jpg)
(2,4) Trees: Pro & Con
Cons: Cannot reuse BST code, since it is not
binary treePros: (2,4) Trees balance without rotations Fewer balancing cases than AVL or splay
trees
Sick, twisted, & wrong: n-node naming scheme is crime against
humanity
![Page 4: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/4.jpg)
Red-Black Trees
Represents a (2,4) tree using binary tree Red node when Entry & parent’s Entry share
node (2,4) tree Black node’s Entry also in child of parent’s node
in (2,4) tree 2 6 73 54
4 62 7
53
35OR
![Page 5: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/5.jpg)
Red-Black Trees
Represents a (2,4) tree using binary tree Red node when Entry & parent’s Entry share
node (2,4) tree Black node’s Entry also in child of parent’s node
in (2,4) tree
Maximizes code reuse, since subclass of BST
2 6 73 54
4 62 7
53
35OR
![Page 6: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/6.jpg)
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 7: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/7.jpg)
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 8: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/8.jpg)
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)
68
![Page 9: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/9.jpg)
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)
683
![Page 10: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/10.jpg)
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)
683
![Page 11: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/11.jpg)
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)
683
4
![Page 12: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/12.jpg)
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
683
4
![Page 13: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/13.jpg)
Double Red With Red Aunt
Double red represents creation of 5-node
Perform recoloring (equivalent to (2,4) tree split)
3 4 6 86
83
4
![Page 14: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/14.jpg)
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
As in splitting, double red may propagate up
2 4 6 7
![Page 15: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/15.jpg)
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
As in splitting, double red may propagate up
2 4 6 76 7
… 4 …
2
![Page 16: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/16.jpg)
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
As in splitting, double red may propagate up
2 4 6 76 7
… 4 …
2
4
672
![Page 17: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/17.jpg)
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
As in splitting, double red may propagate up
2 4 6 76 7
… 4 …
2
4
672
4
672
![Page 18: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/18.jpg)
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
As in splitting, double red may propagate up
2 4 6 76 7
… 4 …
2
4
672
4
672
![Page 19: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/19.jpg)
Double Red With Black Aunt
Poorly balanced 4-node causes this double red Restore tree balance to use AVL tree
restructuring Preserves overall balance of the tree
3 4 6683
48
![Page 20: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/20.jpg)
Double Red With Black Aunt
Rebalance tree using AVL tree restructuring
Recolor nodes, but no changes (2,4) tree 4
672
![Page 21: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/21.jpg)
Double Red With Black Aunt
Rebalance tree using AVL tree restructuring
Recolor nodes, but no changes (2,4) tree 4
672 4
67
2
![Page 22: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/22.jpg)
Double Red With Black Aunt
Rebalance tree using AVL tree restructuring
Recolor nodes, but no changes (2,4) tree 4
672
4 6 7
.. 2 ..
46
72
![Page 23: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/23.jpg)
Double Red With Black Aunt
Rebalance tree using AVL tree restructuring
Recolor nodes, but no changes (2,4) tree 4
672
4 6 7
.. 2 ..
46
72
4 6 7
.. 2 ..
![Page 24: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/24.jpg)
Double Red Restructuring
4 different restructures needed to remedy Differ in how node, parent, & grandparent
related Identical result no matter where we start4
67
7
46
7
64
4
76
![Page 25: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/25.jpg)
Double Red Restructuring
4 different restructures needed to remedy Differ in how node, parent, & grandparent
related Identical result no matter where we start4
67
7
46
7
64
4
76
4 76
![Page 26: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/26.jpg)
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 27: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/27.jpg)
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 28: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/28.jpg)
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 29: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/29.jpg)
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 30: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/30.jpg)
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 31: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/31.jpg)
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 32: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/32.jpg)
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 33: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/33.jpg)
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 34: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/34.jpg)
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 35: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/35.jpg)
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 Case 3: sibling is red
Adjust subtree to better represent 3-node Once complete apply case 1 or case 2
![Page 36: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/36.jpg)
Black Sibling With Red Niece Solve double black using (2,4) tree
transfer…9…
6 8 10
9
6 10
8
![Page 37: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/37.jpg)
Black Sibling With Red Niece Solve double black using (2,4) tree
transfer…9…
6 8
9
6
8
![Page 38: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/38.jpg)
Black Sibling With Red Niece Solve double black using (2,4) tree
transfer …8…
6 9
…9…
6 8
9
6
8
![Page 39: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/39.jpg)
Black Sibling With Red Niece Solve double black using (2,4) tree
transfer …8…
6 9
…9…
6 8
9
6
8
8
6 9
![Page 40: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/40.jpg)
Black Sibling With Red Niece Solve double black using AVL
restructuring …8…
6 9
…9…
6 8
9
6
8
8
6 9
![Page 41: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/41.jpg)
Sibling & Children are Black Solve double black using (2,4) tree
fusion5 9
6 10
5
10
9
6
…
…
![Page 42: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/42.jpg)
Sibling & Children are Black Solve double black using (2,4) tree
fusion5 9
6
5
9
6
…
…
![Page 43: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/43.jpg)
Sibling & Children are Black Solve double black using (2,4) tree
fusion5 9
6
5
9
6
…
…
5
… 6 9
![Page 44: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/44.jpg)
Sibling & Children are Black Solve double black recoloring
parent & sibling5 9
6
5
9
6
…
…
5
… 6 9
5
9
6
…
![Page 45: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/45.jpg)
Sibling & Children are Black Solve double black recoloring
parent & sibling5 9
6
5
9
6
…
…
5
… 6 9
5
9
6
…
![Page 46: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/46.jpg)
Sibling & Children are Black If parent already black, it becomes
double black 4
1 10 1 4
41 10
41
4
![Page 47: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/47.jpg)
Sibling is Red
Adjusting double black stalls for time Transforms situation into something we can
fix
9
5 10
4
![Page 48: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/48.jpg)
Sibling is Red
Adjusting double black stalls for time Transforms situation into something we can
fix
9
5 10
4
![Page 49: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/49.jpg)
Sibling is Red
Adjusting double black stalls for time Transforms situation into something we can
fix
9
5
4
![Page 50: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/50.jpg)
Sibling is Red
Adjusting double black stalls for time Transforms situation into something we can
fix
9
5
4
![Page 51: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/51.jpg)
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
54 9
![Page 52: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/52.jpg)
Do the Activity
![Page 53: Lecture 39: The Rolling Stones, Masters of the Balanced Tree](https://reader035.vdocuments.site/reader035/viewer/2022062521/56816758550346895ddc1651/html5/thumbnails/53.jpg)
For Next Lecture
Weekly assignment available to test skills Due at regular time next Tuesday Talk to me if struggling on problems
Look at what all this means in real world code What are B-trees? How are they related to (2,4) & red-black
trees? Where are they used?
Reminder: project #3 tests due today