lecture7.ppt - Introduction to Algorithms Lecture 7 Recap • Binary-Search Trees • Operations – Walk – Search Minimum Maximum – Insert delete 2

# lecture7.ppt - Introduction to Algorithms Lecture 7 Recap...

• Notes
• 57

This preview shows page 1 - 20 out of 57 pages.

Introduction to Algorithms Lecture 7
2 Recap Binary-Search Trees Operations Walk Search, Minimum, Maximum Insert, delete
3 Today Red-black trees Rotations Insertion Deletion AVL-Trees
4 Balanced Search Trees Worst case height in unbalanced search tree s is O ( n ) . Balanced search trees guarantee height O (lg n ) . Many kinds of balanced trees. A B C D
5 Rotation B A Right Rotation
6 Rotation A B Left Rotation
7 Rotation Basic operation for maintaining balanced trees. Maintains “inorder” key ordering: we have a A b B c Depth( ) decrease by 1 . Depth( ) stays same. Depth( ) increases by 1 . Rotation takes O (1) time. c b a , ,
8 Left-Rotation Left-Rotate ( T , x ) 1. y right [ x ] 2. right [ x ] left [ y ] 3. if left [ y ] NIL 4. then p [ left [ y ]] x 5. p [ y ] p [ x ] 6. if p [ x ] = NIL 7. then root[ T ] y 8. else if x = left [ p [ x ]] 9. then left [ p [ x ]] y 10. else right [ p [ x ]] y 11. left [ y ] x 12. p [ x ] y
9 Left-Rotation y x x y
10 Right-Rotation y x x y
11 Red-Black Trees Every node is either red or black. Root and leaves ( NIL ) are black. If a node is red, then both children are black. All paths from any node x to a descendant leaf have same number of black nodes (Black-Height( x )).
12 Red-Black Tree Example 7 3 18 10 22 8 11 26 NIL NIL NIL NIL NIL NIL NIL NIL NIL
13 Red-Black Trees Theorem. A red-black tree with n keys has height h 2lg( n +1) . Proof (Intuition): “Merge” the red nodes into their parents.
14 Red-Black Trees 7 3 18 10 22 8 11 26 NIL NIL NIL NIL NIL NIL NIL NIL NIL
15 Red-Black Trees Produces a tree with nodes having 2, 3 or 4 children. 3 NIL NIL NIL NIL NIL NIL NIL NIL NIL
16 Red-Black Trees Height h of new tree is black height of orig inal tree. h h /2 n + 1 leaves n + 1 2 h lg( n + 1) h h /2 . Corollary: Search, Min, Successor, etc. take O ( h ) = O (l g n ) time on a red-black tree!
17 Red-Black Insertion Insert x into tree. Color x red. Red-black property 1 still holds. Red-black property 2 still holds (inserted node has NIL ’s for children). Red-black property 4 still holds ( x replaces a black NIL and x has NIL children).
18 Red-Black Insertion If p [ x ] is red, then property 3 is violated. To correct, we move violation up tree until we find a place at which it can be fixed! No new violations are introduced. For each iteration, there are six possible cases.
19 Loop Invariant for RB-Trees At the start iteration of the loop: a. Node x is red.

#### You've reached the end of your free preview.

Want to read all 57 pages?

• Fall '05
• RudolfFleischer

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern