The heap property (for a max heap) is that each node should be greater than or equal to its parent. The parent of element i
in a binary heap stored in an array is element (i - 1) // 2
:
def is_heap(A):
return all(A[i] >= A[(i - 1) // 2] for i in range(1, len(A)))
Obviously because the heap is stored in array we don't need to check the shape.