Priority Queues and Heaps

Chapter 27

 

Priority Queue ADT

•      We want an ADT that lets us remove elements with the highest priority efficiently

–    We also need to be able to add elements

•      Two fundamental operations

–   insert – adds an element to the container

–   deleteMin—finds, returns and removes the minimum element in the container

•      There are other operations, but these two are the basic model

 

Implementation possibilities

•      Linked list

–    What is the time complexity of insert? Of deleteMin?

–    If we keep it sorted, deleteMin is faster, but insert is slower.

•      Array or vector

–    Advantages & disadvantages?

•      Binary Search Tree?

–    Where will deletions be made?

–    How will this effect the balance of the tree?

•      Binary heap

–    BINGO

 

Binary Heap

•      Heaps have two properties:

–   Heap-order property

•   For every node X, the key in the parent of X is smaller than the key in (minimum heap)

•   Except the root, which has no parent

–   Heap-structure property

•   A heap must be a complete binary tree

–  A complete tree is one that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

•      Operations on a heap can destroy the order property, so the operation must not terminate until the heap is reordered

 

Storing the heap

•      We could store it as a tree, using pointer nodes

–    This is the way we think of a heap

•      But since it is a complete binary tree, it is more efficient to store it as an array

•      The root element is stored at index 1, its left child at index 2, right child at index 3, etc.

•      This means the indices can be used to determine parents and children

–    For any element in array index i,

•   the left child is at index 2i and the right child is at index 2i+1

•   The parent is at index i/2 (integer division)

 

ADT for a MaxHeap

•      Data fields

–  Comparable [ ] heap;  // to store the heap

–  int lastIndex;   // keep track of last item

•      Methods

–  void add(Object newElement)

–  Object removeMax( )

–  Object getMax( )

–  boolean isEmpty( )

–  int size ( )

 

The add operation — percolate up

•      We want to insert a new element in our heap

•      Create a hole in the next available spot in the complete binary tree

•      Insert key X

–   Put X in the hole if it does not violate the heap-order property

–   If it does, move the parent of the hole down into the hole and insert X if it does not violate the heap-order property, iteratively

•      Code on overhead, or in text, p. 633

 

removeMax calls reheap

•      Removing the root is the whole purpose of the priority queue, so it is often done.

–   Remove the root—this creates a hole at the top

–   The binary tree is no longer complete, so the last element X would go into the hole

–   If it does not belong there, move up the largest child of the root

–   This creates a hole in the second level

–   If X does not belong there, move up the hole’s largest child repeatedly until X can be put in without violating the heap-order property

 

 

 

buildHeap calls reheap

•      An ordinary array can be thought of as a complete binary tree stored in a vector

•      The operation buildHeap gives the array heap-ordering.

•      It starts at the bottom of the tree, and runs reheap on each subtree

•      Assignment:  Build a heap from an array containing these numbers: 20, 15, 10, 5, 4, 6, 12, 8 

–    Show the entire array after each pass through the buildHeap for loop

 

 

Time complexity of Priority Queues
(implemented as a heap)

•      What is the worst case time to insert an element?

–    Use percolateUp

–    The worst case would be if a minimum element is inserted

–    The average case should be about half of that

–    As a matter of fact, it has been shown that the average number comparisons to insert is less than three

–    This makes average inserting time constant.

•      What is the worst case time to deleteMin and reheap?

–    Remove top element, the use reheap

•      How does this compare with the other ways we looked at to implement a Priority Queue?

•      What is it about a heap that gives us such good time complexity?

 

Using heaps to sort

•      How can we use a heap to change an unsorted array into a sorted array?

Heapsort

•      First- buildHeap

–    What is the time complexity here?

•      Then, take the smallest element and put it in an array

–    What is the time complexity here?

–    You have to rebuild the heap

•      Keep doing this until the heap is empty; at this point the array is sorted

–    What was the time complexity?

•      This requires an extra array to store the sorted list

 

Heapsort in place

•      Every time you deleteMin from the heap, currentSize becomes one smaller

•      The cell that was last in the heap can be used to store the element that was just deleted.

•      Look at an example heap:
26, 41, 31, 59, 58, 60, 53