- Slides: 15
Priority Queue ADT l A priority queue stores a collection of entries l l Each entry is a pair (key, value) Main methods of the Priority Queue ADT l insert(k, x) l l remove. Min() l l inserts an entry with key k and value x removes and returns the entry with smallest key Additional methods l l min(): returns, but does not remove, an entry with smallest key size(), is. Empty()
Entry and Key l An entry in a priority queue is l l Priority queues store entries to allow l l simply a key-value pair for efficient insertion and removal based on keys A key is l An object used l to identify the priority of an entry of a priority queue
Comparator ADT l A comparator l encapsulates the action of comparing l l When the priority queue needs to compare two keys, l l two objects according to a given total order relation It can uses a user-supplied comparator The primary method of the Comparator ADT: l compare(a, b): l Returns an integer i such that § i < 0 if a < b, § i = 0 if a = b, § i > 0 if a > b;
Sorting with a Priority Queue Algorithm Priority. Queue. Sort(sequence S, priority Q P) while (!S. is. Empty( )) do e=S. remove. First( ); P. insert(e); while (!P. is. Empty( )) do e=P. remove( ); S. insert. Last(e); l Refer to Tree. Map. Sorting Project
Java’s Priority. Queue Class l Priority. Queue l l orders elements by their natural ordering. inserts elements in priority order such that l l Common Priority. Queue operations are l offer l l l to insert an element at the appropriate location based on priority order poll l to remove the highest-priority element of the priority queue peek l l the highest-priority element (i. e. , the largest value) § will be the first element removed from the Priority. Queue. to get a reference to the highest-priority element of the priority queue Refer to Priority. Queue. Sorting Project
Collection Interface and Collections Class
Collection interface and Collections class l Interface Collection is the root interface l l Interface Set defines a l l collection that does not contain duplicates. Interface Queue defines a l l from which interfaces Set, Queue and List are derived. collection that represents a waiting line. Class Collections provides static methods l that search, sort and perform other operations on collections.
Lists and Linked. List class l A List (sometimes called a sequence) l is a Collection that can contain duplicate elements. l is implemented by several classes, l l including Array. List, and Linked. List. A Linked. List enables l efficient insertion (or removal) of elements l in the middle of a collection.
Linked. List methods l add. All l l list. Iterator l l appends all elements of a collecton to the end of a List. gets A List’s bidirectional iterator. sub. List obtains a portion of a List. l This is a so-called range-view method, l l which enables the program to view a portion of the list. Refer to Linked. List. App Project
Arrays as Lists l Class Arrays provides static method as. List l l A List view allows you to manipulate l l the array as if it were a list. This is useful for adding the elements in an array l l to view an array as a List collection. to a collection and for sorting array elements. Refer to Using. To. Array Project
Collections Methods l Class Collections provides l several high-performance algorithms for l l manipulating collection elements. Refer to Using. Collections. Method Project
Sets l A Set is an unordered Collection l l of unique elements (i. e. , no duplicate elements). The collections framework contains several l Set implementations, including Hash. Set and Tree. Set. l Hash. Set stores its elements in a hash table, and l Tree. Set stores its elements in a tree.
Sets (Cont’d) l Tree. Set method l head. Set gets a subset of the Tree. Set in which every element l l tail. Set gets a subset in which each element l l is greater than or equal to the specified value. first and last l l is less than the specified value. get the smallest and largest elements of the set, respectively. Refer to Removing. Duplicates Project