ADT Priority Queue Heaps A Heap Implementation of the ADT Priority Queue Heapsort 1
ADT Priority Queue 3 The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value 2
Operations of the ADT priority queue Create an empty priority queue Determine whether a priority queue is empty Insert a new item into a priority queue Retrieve and then delete the item in a priority queue with the highest priority value Pseudocode for the operations of the ADT priority queue o createpqueue() o // Creates an empty priority queue. o pqisempty() o // Determines whether a priority queue is empty. o pqinsert(newitem) throws PQueueException o // Inserts newitem into a priority queue. Throws PQueueException if priority queue is full. o pqdelete() o // Retrieves and then deletes the item in a priority queue with the highest priority value. 3
Possible implementations Sorted linear implementations o Appropriate if the number of items in the priority queue is small o Array-based implementation o Maintains the items sorted in ascending order of priority value o Reference-based implementation o Maintains the items sorted in descending order of priority value Possible implementations Sorted linear implementations o Binary search tree implementation 4
Heaps 9 A heap is a complete binary tree That is empty or Whose root contains a search key greater than or equal to the search key in each of its children, and Whose root has heaps as its subtrees 5
Heap differs from a binary tree in two significant ways Binary search trees are sorted, heaps are ordered in a weaker way. Binary search trees come in many different shapes, heaps are always complete binary tree Maxheap vs. Minheap Maxheap o A heap in which the root contains the item with the largest search key Minheap o A heap in which the root contains the item with the smallest search key Pseudocode for the operations of the ADT heap o createheap() o // Creates an empty heap. o heapisempty() o // Determines whether a heap is empty. o heapinsert(newitem) throws HeapException o // Inserts newitem into a heap. Throws HeapException if heap is full. o heapdelete() o // Retrieves and then deletes a heap s root o // item. This item has the largest search key 6
Heaps: An Array-based Implementation of a Heap Data fields o items: an array of heap items o size: an integer equal to the number of items in the heap Heaps: An Array-based Implementation of a Heap heapdelete o Step 1: Return the item in the root o Results in disjoint heaps 7
Heaps: An Array-based Implementation of a Heap heapdelete o Step 2: Copy the item from the last node into the root o Results in a semiheap Heaps: An Array-based Implementation of a Heap heapdelete o Step 3: Transform the semiheap back into a heap o Performed by the recursive algorithm heaprebuild 8
Efficiency heapdelete is O(log n) heapinsert Strategy o Insert newitem into the bottom of the tree o Trickle new item up to appropriate spot in the tree Efficiency: O(log n) 9
A Heap Implementation of the ADT Priority Queue 19 A Heap Implementation of the ADT Priority Queue Heap Class o Represents an array-based implementation of the ADT heap Priority-queue operations and heap operations are analogous o The priority value in a priority-queue corresponds to a heap item s search key 10
A Heap Implementation of the ADT Priority Queue Disadvantage o Requires the knowledge of the priority queue s maximum size Advantage o A heap is always balanced Heapsort 22 11
Heapsort Strategy o Transforms the array into a heap o Removes the heap's root (the largest element) by exchanging it with the heap s last element o Transforms the resulting semiheap back into a heap Heapsort Efficiency o Compared to mergesort o Both heapsort and mergesort are O(n * log n) in both the worst and average cases o Advantage over mergesort o Heapsort does not require a second array o Compared to quicksort o Quicksort is the preferred sorting method 12
HOMEWORK Read through Chapter 12.2 13