File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -142,27 +142,25 @@ def heappop(heap):
142142 returnitem = heap [0 ]
143143 item = heap .pop ()
144144 pos = 0
145- while True :
146- child2pos = (pos + 1 ) * 2
147- child1pos = child2pos - 1
148- if child2pos < endpos :
149- child1 = heap [child1pos ]
150- child2 = heap [child2pos ]
151- if item <= child1 and item <= child2 :
152- break
153- if child1 < child2 :
154- heap [pos ] = child1
155- pos = child1pos
156- continue
157- heap [pos ] = child2
158- pos = child2pos
159- continue
160- if child1pos < endpos :
161- child1 = heap [child1pos ]
162- if child1 < item :
163- heap [pos ] = child1
164- pos = child1pos
165- break
145+ # Sift item into position, down from the root, moving the smaller
146+ # child up, until finding pos such that item <= pos's children.
147+ childpos = 2 * pos + 1 # leftmost child position
148+ while childpos < endpos :
149+ # Set childpos and child to reflect smaller child.
150+ child = heap [childpos ]
151+ rightpos = childpos + 1
152+ if rightpos < endpos :
153+ rightchild = heap [rightpos ]
154+ if rightchild < child :
155+ childpos = rightpos
156+ child = rightchild
157+ # If item is no larger than smaller child, we're done, else
158+ # move the smaller child up.
159+ if item <= child :
160+ break
161+ heap [pos ] = child
162+ pos = childpos
163+ childpos = 2 * pos + 1
166164 heap [pos ] = item
167165 return returnitem
168166
You can’t perform that action at this time.
0 commit comments