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

Method _bubble_down

graphs/minimum_spanning_tree_prims2.py:148–173  ·  view source on GitHub ↗
(self, elem: T)

Source from the content-addressed store, hash-verified

146 return None
147
148 def _bubble_down(self, elem: T) -> None:
149 # Place a node at the proper position (downward movement) [to be used
150 # internally only]
151 curr_pos = self.position_map[elem]
152 _, weight = self.heap[curr_pos]
153 child_left_position = get_child_left_position(curr_pos)
154 child_right_position = get_child_right_position(curr_pos)
155 if child_left_position < self.elements and child_right_position < self.elements:
156 _, child_left_weight = self.heap[child_left_position]
157 _, child_right_weight = self.heap[child_right_position]
158 if child_right_weight < child_left_weight and child_right_weight < weight:
159 self._swap_nodes(child_right_position, curr_pos)
160 return self._bubble_down(elem)
161 if child_left_position < self.elements:
162 _, child_left_weight = self.heap[child_left_position]
163 if child_left_weight < weight:
164 self._swap_nodes(child_left_position, curr_pos)
165 return self._bubble_down(elem)
166 else:
167 return None
168 if child_right_position < self.elements:
169 _, child_right_weight = self.heap[child_right_position]
170 if child_right_weight < weight:
171 self._swap_nodes(child_right_position, curr_pos)
172 return self._bubble_down(elem)
173 return None
174
175 def _swap_nodes(self, node1_pos: int, node2_pos: int) -> None:
176 # Swap the nodes at the given positions

Callers 2

extract_minMethod · 0.95
update_keyMethod · 0.95

Calls 3

_swap_nodesMethod · 0.95
get_child_left_positionFunction · 0.85
get_child_right_positionFunction · 0.85

Tested by

no test coverage detected