|
| 1 | +""" |
| 2 | +See max_heap.py for more detailed comments |
| 3 | +""" |
| 4 | + |
| 5 | +class MinHeap: |
| 6 | + |
| 7 | + |
| 8 | + def __init__(self, maxsize): |
| 9 | + self.maxsize = maxsize |
| 10 | + self.size = 0 |
| 11 | + self.first = 0 |
| 12 | + self.heap = [0] * self.maxsize |
| 13 | + |
| 14 | + |
| 15 | + def is_empty(self): |
| 16 | + return self.size == 0 |
| 17 | + |
| 18 | + |
| 19 | + def is_leaf(self, pos): |
| 20 | + if self.mid_index() <= pos <= self.last_index(): |
| 21 | + return True |
| 22 | + return False |
| 23 | + |
| 24 | + |
| 25 | + def parent(self, pos): |
| 26 | + return pos // 2 |
| 27 | + |
| 28 | + |
| 29 | + def left_child(self, pos): |
| 30 | + return 2*pos + 1 |
| 31 | + |
| 32 | + |
| 33 | + def right_child(self, pos): |
| 34 | + return 2*pos + 2 |
| 35 | + |
| 36 | + |
| 37 | + def last_index(self): |
| 38 | + return self.size - 1 |
| 39 | + |
| 40 | + |
| 41 | + def mid_index(self): |
| 42 | + return self.size // 2 |
| 43 | + |
| 44 | + |
| 45 | + def swap(self, x, y): |
| 46 | + self.heap[x], self.heap[y] = self.heap[y], self.heap[x] |
| 47 | + |
| 48 | + |
| 49 | + def pop_min(self): |
| 50 | + min_element = self.heap[self.first] |
| 51 | + self.heap[self.first] = self.heap[self.last_index()] |
| 52 | + self.heap[self.last_index()] = 0 |
| 53 | + self.size -= 1 |
| 54 | + self.min_heapify(self.first) |
| 55 | + return min_element |
| 56 | + |
| 57 | + |
| 58 | + def min_heapify(self, pos): |
| 59 | + if not self.is_leaf(pos): |
| 60 | + left = self.heap[self.left_child(pos)] |
| 61 | + right = self.heap[self.right_child(pos)] |
| 62 | + curr = self.heap[pos] |
| 63 | + |
| 64 | + if curr > left or curr > right: |
| 65 | + |
| 66 | + if left > right: |
| 67 | + self.swap(pos, self.left_child(pos)) |
| 68 | + self.min_heapify(self.left_child(pos)) |
| 69 | + else: |
| 70 | + self.swap(pos, self.right_child(pos)) |
| 71 | + self.min_heapify(self.right_child(pos)) |
| 72 | + |
| 73 | + |
| 74 | + def insert(self, element): |
| 75 | + if self.is_empty(): |
| 76 | + self.heap[self.first] = element |
| 77 | + self.size += 1 |
| 78 | + return |
| 79 | + |
| 80 | + if self.size >= self.maxsize: |
| 81 | + return |
| 82 | + |
| 83 | + self.size += 1 |
| 84 | + self.heap[self.last_index()] = element |
| 85 | + |
| 86 | + curr = self.last_index() |
| 87 | + |
| 88 | + while self.heap[curr] < self.heap[self.parent(curr)]: |
| 89 | + self.swap(curr, self.parent(curr)) |
| 90 | + curr = self.parent(curr) |
| 91 | + |
| 92 | + |
| 93 | + def print(self): |
| 94 | + for i in range(0, self.mid_index() + 1): |
| 95 | + parent = self.heap[i] |
| 96 | + left = self.heap[self.left_child(i)] |
| 97 | + right = self.heap[self.right_child(i)] |
| 98 | + |
| 99 | + print(f"Parent: {self.heap[i]}") |
| 100 | + |
| 101 | + if left: |
| 102 | + print(f"Left child: {left}") |
| 103 | + if right: |
| 104 | + print(f"Right child: {right}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + min_heap = MinHeap(15) |
| 109 | + min_heap.insert(5) |
| 110 | + min_heap.insert(3) |
| 111 | + min_heap.insert(17) |
| 112 | + min_heap.insert(10) |
| 113 | + min_heap.insert(84) |
| 114 | + min_heap.insert(19) |
| 115 | + min_heap.insert(6) |
| 116 | + min_heap.insert(22) |
| 117 | + min_heap.insert(9) |
| 118 | + |
| 119 | + min_heap.print() |
| 120 | + print(min_heap.heap) |
| 121 | + print('Min element is - ', min_heap.pop_min()) |
| 122 | + min_heap.print() |
0 commit comments