Author: Hayden Smith 2021
Why?
What?
Heaps can be conceptualised as dense tree structures where:
BSTs are typically implemented as linked data structures.
Heaps are often implemented via arrays (assumes we know max size)
Simple index calculations allow navigation through the tree:
typedef struct HeapRep {
Item *items; // array of Items
int nitems; // #items in array
int nslots; // #elements in array
} HeapRep;
typedef HeapRep *Heap;
Heap newHeap(int N)
{
Heap new = malloc(sizeof(HeapRep));
Item *a = malloc((N+1)*sizeof(Item));
assert(new != NULL && a != NULL);
new->items = a; // no initialisation needed
new->nitems = 0; // counter and index
new->nslots = N; // index range 1..N
return new;
}
Insertion is a two-step process
void HeapInsert(Heap h, Item it) {
// is there space in the array?
assert(h->nitems < h->nslots);
h->nitems++;
// add new item at end of array
h->items[h->nitems] = it;
// move new item to its correct place
fixUp(h->items, h->nitems);
}
// force value at a[i] into correct position
void fixUp(Item a[], int i) {
while (i > 1 && less(a[i/2], a[i])) {
swap(a, i, i/2);
i = i / 2; // integer division
}
}
void swap(Item a[], int i, int j) {
Item tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}Deletion is a three-step process:
Item HeapDelete(Heap h) {
Item top = h->items[1];
// overwrite first by last
h->items[1] = h->items[h->nitems];
h->nitems--;
// move new root to correct position
fixDown(h->items, 1, h->nitems);
return top;
}
// force value at a[i] into correct position
// note that N gives max index *and* # items
void fixDown(Item a[], int i, int N) {
while (2 * i <= N) {
// compute address of left child
int j = 2 * i;
// choose larger of two children
if (j < N && less(a[j], a[j+1])) j++;
if (!less(a[i], a[j])) break;
swap(a, i, j);
// move one level down the heap
i = j;
}
}
Heap behaviour is exactly behaviour required for Priority Queue :
typedef Heap PQueue;
void join(PQueue pq, Item it) { HeapInsert(pq,it); }
Item leave(PQueue pq) { return HeapDelete(pq); }Implementation Comparison
We can use a heap for sorting very easily:
Cost of heapsort = O(NlogN)