Complexity Analysis

Building the heap

As mentioned in the previous lesson, the time complexity of building a heap is O(N)O(N). This is not obvious right away so let’s analyse the algorithm.

The entire building of the heap can be expressed as NN heapify events. Each heapify event take O(h)O(h) time where hh is the height of the tree.

When I say heapify in this lesson, I mean the top to bottom heapify, the same can be proved for the bottom to up heapify as well.

Each heapify event can be provided with a tighter upper bound than hh. For example, for leaf nodes, the heapify event is just 11 operation since there is no node below to propagate the heapify to.


Number of operations

Step by step:

  • 1st1st element (root): hh levels below it, so hh operations
  • For the next two elements 2nd2nd to 3rd3rd: h1h-1 operations.
  • For the next four elements 4th4th to 7th7th: h2h-2 operations.
  • And so on…
  • Second last level: 2 operations.
  • Leaf nodes (last level): 1 operation.

For the sake of simplicity, let’s assume NN is a power of 22 so that we work with a complete binary heap.

Also, recall that the count of nodes at any level can be expressed in terms of NN. Nodes halves every level as two nodes connects to one node each time to go up a level.

  • Nodes in the last level (leafs) = N2\frac{N}{2}. Let’s call this level 00.

  • Nodes in the second last level i.e. level 11 = N4\frac{N}{4}.

  • Nodes in level 2 = N8\frac{N}{8} ...