{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS2_31_Heaps - CS2 Module 31 Category CS Concepts Topic...

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

View Full Document Right Arrow Icon
CS2 Module 31 Category: CS Concepts Topic: Heaps Objectives Heaps Priority Queues Heapsort Revised to use Vector 10/27/2001
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
CS 2 Introduction to Object Oriented Programming Module 31 CS Concepts Heaps, Priority Queues, Heapsort
Image of page 2
Priority Queues In a number of applications a data structure which can produce the smallest item in a collection is useful. Obviously any sorted list can do the job. However, we may wish to have a data structure with the ability to add items at any time and remove the smallest item at any time In the interest of efficiency a novel data structure can be utilized to produce good results. But first, let's be clear on exactly what is our target application
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
Imagine a bakery It’s the kind where when you enter you take a number. Only they don’t have one of those “Now Serving” gadgets. And they don’t mind if you leave and come back. The rule is simple: Lowest number (in the bakery) gets served next. How do you determine who has the smallest number?
Image of page 4
007 Or take, for example, a queue of Qs: (That’s Q from the James Bond movies)
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
008 Perhaps we need to find the smallest in this queue of Qs.
Image of page 6
Alternatives We might search for the smallest one: Or we might keep them in order:
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
Priority Queues A better example is a queue of documents waiting to be printed. Perhaps we wish to find the smallest document, and print it first. This is known as the  Shortest Job First  scheduling algorithm
Image of page 8
Priority Queues Take for example a printer queue. The small jobs take less time to print. So it’s a better policy to print them first 90 mins. 20 mins. 5 mins. 5 secs. Simply printing ‘in order’ can cause bottle necks – maybe we need to beef up the throughput!
Image of page 9

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

View Full Document Right Arrow Icon
Sound Unlikely? So, accept for the moment that we must create a data structure that has the following property: Finding or removing the Finding or removing the smallest element is fast smallest element is fast Now, exactly how might we implement this?
Image of page 10
Poor Technique #1 Plain Linked List We might start with a simple linked list, and keep the list unsorted. 1 1 1 1 2 2 2 2 Insertion is at the head of the list, ( O (1) ). Deletion* requires us to search for the smallest, ( O (N) ). We could also adopt some rules to make the list fast: So insertion is fast, and deletion is slow * Deletion is what we do when we remove the smallest element
Image of page 11

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

View Full Document Right Arrow Icon
Poor Technique #2 We can tinker with our previous solution to try and make it faster. Let’s sort the elements on insertion. 1 1 1 1 2 2 2 2 Insertion is in order ( O (N) ) Deletion simple removes the smallest, ( O (1) ) So deletion is fast, and insertion is slow There’s a tradeoff between insertion and deletion speed
Image of page 12
Better Technique #3 A linked list either required O(N) for either insertion of deletion. Could we instead use a BST to get some log(N) behavior for either of the activities?
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
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