Heaps ♢ COMP2521 ♢ (19T3)

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [0/15]
❖ Heaps

Heaps can be viewed as trees with top-to-bottom ordering
(cf. binary search trees which have left-to-right ordering)

[Diagram:Pics/heaps/heap-bst.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [1/15]
❖ ... Heaps

Heaps are typically used for implementing Priority Queues

Since heaps are dense trees, depth = floor(log2N)+1

Insertion cost = O(logN),   Deletion cost = O(logN)

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [2/15]
❖ ... Heaps

Heaps grow as follows (level-order):

[Diagram:Pics/heaps/heap-grow.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [3/15]
❖ ... Heaps

BSTs are typically implemented as linked data structures.

Heaps are often implemented via arrays, provided we know/can guess size.

Simple index calculations allow navigation through the tree:

[Diagram:Pics/heaps/heap-array.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [4/15]
❖ ... Heaps

Heap data structure:

typedef struct HeapRep {
   Item *items;  // array of Items
   int  nitems;  // #items in array
   int  nslots;  // #elements in array
} HeapRep;

typedef HeapRep *Heap;

Initialisation is similar to that for simple Hash Tables.

One difference: we use indexes from 1..nitems

Note: unlike "normal" C arrays, nitems also gives index of last item

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [5/15]
❖ ... Heaps

Creating new 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
}

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [6/15]
❖ Insertion with Heaps

Insertion is a two-step process

[Diagram:Pics/heaps/heap-fixup.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [7/15]
❖ ... Insertion with Heaps

Insertion into heap:

void insert(Heap h, Item it)
{
   assert(h->nitems < h->nslots);
   h->nitems++;
   h->items[h->nitems] = it;
   fixUp(h->items, h->nitems);
}

Always start new item at next available position on bottom level
(corresponds to next free element in the array (i.e. items[nitems])

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [8/15]
❖ Heaps

Heaps can be viewed as trees with top-to-bottom ordering.

Heaps are typically implemented via arrays.

Simple index calculations allow navigation through the tree:

[Diagram:Pics/heaps/heap-array.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [9/15]
❖ Insertion with Heaps

Insertion is a two-step process

void insert(Heap h, Item it)
{
   assert(h->nitems < h->nslots);
   h->nitems++;
   h->items[h->nitems] = it;
   fixUp(h->items, h->nitems);
}

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [10/15]
❖ ... Insertion with Heaps

Bottom-up heapify:

// 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;
}

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [11/15]
❖ Exercise: Heap Construction

Show the construction of the heap produced by inserting:

S O R T I N G I S F U N

The first four steps:

[Diagram:Pics/heaps/heap-construct.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [12/15]
❖ Deletion with Heaps

Deletion is a three-step process:

[Diagram:Pics/heaps/heap-fixdown.png]

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [13/15]
❖ ... Deletion with Heaps

Deletion from heap (always remove root):

Item delete(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;
}

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [14/15]
❖ ... Deletion with Heaps

Top-down heapify:

// 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;
   }
}

Heaps ♢ COMP2521 (19T3) ♢ Week 06a ♢ [15/15]


Produced: 23 Oct 2019