File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313heappush(heap, item) # pushes a new item on the heap
1414item = heappop(heap) # pops the smallest item from the heap
1515item = heap[0] # smallest item on the heap without popping it
16- heapify(heap ) # transform list into a heap, in-place, in linear time
16+ heapify(x ) # transforms list into a heap, in-place, in linear time
1717
1818Our API differs from textbook heap algorithms as follows:
1919
@@ -175,16 +175,16 @@ def heappop(heap):
175175 returnitem = lastelt
176176 return returnitem
177177
178- def heapify (heap ):
179- """Transform list heap into a heap, in-place, in O(len(heap)) time."""
180- n = len (heap )
178+ def heapify (x ):
179+ """Transform list into a heap, in-place, in O(len(heap)) time."""
180+ n = len (x )
181181 # Transform bottom-up. The largest index there's any point to looking at
182182 # is the largest with a child index in-range, so must have 2*i + 1 < n,
183183 # or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so
184184 # j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is
185185 # (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1.
186186 for i in xrange (n // 2 - 1 , - 1 , - 1 ):
187- _siftdown (heap , i )
187+ _siftdown (x , i )
188188
189189if __name__ == "__main__" :
190190 # Simple sanity test
Original file line number Diff line number Diff line change @@ -12,6 +12,20 @@ def check_invariant(heap):
1212 parentpos = (pos - 1 ) >> 1
1313 verify (heap [parentpos ] <= item )
1414
15+ # An iterator returning a heap's elements, smallest-first.
16+ class heapiter (object ):
17+ def __init__ (self , heap ):
18+ self .heap = heap
19+
20+ def next (self ):
21+ try :
22+ return heappop (self .heap )
23+ except IndexError :
24+ raise StopIteration
25+
26+ def __iter__ (self ):
27+ return self
28+
1529def test_main ():
1630 # 1) Push 100 random numbers and pop them off, verifying all's OK.
1731 heap = []
@@ -47,17 +61,16 @@ def test_main():
4761 check_invariant (heap )
4862 # 5) Less-naive "N-best" algorithm, much faster (if len(data) is big
4963 # enough <wink>) than sorting all of data. However, if we had a max
50- # heap instead of a min heap, it would go much faster still via
64+ # heap instead of a min heap, it could go faster still via
5165 # heapify'ing all of data (linear time), then doing 10 heappops
5266 # (10 log-time steps).
5367 heap = data [:10 ]
5468 heapify (heap )
5569 for item in data [10 :]:
56- if item > heap [0 ]: # this gets rarer and rarer the longer we run
70+ if item > heap [0 ]: # this gets rarer the longer we run
71+ heappop (heap ) # we know heap[0] isn't in best 10 anymore
5772 heappush (heap , item )
58- heappop (heap )
59- heap .sort ()
60- vereq (heap , data_sorted [- 10 :])
73+ vereq (list (heapiter (heap )), data_sorted [- 10 :])
6174 # Make user happy
6275 if verbose :
6376 print "All OK"
You can’t perform that action at this time.
0 commit comments