CS302 --- Heaps and Priority Queues

Brad Vander Zanden


Definition

Priority Queue: A data structure whose basic operations are:
  1. inserting new elements, and
  2. removing the largest or smallest element.

Example Uses


Operations we will Support

  1. Construct a priority queue from a set of items
  2. Insert a new item
  3. Remove the smallest item

Elementary Implementations

  • Unordered list: Use an array of unordered items
    1. Construct: add items to the list one at a time -- O(n)
    2. Insert: add an item to the end of the list -- O(1)
    3. Remove smallest: search the list, remove the smallest item and return it -- O(n)

  • Ordered List: Use an array of ordered items
    1. Construct: requires a sort -- O(n log n)
    2. Insert: find the proper place in the list, move all smaller elements one position to the right -- O(n)
    3. Remove smallest: remove the first element -- O(1)

    Heap Data Structure


    Algorithms on Heaps

    Running Times