CSC 213 Lecture 9: Red-Black Trees
Announcements Reminder: Daily Quizzes should take 15 minutes Goal is to provide chance to see if you really understand material If a quiz is taking you longer, you may not understand material as well as you thought Go back and review the book Talk to me Talk to a classmate Talk to the CSC tutors Talk to a learned mentor
Announcements To check if your compiler from lab is working, you see what tokens your system outputs using this command: java –cp java_cup.jar;. mpc filename This will generate a file filename.token which contains all the tokens you found.
(2,4) Trees: Pro & Con Pros: (2,4) Trees are balanced trees with no rotations Contains many fewer balancing cases than AVL or splay trees Cons: Cannot use any BST code we already wrote &%#$*#*: n-node name is a crime and should be abolished
Red-Black Trees Binary tree representation of a (2,4) tree Mark nodes red when entries share a node in (2,4) tree Mark nodes black when also parent & child in (2,4) tree Get to use much of our existing BST code! OR
Red-Black Trees (§ 9.5) Draw the equivalent (2,4) tree:
Red-Black Trees (§ 9.5) Red-black trees are BSTs satisfying these properties: Root Property: Root node is black External Property: All leaf nodes are black Internal Property: Red nodes only have black children Depth Property: All leaves have same black depth Black depth = number of black ancestors a node has
Height of a Red-Black Tree Red-black tree storing n entries has height O(log n) Remember, it is functionally equivalent to (2,4) tree Search a red-black tree identical to searching a BST Searching red-black tree takes ______________ time
Insertion insert (k, o) : Perform usual BST insertion and color external nodes black If new node, z, is root, then color it black, else color z red If parent, v, of z is black, internal property is preserved Else ( v is red) we have double red and must reorganize tree Example: insert(3) is all good z vv z
Insertion insert (k, o) : Perform usual BST insertion and color external nodes black If new node, z, is root, then color it black, else color z red If parent, v, of z is black, internal property is preserved Else ( v is red) we have double red and must reorganize tree Example: insert(4) causes double red z vv z
Remedying a Double Red Double red with child z, parent v, and aunt, w Case 1: w is red Double red is a 5-node; recoloring is split equivalent z v w
Recoloring Recoloring remedies double red when uncle is red Make v and w black and grandparent, u, red If u is root, however, it must stay black Equivalent to splitting a 5-node Just like in (2,4) tree, double red may propagate up to u … 4 … z v 2 w z v 2 w
Remedying a Double Red Double red with child z, parent v, and aunt, w Case 2: w is black Double red is illegal structure of a legal 4-node z vw
Restructuring Restructuring remedies double red when uncle is black Equivalent (2,4) tree never changes This only fixes an error in red-black tree’s organization Since only reorganizes red-black tree, cannot propagate further z v w z v w
Restructuring (cont.) Four ways we perform this restructuring Just depends on whether double red nodes are left or right children Result is always the same!
Analysis of Insertion Tree has O(log n) height Step 1 takes ______ time Step 2 takes ______ time Recoloring takes _____ time Restructuring takes ____ time Step 3 takes _______ time Insertion takes ______ time! Algorithm insert(k, o) 1.Search for k to find insert node z 2.Add new entry at node z and color z red 3. while isRed(z) && isRed(parent(z)) if isBlack(sibling(parent(z))) z restructure(z) return else /* isRed(sibling(parent(z)) */ recolor(z) z parent(z)
Deletion remove (k) starts by performing normal BST deletion Remove Entry at node v, with w being external node removed, and r is its sibling If either v or r was red, color r black Example: remove(1) v rw r r
Deletion remove (k) starts by performing normal BST deletion Remove Entry at node v, with w being external node removed, and r is its sibling If either v or r was red, color r black Else ( v and r were both black), color r double black This violates the internal property Example: remove(8) causes double black: v rw r
Remedying a Double Black Different remedy for double black depending on state of sibling, y Case 1: y is black and has a red child Just need to restructure (from above) tree Case 2: y is black and children are black Underflow in (2,4) tree; perform recoloring Case 3: y is red Use adjustment to represent 3-node better After adjustment, apply case 1 or case 2
(2,4) Tree Transfer Restructuring double black like performing transfer in (2,4) tree
(2,4) Tree Fusion Recoloring double black like performing fusion in (2,4) tree … … … …
(2,4) Tree Fusion Recoloring double black like performing fusion in (2,4) tree
Adjustment Adjusting double black lets us determine whether we need to recolor or restructure
Red-Black Tree Reorganization Insertion remedy double red Red-black tree action(2,4) tree actionresult restructuring change of 4-node representation double red removed recoloringsplit double red removed or propagated up
Red-Black Tree Reorganization Deletion remedy double black Red-black tree action(2,4) tree actionresult restructuringtransfer double black removed recoloringfusion double black removed or propagated up adjustment change of 3-node representation restructuring or recoloring follows
Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into a red- black tree Then delete 3, 6, 5, 2, 8, 4, 1, 7 from your tree
Daily Quiz Write node class for Red-Black tree