Académique Documents
Professionnel Documents
Culture Documents
Priority queue
PriorityQueue():
amathematicalmodel
foracertainclassof
datastructuresthat
havesimilarbehavior
a constructor
Onlyoperationsdefined
void add(Comparable o): inserts o into the priority queue
Comparable removeLeast(): removes and returns the least
element
Comparable getLeast(): returns (but does not remove) the
least element
boolean isEmpty(): returns true iff empty
int size(): returns the number of elements
void clear(): discards all elements
Evaluating implementations
Array implementations
Heap implementation
For a priority queue, we will define a node to have the heap property if it
is as least as small as its children (since we are using smaller numbers to
represent higher priorities)
12
8
12
12
18
0
6
14
8
3
lastIndex = 5
5
10
11
12
3 12 6 18 14 8
Unless i == 0
9
To add an element:
To remove an element:
Comments
Java 5 java.util.PriorityQueue
void clear()
int size()
12
The End
13