1

How can I go about proving that maximum item in a min-heap must be at one of the leaves, in a tree with N items?

I understand the overall design of a min-heap, and I can show/diagram that the maximum item is at one of the leaves (node at depth N + 1 > node at depth N). I'm just unsure as to how I should format the proof.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    I find the easiest way is usually: Suppose it was not the case. I.e. suppose the maximum item was not at a leaf. Does this lead to a contradiction? – j_random_hacker Oct 24 '13 at 03:01
  • Also, for simplicity, start with the slightly easier problem in which we assume that there is a unique maximum item. – j_random_hacker Oct 24 '13 at 03:02
  • The first step would be formulating exactly what you mean to prove, because your English description could be interpreted in several different ways, some of which are true (and provable) and some of which are false (and not provable). – Chris Okasaki Oct 24 '13 at 18:37

2 Answers2

1

For start, please note that "heap" property requires that subtrees rooted at non-leaf nodes should also maintain the heap property because they are also heaps. For min-heap, the "heap" property is that root value should be less than values at the children.

If any non-leaf node of a min-heap holds the maximum item, then the heap property of the subtree whose root is the current non-leaf node is violated because children values of this subtree is less than the root value. To maintain the heap property, the root value has to be floated down to one of the children.

In order to not violate the "heap" property, the floating down of the "maximum" item will continue until the node holding the maximum value has no more children. Thus, the "maximum" item will always be at a node that has no children (i.e. leaf node).

user2784234
  • 451
  • 3
  • 9
1

To state the answer simply, assume for the sake of contradiction the max value is in a non-leaf. This violates the heap property, which for min-heaps requires that a node is smaller than its children in value. Therefore, the max value must be in a leaf.

qwr
  • 9,525
  • 5
  • 58
  • 102