Download presentation
Presentation is loading. Please wait.
Published byScott Bailey Modified over 9 years ago
1
CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees
2
Today’s Goal Implement (2,4)-trees with a binary tree Colors nodes red & black Using colors, group Entrys into (2,4) nodes Provides O (log n ) time for insert, remove, & find
3
(2,4) Trees: Pro & Con Cons: Cannot use existing BST code Pros: (2,4) Trees balance without rotations Fewer balancing cases than AVL or splay trees Just sick, twisted, & wrong: n -node naming scheme is crime against humanity
4
Red-Black Trees Binary tree representation of (2,4) tree Red node is part of parent’s node in (2,4) tree Black node is also child of parent’s node in (2,4) tree As a BST, maximizes reuse of code 2 6 73 54 4 6 27 5 3 3 5 OR
5
Red-Black Tree Properties Root Property: Root node is black External Property: Leaves are black Internal Property: Red nodes’ children are black Depth Property: Leaves have identical black depth Black depth = number of black ancestors a node has 9 154 6212 7 21
6
Insertion Insert Entry into BST as usual If new node is leaf, color it red If new Node is root, paint it black If node’s parent is red, violates internal property double red Must reorganize tree to remove double red Example: insert(3) 6 8 3
7
Insertion Insert Entry into BST as usual If new node is leaf, color it red If new Node is root, paint it black If node’s parent is red, violates internal property double red Must reorganize tree to remove double red Example: insert(4) needs balancing 6 8 3 4
8
Remedying Double Red If aunt of node is red Double red Double red denotes creation of 5-node Perform recoloring (equivalent to (2,4) tree split) 3 4 6 8 6 8 3 4
9
Recoloring double red Remedies double red when uncle is red Paint parent & uncle black, grandparent red If grandparent is root, must also paint it black double red As in splitting, double red may propagate up 2 4 6 7 6 7 … 4 … 2 4 6 7 2 4 6 7 2
10
Remedying a Double Red Otherwise, node’s aunt is black Double red Double red is poorly balanced 4-node Restructure the 3 nodes like an AVL tree Preserves overall balance of the tree 3 4 6 6 8 3 4 8
11
Restructuring double red Remedies double red when uncle is black No further propagation -- just an AVL balance 4 6 7 2 4 6 7.. 2.. 4 6 7 2 4 6 7.. 2..
12
Restructuring (cont.) Four ways to perform restructuring Differ only in 3 nodes relationships All end with identical result! 4 6 7 7 4 6 7 6 4 4 7 6 4 7 6
13
Deletion Start with normal BST deletion If removed Entry or external node’s sibling red Paint external node’s sibling black Example: remove(1) 6 3 8 4 1
14
Deletion If removed Entry & external node’s sibling are black double black Paint sibling double black This causes violation of internal property double black Example: remove(8) causes double black 6 3 8 4
15
Double Black Remedying Double Black Solution depends on sibling Case 1: sibling is black with red child Restructure nodes like in an AVL tree Case 2: sibling and its children are black Equal to (2,4) tree underflow; perform recoloring Case 3: sibling is red adjust so as to represent 3-node better After adjustment, can then apply case 1 or case 2
16
(2,4) Tree Transfer double black Restructuring double black is like transfer 9 6 8 10 8 6 9 9 6 8 8 6 9
17
(2,4) Tree Fusion double black Recoloring double black is like fusion 5 9 6 10 5 … 6 9 5 10 9 6 … … 5 9 6 …
18
(2,4) Tree Fusion double black Recoloring double black is like fusion 4 1 7 1 4 4 1 7 4 1 4 4
19
Adjustment double black Adjusting double black stalls for time Transforms into situation we can fix 9 5 10 6 9 5 6
20
For Friday Friday is BST problem day Bring any questions you may have to answer Will have variety of BST-related problems Gives you last chance to work on these ideas
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.