I want to calculate the tighter bound on heapify using siftdown approach so I proceeded as follows :
At each level i, each key on that level can go to leaf level h (where h is height of tree) in the worst case.
As there are 2i nodes at level i, so total work done is
∑0≤i≤h (h – i ) * 2i
But I couldn’t proceed further. I know it has to come O(n) but I couldn’t reach it. Please help me solving this.
S = ∑0≤i≤h (h – i ) * 2i
S = h + 2(h – 1) + 4(h – 2) + … + 2h – 1 … (1)
Multiply both sides by 2,
2S = 2h + 4(h – 1) + 8(h – 2) + … + 2h … (2)
Subtract from (1) from (2),
S = h -2h + 2 + 4 + 8 + …. + 2h
= -h – 1 + (1 + 2 + 4 + 8 +… + 2h )
= (2h + 1 – 1) – (h + 1)
[Note: 1 + 2 + 4 + 8 + … + 2h = (2h + 1 – 1)
Since a complete binary tree of height h has between 2h and 2h + 1 nodes, the above sum is O(n) where n is the number of nodes in the heap.