Several highly voted answers have given excellent explanations. I will show several figures to show the proof clearly and hope it is easy to understand.
The height of heap h = O(log(N)), where N is the number of (internal) nodes.
Note: ◯ for internal nodes, and □ for external nodes or leaf nodes; values are only saved at internal nodes.
depth #node
0, 1 ---- ◯ (Root) --------
/ \
1, 2 ________◯ ◯ ________________
... ... ... ...
/ \ / \
h-2, 2^{h-2} _______ ◯ ◯ ◯ ◯ ____________
______ / \ / \ / \ / \ ____________
h-1, 1 ________ ◯ □ □ □ □ □ □ □ __________
/ \
___________ □ □ ______________________________
We can prove that h = O(log(N)) as below.
- Let
hbe the height of a heap storingninternal nodes. - For level
i = 0, 1, ..., h-2, there are$2^i$ nodes. - For level
i=h-1, there are at least 1 node. - Thus, we have the following equation
Let us prove that the total time complexity is O(n) to heapify a list of n numbers.
- At the bottom level
h-1, there is at least one node. No need to adjust it. - At the bottom level
h-2, there are$2^{h-2}$ nodes, but each node might movedownby 1 level. - At third level from bottom, there are
$2^{h-3}$ nodes, but each node might movedownby 2 levels. - So on and so forth, at level
i, there are$2^{i}$ nodes, but each node might move down byh-1-ilevels. - Therefore, time complexity
Tis
Then multiply Eq (2) with 2, to get
Let Eq (3) - Eq (2) to get:
Left hand side is 2T -T = T = Right hand side. Therefore, we have
To further relax the right hand side, we have
Therefore, Eq (5) proves