Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSC 213 Lecture 9: Red-Black Trees

2 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

3 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.

4 (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

5 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! 2 6 73 54 4 6 27 5 3 3 5 OR

6 Red-Black Trees (§ 9.5) Draw the equivalent (2,4) tree: 9 154 6212 7 21

7 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 9 154 6212 7 21

8 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

9 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 6 8 6 3 8 z vv z

10 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 6 3 8 6 3 8 4 z vv z

11 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 4 6 7 z v 2 4 6 7 2 w

12 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 2 4 6 7 6 7 … 4 … 2 4 6 7 z v 2 w 4 6 7 z v 2 w

13 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 4 6 7 z vw 2 4 6 7.. 2..

14 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 4 6 7 z v w 2 4 6 7.. 2.. 4 6 7 z v w 2 4 6 7.. 2..

15 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! 4 6 7 7 4 6 7 6 4 4 7 6 4 7 6

16 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)

17 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) 6 3 8 4 v rw 1 6 3 8 4 r r

18 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: 6 3 8 4 v rw 6 3 4 r

19 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

20 (2,4) Tree Transfer Restructuring double black like performing transfer in (2,4) tree 9 6 8 10 8 6 9 9 6 8 8 6 9

21 (2,4) Tree Fusion Recoloring double black like performing fusion in (2,4) tree 5 9 6 10 5 … 6 9 5 10 9 6 … … 5 9 6 …

22 (2,4) Tree Fusion Recoloring double black like performing fusion in (2,4) tree 4 1 7 1 4 4 1 7 4 1 4 4

23 Adjustment Adjusting double black lets us determine whether we need to recolor or restructure 9 5 10 6 9 5 6

24 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

25 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

26 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

27 Daily Quiz Write node class for Red-Black tree


Download ppt "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."

Similar presentations


Ads by Google