return to all data-structures

return to concepts

The Heap Data Structure

Overview

When to use heaps

Heapification

Heapification is when a heap is rearranged to maintain the heap property1. In the context of a max heap, this means that each node is greater than all of its children.

The HeapSort Algorithm

This algorithm utilizes a binary head, or a heap whose nodes each have two children nodes (with the possible exception of the penultimate level). The steps2 are as follows:

  1. Build a max heap from the input data.
  2. Replace the root of the heap (which happens to contain the largest item) with the last item of the heap.
  3. Reduce the size of the heap by 1, then heapify the root of the tree.
  4. Repeat steps 1-3 as long as the size of the heap is >1.

Once the algorithm breaks, the input data will be sorted!

Time Complexity

Operation Time Complexity
Heapify O(log(n))
Insert O(log(n))
Delete O(log(n))
Building a Heap Simple Bound: O(n log(n))
Building a Heap Tight Bound3 : O(n)

Heap Review

Check out GeeksforGeek’s Heap Quiz to review heap-related properties and concepts.

References

  1. NIST: Heapify
  2. GeeksforGeeks: HeapSort
  3. GeeksforGeeks: Time Complexity of Building a Heap

return to all data-structures

return to concepts