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

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

Introduction to Algorithms Lecture 7
Image of page 1
2 Recap Binary-Search Trees Operations Walk Search, Minimum, Maximum Insert, delete
Image of page 2
3 Today Red-black trees Rotations Insertion Deletion AVL-Trees
Image of page 3
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
Image of page 4
5 Rotation B A Right Rotation
Image of page 5
6 Rotation A B Left Rotation
Image of page 6
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 , ,
Image of page 7
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
Image of page 8
9 Left-Rotation y x x y
Image of page 9
10 Right-Rotation y x x y
Image of page 10
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 )).
Image of page 11
12 Red-Black Tree Example 7 3 18 10 22 8 11 26 NIL NIL NIL NIL NIL NIL NIL NIL NIL
Image of page 12
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.
Image of page 13
14 Red-Black Trees 7 3 18 10 22 8 11 26 NIL NIL NIL NIL NIL NIL NIL NIL NIL
Image of page 14
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
Image of page 15
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!
Image of page 16
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).
Image of page 17
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.
Image of page 18
19 Loop Invariant for RB-Trees At the start iteration of the loop: a. Node x is red.
Image of page 19
Image of page 20

You've reached the end of your free preview.

Want to read all 57 pages?

  • Fall '05
  • RudolfFleischer

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes