What would be expect to be able to do with a heap-tree? We would like to be able to remove the top-priority node (that is, `serve' the next customer) and we would like to insert new nodes (or customers) with a given priority. Assuming that priorities are given by integers, we need operations
boolean heapempty(); int root(); delete_root(); insert(int v);
In order to develop algorithms using our array representation, we keep the largest position that has been filled so far, which is the same as the current number of nodes of the heap-tree. The first two are very easy:
int MAX = 100; // Maximum number of nodes allowed.
int heap[MAX]; // Stores priority values of the nodes of the heap-tree.
int n = 0; // Largest position that has been filled so far.
boolean heapempty() {
return truth value of "n = 0";
}
int root() {
if (heapempty())
raise an exception;
else
return heap[1];
}