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

Method bottom_to_top

graphs/minimum_spanning_tree_prims.py:42–61  ·  view source on GitHub ↗
(self, val, index, heap, position)

Source from the content-addressed store, hash-verified

40
41 # Update function if value of any node in min-heap decreases
42 def bottom_to_top(self, val, index, heap, position):
43 temp = position[index]
44
45 while index != 0:
46 parent = int((index - 2) / 2) if index % 2 == 0 else int((index - 1) / 2)
47
48 if val < heap[parent]:
49 heap[index] = heap[parent]
50 position[index] = position[parent]
51 self.set_position(position[parent], index)
52 else:
53 heap[index] = val
54 position[index] = temp
55 self.set_position(temp, index)
56 break
57 index = parent
58 else:
59 heap[0] = val
60 position[0] = temp
61 self.set_position(temp, 0)
62
63 def heapify(self, heap, positions):
64 start = len(heap) // 2 - 1

Callers 1

prisms_algorithmFunction · 0.95

Calls 1

set_positionMethod · 0.95

Tested by

no test coverage detected