MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __swap_down

Method __swap_down

data_structures/heap/max_heap.py:38–51  ·  view source on GitHub ↗

Swap the element down

(self, i: int)

Source from the content-addressed store, hash-verified

36 self.__swap_up(self.__size)
37
38 def __swap_down(self, i: int) -> None:
39 """Swap the element down"""
40 while self.__size >= 2 * i:
41 if 2 * i + 1 > self.__size: # noqa: SIM114
42 bigger_child = 2 * i
43 elif self.__heap[2 * i] > self.__heap[2 * i + 1]:
44 bigger_child = 2 * i
45 else:
46 bigger_child = 2 * i + 1
47 temporary = self.__heap[i]
48 if self.__heap[i] < self.__heap[bigger_child]:
49 self.__heap[i] = self.__heap[bigger_child]
50 self.__heap[bigger_child] = temporary
51 i = bigger_child
52
53 def pop(self) -> int:
54 """Pop the root element"""

Callers 1

popMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected