(val, index, heap, position)
| 34 | |
| 35 | # Update function if value of any node in min-heap decreases |
| 36 | def bottomToTop(val, index, heap, position): |
| 37 | temp = position[index] |
| 38 | |
| 39 | while(index != 0): |
| 40 | if index % 2 == 0: |
| 41 | parent = int( (index-2) / 2 ) |
| 42 | else: |
| 43 | parent = int( (index-1) / 2 ) |
| 44 | |
| 45 | if val < heap[parent]: |
| 46 | heap[index] = heap[parent] |
| 47 | position[index] = position[parent] |
| 48 | setPosition(position[parent], index) |
| 49 | else: |
| 50 | heap[index] = val |
| 51 | position[index] = temp |
| 52 | setPosition(temp, index) |
| 53 | break |
| 54 | index = parent |
| 55 | else: |
| 56 | heap[0] = val |
| 57 | position[0] = temp |
| 58 | setPosition(temp, 0) |
| 59 | |
| 60 | def heapify(heap, positions): |
| 61 | start = len(heap) // 2 - 1 |
no test coverage detected