lecture00 - 1/29/2009 What's 51 about? COMPUTER SCIENCE 51...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1/29/2009 1 COMPUTER SCIENCE 51 Spring 2009 cs51.seas.harvard.edu Prof. Greg Morrisett Prof. Ramin Zabih What’s 51 about? Programming isn’t hard. Programming well is very hard. We want you to write code that is: Reliable, efficient, readable, testable, provable, maintainable… beautiful ! Expand your problem-solving skills: Recognize problems & map them onto the right abstractions and algorithms. Rule #1 Good programmers are lazy. Never write the same code twice. Why? Reuse libraries. Why? Why? Pick a language that makes the code easy. Example? An Example: Suppose you want to implement a reverse phone book: (don’t; get someone else’s code) Lookup person based on phone number Insert entries into the phone book Many possible data structures… Red-Black Trees A particular kind of balanced search tree 7 4 1 11 5 15 12 17 3 0 Key Invariants: 7 4 1 11 5 15 12 17 3 0 1. No red node has a red child 2. Every path from root has same number of black nodes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1/29/2009 2 Must Rebalance 25 7 4 1 11 5 15 12 17 3 0 ML Code for Insert fun balance(( Blk , T ( Red , T ( Red ,a,x,b),y,c),z,d) |( Blk , T ( Red ,a,x, T ( Red ,b,y,c)),z,d) |( Blk ,a,x, T ( Red , T ( Red ,b,y,c),z,d)) |( Blk ,a,x, T ( Red ,b,y, T ( Red ,c,z,d)))) = T ( Red , T ( Blk ,a,x,b),y, T ( Blk ,c,z,d)) | balance x = T x fun ins x Empty = T (R,Empty,x,Empty) | ins x ( T (color,a,y,b)) = if x <= y then balance(color,ins x a,y,b) else if x > y then balance(color,a,y,ins x b) C code (part 1/4) void rb_insert( Tree T, node x ) { tree_insert( T, x ); x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { y = x->parent->parent->right; if ( y->colour == red ) { x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; x = x->parent->parent; } else { if ( x == x->parent->right ) { x = x->parent; left_rotate( T, x ); } x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); } } else { . . . /* repeat above with red/black swapped */ C code (part 2/4) void left_rotate( Tree T, node x ) { node y; y = x->right; x->right = y->left; if ( y->left != NULL ) y->left->parent = x; y->parent = x->parent;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.

Page1 / 7

lecture00 - 1/29/2009 What's 51 about? COMPUTER SCIENCE 51...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online