{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture11 - Introduction to Algorithms...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine
Image of page 1

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 20 L11.2 © 2001 by Charles E. Leiserson Dynamic order statistics OS-S ELECT ( i , S ) : returns the i th smallest element in the dynamic set S . OS-R ANK ( x , S ) : returns the rank of x S in the sorted order of S ’s elements. I DEA : Use a red-black tree for the set S , but keep subtree sizes in the nodes. key size key size Notation for nodes:
Image of page 2
Introduction to Algorithms Day 20 L11.3 © 2001 by Charles E. Leiserson Example of an OS-tree M 9 M 9 C 5 C 5 A 1 A 1 F 3 F 3 N 1 N 1 Q 1 Q 1 P 3 P 3 H 1 H 1 D 1 D 1 size [ x ] = size [ left [ x ]] + size [ right [ x ]] + 1
Image of page 3

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 20 L11.4 © 2001 by Charles E. Leiserson Selection OS-S ELECT ( x , i ) i th smallest element in the subtree rooted at x k size [ left [ x ]] + 1 k = rank( x ) if i = k then return x if i < k then return OS-S ELECT ( left [ x ] , i ) else return OS-S ELECT ( right [ x ] , i – k ) Implementation trick: Use a sentinel (dummy record) for NIL such that size [ NIL ] = 0 . (OS-R ANK is in the textbook.)
Image of page 4
Introduction to Algorithms Day 20 L11.5 © 2001 by Charles E. Leiserson Example M 9 M 9 C 5 C 5 A 1 A 1 F 3 F 3 N 1 N 1 Q 1 Q 1 P 3 P 3 H 1 H 1 D 1 D 1 OS-S ELECT ( root , 5) i = 5 k = 6 9 i = 5 k = 2 i = 3 k = 2 i = 1 k = 1 Running time = O ( h ) = O (lg n ) for red-black trees.
Image of page 5

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 20 L11.6 © 2001 by Charles E. Leiserson Data structure maintenance Q. Why not keep the ranks themselves in the nodes instead of subtree sizes? A. They are hard to maintain when the red-black tree is modified. Modifying operations: I NSERT and D ELETE . Strategy: Update subtree sizes when inserting or deleting.
Image of page 6
Introduction to Algorithms Day 20 L11.7 © 2001 by Charles E. Leiserson Example of insertion M 9 M 9 C 5 C 5 A 1 A 1 F 3 F 3 N 1 N 1 Q 1 Q 1 P 3 P 3 H 1 H 1 D 1 D 1 I NSERT (“K”) 10 10 6 6 4 4 2 2 K 1 K 1
Image of page 7

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

View Full Document Right Arrow Icon
Introduction to Algorithms Day 20 L11.8 © 2001 by Charles E. Leiserson Handling rebalancing Don’t forget that RB-I NSERT and RB-D ELETE may also need to modify the red-black tree in order to maintain balance.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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