Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues.

Similar presentations


Presentation on theme: "Priority Queues."— Presentation transcript:

1 Priority Queues

2 ADT Priority Queue A priority queue is a data structure for maintaining a set S of elements, such that the element with the largest (max-priority queue) or smallest (min-priority queue) priority is easy to extract. Can be used to store key-value pairs, where key is used to determine the “size” of the pair

3 Priority Queue Applications
Scheduling jobs on a computer A max-priority queue keeps track of jobs to be performed and their relative priorities When a job is finished or interrupted, the next highest-priority job is selected Event-driven simulation A min-priority queue keeps events to be simulated, along with the future time those events should occur The event which will occur closest in the future (i.e., with the smallest time) is the first event in the queue

4 Public Interfaces of NPSPriorityQueue
public interface NPSPriorityQueue<E extends Comparable> {     /** Adds an element to the priority queue  */     public void add(E element);          /** Removes all elements from the priority queue. */     public void clear( );     /** Determines whether the priority queue is empty or not. */     public boolean isEmpty( );     /** Returns the head element without removing it */     public E peek( ) throws NPSQueueEmptyException;      /** Removes and returns the head element */     public E poll( ) throws NPSQueueEmptyException;    /** Returns the number of elements in the priority queue */     public int size( ); }

5 Defining the NPSPriorityQueue ADT
Options for implementing NPSPriorityQueue Sorted List SortedArrayList SortedLinkedList Heap What are the benefits/disadvantages of using a sorted list?

6 NPSSortedArrayList public class NPSSortedArrayList<E> implements NPSList<E> { /** Default initial size of an array */ public static final int DEFAULT_SIZE = 25; /** Flag for not finding the specified element */ public static final int NOT_FOUND = -1; /** An array to store the elements */ private E[] element; /** The number of elements in the list */ private int count; /** Comparator used to order the elements of E[] */ private Comparator<E> comparator;

7 NPSSortedArrayList /**
* Creates a list with the size parameter as its initial capacity, and * sorted using the given comparator * size initial size of the array comparator comparator used to sort the array IllegalArgumentException if negative size is passed */ public NPSSortedArrayList(int size, Comparator<E> comparator) { this(size); this.comparator = comparator; }

8 NPSSortedArrayList /**
* Adds an item to end of the list. Overflow condition * is handled automatically. * item an item to add. */ public void add(E item) { if (count == element.length) { expand(); } int index; if (comparator != null)‏ index = binarySearch(element, item, comparator); else index = binarySearch(element, item); if (index >= 0)‏ addAtIndex(index, item); addAtIndex((-index - 1), item);

9 NPSSortedArrayList /**
* Finds the index position of the specified item. * If the item is not found, then NOT_FOUND is * return. If the list is empty, the method * returns NOT_FOUND by default. */ public int indexOf(E item) { int loc = 0; if (comparator != null)‏ loc = binarySearch(element, item, comparator); else loc = binarySearch(element, item); if (loc < 0) { loc = NOT_FOUND; } return loc;

10 NPSSortedArrayList /**
* Replaces the current element at the index position * with the passed element. * index the position of the object to be replaced item the new item to replace the current element the current element at the index position if the * replacement is made IndexOutOfBoundsException when the index is invalid */ public E set(int index, E item) throws IndexOutOfBoundsException { checkForBounds(index, size() - 1); E old = element[index]; remove(index); add(item); return old; }

11 NPSSortedListPQ public class NPSSortedListPQ<E> implements NPSPriorityQueue<E> { /** The priority queue, implemented as an NPSSortedArrayList */ NPSSortedArrayList<E> pq; // // Constructors public NPSSortedListPQ() { pq = new NPSSortedArrayList<E>(); } public NPSSortedListPQ(int size) { pq = new NPSSortedArrayList<E>(size); public NPSSortedListPQ(int size, Comparator<E> comparator) { pq = new NPSSortedArrayList<E>(size, comparator);

12 NPSSortedListPQ public void add(E element) { pq.add(element); }
public void clear() { pq.clear(); public boolean isEmpty() { return pq.isEmpty(); public E peek() throws NPSQueueEmptyException { return pq.get(0); public E poll() throws NPSQueueEmptyException { return pq.remove(0); public int size() { return pq.size();

13 Comparator Example public static class reverseCompare<E extends Comparable<E>> implements Comparator<E> { public int compare(E o1, E o2) { return o2.compareTo(o1); } public static void main(String [] args) { NPSPriorityQueue<Integer> myPQ2 = new NPSSortedListPQ<Integer>(25, new reverseCompare<Integer>()); myPQ2.add(23); myPQ2.add(46); myPQ2.add(1); System.out.println("Emptying reversed priority queue..."); while(myPQ2.size() > 0) { System.out.println(myPQ2.poll());


Download ppt "Priority Queues."

Similar presentations


Ads by Google