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] <= heap[2*k+2]for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is thatheap[0]is always its smallest element.
(And that's probably the reason there is no peek function: there is no need for it.)
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] <= heap[2*k+2]for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is thatheap[0]is always its smallest element.
(And that's probably the reason there is no peek function: there is no need for it.)
If you're using Python 2.4 or newer, you can also use heapq.nsmallest().
Videos
I was reading about priority queue in python and came across two ways to use them:
-
Heapq module
-
Priority Queue class
In the priority queue class we have 'put' method just like Java's 'offer'. And python has 'get' vs Java's 'poll' method. But i couldn't find any 'peek' method like the one in Java's priority queue. Is it really not implemented. If yes then why?