530 likes | 704 Views
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.
E N D
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
(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 • Sick, twisted, & wrong: • n-node naming scheme is crime against humanity
4 2 6 7 3 5 4 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’sEntry also in child of parent’s nodein (2,4) tree 5 3 6 OR 3 5 2 7
4 2 6 7 3 5 4 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’sEntry also in child of parent’s nodein (2,4) tree • Maximizes code reuse, since subclass of BST 5 3 6 OR 3 5 2 7
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 blackdepth • Number of blackancestorsfor the node 9 4 15 21 2 6 12 7
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
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
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 3 8
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 3 8
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 isred, violates internal property • Must reorganize tree to remove double red • Example: insert(4) 6 3 8 4
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 isred, violates internal property • Must reorganize tree to remove double red • Example: insert(4)makes tree unbalanced 6 3 8 4
Double Red With Red Aunt • Double red represents creation of 5-node • Perform recoloring(equivalent to (2,4) tree split) 6 3 4 6 8 8 3 4
Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • As in splitting, double red may propagate up 2 4 6 7
Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • As in splitting, double red may propagate up … 4 … 2 4 6 7 2 6 7
Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • As in splitting, double red may propagate up … 4 … 2 4 6 7 2 6 7 4 7 2 6
Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • As in splitting, double red may propagate up … 4 … 2 4 6 7 2 6 7 4 4 7 7 2 2 6 6
Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • As in splitting, double red may propagate up … 4 … 2 4 6 7 2 6 7 4 4 7 7 2 2 6 6
Double Red With BlackAunt • Poorly balanced 4-node causes this double red • Restore tree balance to use AVL tree restructuring • Preserves overall balance of the tree 6 3 4 6 8 3 8 4
Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolor nodes, but no changes (2,4) tree 4 7 2 6
Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolor nodes, but no changes (2,4) tree 6 4 7 7 2 4 2 6
Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolor nodes, but no changes (2,4) tree 6 4 7 7 2 4 2 6 4 6 7 .. 2 ..
Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolor nodes, but no changes (2,4) tree 6 4 7 7 2 4 2 6 4 6 7 4 6 7 .. 2 .. .. 2 ..
Double Red Restructuring • 4 different restructuresneeded to remedy • Differ in how node, parent, & grandparent related • Identical result no matter where we start 7 4 7 4 7 4 6 6 6 6 4 7
Double Red Restructuring • 4 different restructuresneeded to remedy • Differ in how node, parent, & grandparent related • Identical result no matter where we start 7 4 7 4 7 4 6 6 6 6 4 7 6 4 7
Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 1 4
Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 1 4
Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 4
Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 4
What’s Blacker Than Black? • If removed Entry & leaf’s siblingalready black • Paint sibling double black • This is an illegal state – violates internal property • Example: remove(8) 6 8 3 4
What’s Blacker Than Black? • If removed Entry & leaf’s siblingalready black • Paint sibling double black • This is an illegal state – violates internal property • Example: remove(8) 6 8 3 4
What’s Blacker Than Black? • If removed Entry & leaf’s siblingalready black • Paint sibling double black • This is an illegal state – violates internal property • Example: remove(8) 6 3 4
What’s Blacker Than Black? • If removed Entry & leaf’s siblingalready black • Paint sibling double black • This is an illegal state – violates internal property • Example: remove(8) causesdouble black 6 3 4
Remedying Double Black • Case 1: sibling is blackwithredchild • Reorder nodes using AVL tree restructure • Case 2: sibling and its children are black • Equal to (2,4) tree underflow, so recolor nodes
Remedying Double Black • Case 1: sibling is blackwithredchild • 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
Black Sibling With Red Niece • Solve double black using (2,4) tree transfer …9… 6 8 10 9 10 6 8
Black Sibling With Red Niece • Solve double black using (2,4) tree transfer …9… 6 8 9 6 8
Black Sibling With Red Niece • Solve double black using (2,4) tree transfer …9… …8… 6 6 8 9 9 6 8
BlackSibling With Red Niece • Solve double black using (2,4) tree transfer …9… …8… 6 6 8 9 9 8 6 9 6 8
Black Sibling With Red Niece • Solve double black using AVL restructuring …9… …8… 6 6 8 9 9 8 6 9 6 8
Sibling & Children are Black • Solve double black using (2,4) tree fusion 5 9 … 6 10 5 9 … 6 10
Sibling & Children are Black • Solve double black using (2,4) tree fusion 5 9 … 6 5 9 … 6
Sibling & Children are Black • Solve double black using (2,4) tree fusion 5 9 5 … … 6 69 5 9 … 6
Sibling & Children are Black • Solve double black recoloring parent & sibling 5 9 5 … … 6 69 5 5 9 9 … … 6 6
Sibling & Children are Black • Solve double black recoloring parent & sibling 5 9 5 … … 6 69 5 5 9 9 … … 6 6
Sibling & Children are Black • If parent already black, it becomes double black 4 1 1 4 10 4 4 4 1 10 1
Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 10 5 4
Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 10 5 4
Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 5 4
Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 5 4