25 - 16:36:27 CS 61B Lecture 25 Monday Todays reading 1 25...

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

View Full Document Right Arrow Icon
10/25/10 16:36:27 1 25 CS 61B: Lecture 25 Monday, October 25, 2010 Today’s reading: Goodrich & Tamassia, Sections 8.1-8.3. PRIORITY QUEUES =============== A priority queue, like a dictionary, contains _entries_ that each consist of a key and an associated value. However, whereas a dictionary is used when we want to be able to look up arbitrary keys, a priority queue is used to prioritize entries. Define a total order on the keys (e.g. alphabetical order). You may identify or remove the entry whose key is the lowest (but no other entry). This limitation helps to make priority queues fast. However, an entry with any key may be inserted at any time. For concreteness, let’s use Integer objects as our keys. The main operations: - insert() adds an entry to the priority queue; - min() returns the entry with the minimum key; and - removeMin() both removes and returns the entry with the minimum key. 5 --------- | --------- --------- |4: womp| v |4: womp| | | |7: gong|-insert(k, v)->|7: gong|-removeMin()->|7: gong|-min() | | ^ |5: hoot| | |5: hoot| | --------- | --------- v --------- v hoot (4, womp) (5, hoot) Priority queues are most commonly used as "event queues" in simulations. Each value on the queue is an event that is expected to take place, and each key is the time the event takes place. A simulation operates by removing successive events from the queue and simulating them. This is why most priority queues return the minimum, rather than maximum, key: we want to simulate the events that occur first first. public interface PriorityQueue { public int size(); public boolean isEmpty(); Entry insert(Object k, Object v); Entry min(); Entry removeMin(); } See page 340 of Goodrich & Tamassia for how they implement an "Entry". Binary Heaps: An Implementation of Priority Queues --------------------------------------------------- A _complete_binary_tree_ is a binary tree in which every row is full, except possibly the bottom row, which is filled from left to right as in the illustration below. Just the keys are shown; the associated values are omitted. 2 index: 0 1 2 3 4 5 6 7 8 9 10 / \ / \ ------------------------------------------------ 5 3 | | 2 | 5 | 3 | 9 | 6 | 11 | 4 | 17 | 10 | 8 | / \ / \ ------------------------------------------------ 9 6 11 4 ^ / \ / | 17 10 8 \--- array index 0 intentionally left empty. A _binary_heap_ is a complete binary tree whose entries satisfy the _heap-order_property_: no child has a key less than its parent’s key. Observe that every subtree of a binary heap is also a binary heap, because every subtree is complete and satisfies the heap-order property. Because they are complete, binary heaps are often stored as arrays of entries, ordered by a level-order traversal of the tree, with the root at index 1. This mapping of tree nodes to array indices is called _level_numbering_. Observe that if a node’s index is i, its children’s indices are 2i and 2i+1, and its parent’s index is floor(i/2). Hence, no node needs to store explicit references to its parent or children. (Array index 0 is left empty to make the indexing work out this nicely. If we instead put the root at index 0, node i’s children are at 2i+1 and 2i+2, and its parent is at floor([i-1]/2).)
Image of page 1

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

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