Peeking in a heap in python
Peeking in a heap in python
Yes, you can make this assumption, because it is stated in the documentation:
Heaps are arrays for which
heap[k] <= heap[2*k+1]
andheap[k] <=
for all k, counting
heap[2*k+2]
elements from zero. For the sake of
comparison, non-existing elements are
considered to be infinite. The
interesting property of a heap is that
heap[0]
is always its smallest
element.
(And thats probably the reason there is no peek
function: there is no need for it.)
If youre using Python 2.4 or newer, you can also use heapq.nsmallest().