View
214
Download
0
Category
Preview:
Citation preview
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 TreesBinary 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
2 7
5
3
3
5OR
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 has9
154
62 12
7
21
Height of a Red-Black TreeRed-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
Insertioninsert(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 8z
v v
z
Insertioninsert(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
4z
v v
z
Remedying a Double RedDouble red with child z, parent v, and aunt, wCase 1: w is red Double red is a 5-node; recoloring is split equivalent
4
6
7z
v
2 4 6 7
2w
RecoloringRecoloring remedies double red when uncle is redMake 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 76 7
… 4 …
2
4
6
7z
v2
w 4
6
7z
v2
w
Remedying a Double RedDouble red with child z, parent v, and aunt, wCase 2: w is black Double red is illegal structure of a legal 4-node
4
6
7z
vw2
4 6 7
.. 2 ..
RestructuringRestructuring remedies double red when uncle is blackEquivalent (2,4) tree never changes
This only fixes an error in red-black tree’s organizationSince only reorganizes red-black tree, cannot propagate further
4
6
7z
vw2
4 6 7
.. 2 ..
4
6
7
z
v
w2
4 6 7
.. 2 ..
Restructuring (cont.)Four ways we perform this restructuring Just depends on whether double red nodes
are left or right childrenResult is always the same!
4
6
7
7
4
6
7
6
4
4
7
6
4 7
6
Analysis of InsertionTree 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)
Deletionremove(k) starts by performing normal BST deletionRemove 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
4v
r w
1
6
3 8
4
r
r
Deletionremove(k) starts by performing normal BST deletionRemove 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
r w
6
3
4
r
Remedying a Double Black
Different remedy for double black depending on state of sibling, yCase 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
9
6 8 10
8
6 9
9
6 10
8
8
6 9
(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
…
(2,4) Tree Fusion
Recoloring double black like performing fusion in (2,4) tree
4
1 7 1 4
4
1 7
4
1
44
Adjustment
Adjusting double black lets us determine whether we need to recolor or restructure
9
5 10
6
9
5
6
Red-Black Tree Reorganization
Insertion remedy double red
Red-black tree action (2,4) tree action result
restructuringchange of 4-node representation
double red removed
recoloring splitdouble red removed or propagated up
Red-Black Tree Reorganization
Deletion remedy double black
Red-black tree action (2,4) tree action result
restructuring transferdouble black removed
recoloring fusiondouble black removed or propagated up
adjustmentchange of 3-node representation
restructuring or recoloring follows
Your Turn
Insert 1, 2, 3, 4, 5, 6, 7, 8 into a red-black treeThen delete 3, 6, 5, 2, 8, 4, 1, 7 from your tree
Recommended