问题
In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY,
"the worst case occurs when the bottom level of the tree is exactly half full"
I guess the reason is that in this case, Max-Heapify has to "float down" through the left subtree.
But the thing I couldn't get is "why half full" ?
Max-Heapify can also float down if left subtree has only one leaf. So why not consider this as the worst case ?
回答1:
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 T(n)
is analysed by the number of elements in the tree (n
), 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 n
, and that will yield that T(n) = T(max num. nodes in subtree) + O(1)
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 2n/3
.
If you're proposing a case with only a few nodes, then that's irrelevant, since all base cases can be considered O(1)
and ignored.
来源:https://stackoverflow.com/questions/6859514/worst-case-in-max-heapify-the-worst-case-occurs-when-the-bottom-level-of-the-t