The following functions give the basic interface for a Priority Queue.
A simple Priority Queue could be implemented using a linked list sorted in decreasing order.
This method actually works fairly well. The main problem is that the insertion has O(n) run time. It is possible to be more efficient.
A better implementation uses a heap. A heap is actually implemented as an array with some specialized operations, but can be thought of as a complete binary tree, with a special property satisfied at each node. This property says that a node's value is greater than or equal to its parent. A complete binary tree means that each level is full except perhaps the last, which is simply filled left to right as needed. A goal of any operation is to maintain the heap property and the completeness of the (conceptual) binary tree.
You will recall that we went over the heap operations in lecture. Check your remembrances now and be sure you understand why:
The good news for you is that java.util.PriorityQueue has already been programmed for you and implements a priority queue based on a heap. So you don't need to write the code. But you do need to ensure that anything you store in a heap must implement the Comparable interface.
In this file you will find a list of 100 World cities and their popuations. In order to make it possible to enter the cities into a PriorityQueue you will need to create a Comparable class that can store the data.
Make your own or you can fill out my shell.
The Heap implementation achieves O(log n) insertions and deletions, which is better than what the sorted list could offer. On this basis it is clear that you can build the heap in time O(n log n). (As a matter of fact, the heap can be built in time O(n). For example see wikipedia.) You can create a top ten list from a file of n cities in the same big Oh time it takes to build the heap.