in heap.py [0:0]
def push_invalid(self, pos):
root = pos
if pos not in self.invalid_pos:
return
if self.has_child(root):
# Push an invalid node towards the bottom of the heap
left_child = self.left_child(root)
right_child = self.right_child(root)
if left_child in self.invalid_pos:
self.push_invalid(left_child)
if right_child < len(self.data):
if right_child in self.invalid_pos:
self.push_invalid(right_child)
if right_child not in self.invalid_pos and left_child not in self.invalid_pos:
# Both are valid
exchange_with = left_child if self.cmp(self.data[left_child], self.data[right_child]) else right_child
elif left_child not in self.invalid_pos:
# Left is the only valid
exchange_with = left_child
else:
# Either both are invalid or right is the only valid
exchange_with = right_child
else:
exchange_with = left_child
if exchange_with not in self.invalid_pos:
self._swap_pos(root, exchange_with)
self.push_invalid(exchange_with)
self.clean_invalid()