A binary heap is a complete binary tree which satisfies the heap property. For a max-heap, this property is:
For every node đ, the value of đ is greater than or equal to the values of its children.
Formally, if đ´A is an array representation of the heap and đi is a node, then:
đ´[đ]âĽđ´[2đ+1] and đ´[đ]âĽđ´[2đ+2]
(assuming a 0-based array indexing).
To prove that the root of any subtree in a max-heap contains the largest value in that subtree, let's consider a node đ in the max-heap and the subtree rooted at đ.
According to the max-heap property, for the root node đ,
đ´[đ]âĽđ´[2đ+1] and đ´[đ]âĽđ´[2đ+2]
This means đ´[đ]Â is greater than or equal to its immediate children.
Since the subtree rooted at any child of đ is also a max-heap, the root of these subtrees will be greater than or equal to all their respective children. Thus, by recursive application of the heap property:
đ´[2đ+1]âĽall descendants of 2đ+1
đ´[2đ+2]âĽall descendants of 2đ+2
Thus, A[i]âĽall descendants of i
Thus, the root of any subtree in a max-heap contains the largest value occurring anywhere in that subtree.