Why is heapify O n

Read the entire context:

The children's subtrees each have size at most 2n/3 - the worst case occurs when the last row of the tree is exactly half full

Since the running time is analysed by the number of elements in the tree (), and the recursion steps into one of the subtrees, we need to find an upper bound on the number of nodes in a subtree, relative to , and that will yield that

The worst case of number of nodes in a subtree is when the final row is as full as possible on one side, and as empty as possible on the other. This is called half full. And the left subtree size will be bounded by .

If you're proposing a case with only a few nodes, then that's irrelevant, since all base cases can be considered and ignored.

answered Jul 28 '11 at 13:29


37k77 gold badges6969 silver badges7676 bronze badges