|
| 1 | +""" Implementation of Min Heap Data Structure """ |
| 2 | + |
| 3 | + |
| 4 | +class MinHeap: |
| 5 | + """ MinHeap is a complete binary tree. |
| 6 | + In MinHeap, the key at root must be minimum among all keys |
| 7 | + present in Binary Heap. The same property must be recursively true for |
| 8 | + all nodes in Binary Tree. |
| 9 | + """ |
| 10 | + |
| 11 | + def __init__(self): |
| 12 | + """ initialize new empty 1-indexed heap """ |
| 13 | + self.data = [None] |
| 14 | + |
| 15 | + def length(self): |
| 16 | + """ returns total number of items present in heap """ |
| 17 | + return len(self.data) - 1 |
| 18 | + |
| 19 | + def is_empty(self): |
| 20 | + """ checks if heap is empty or not """ |
| 21 | + return self.length() == 0 |
| 22 | + |
| 23 | + def insert(self, item): |
| 24 | + """ inserts new item into heap while maintaining heap invariant """ |
| 25 | + self.data.append(item) |
| 26 | + self.swim(self.length()) |
| 27 | + |
| 28 | + def get_min(self): |
| 29 | + """ get_min returns the minimum value (i.e. root) in the heap, |
| 30 | + without removing it. Returns None, if the heap is empty. |
| 31 | + """ |
| 32 | + if self.is_empty(): |
| 33 | + return None |
| 34 | + return self.data[1] |
| 35 | + |
| 36 | + def extract_min(self): |
| 37 | + """ extract_min returns the minimum value (i.e. root) in the heap |
| 38 | + and removes it from the heap. Returns None, if heap is empty. |
| 39 | + """ |
| 40 | + if self.is_empty(): |
| 41 | + return None |
| 42 | + |
| 43 | + min_value = self.data[1] |
| 44 | + self.data[1] = self.data[self.length()] |
| 45 | + self.data = self.data[:self.length()] |
| 46 | + self.sink(1) |
| 47 | + return min_value |
| 48 | + |
| 49 | + def swim(self, index): |
| 50 | + """ swim moves element at index upward to maintain heap invariant """ |
| 51 | + if index <= 1: |
| 52 | + return |
| 53 | + |
| 54 | + parent = index // 2 |
| 55 | + if self.data[parent] > self.data[index]: |
| 56 | + self.data[parent], self.data[index] = self.data[index], self.data[parent] |
| 57 | + self.swim(parent) |
| 58 | + |
| 59 | + def sink(self, index): |
| 60 | + """ sink moves element at index downward to maintain heap invariant """ |
| 61 | + min_index = index |
| 62 | + left = index * 2 |
| 63 | + right = (index * 2) + 1 |
| 64 | + |
| 65 | + if left <= self.length(): |
| 66 | + if self.data[min_index] > self.data[left]: |
| 67 | + min_index = left |
| 68 | + |
| 69 | + if right <= self.length(): |
| 70 | + if self.data[min_index] > self.data[right]: |
| 71 | + min_index = right |
| 72 | + |
| 73 | + if min_index != index: |
| 74 | + self.data[index], self.data[min_index] = self.data[min_index], self.data[index] |
| 75 | + self.sink(min_index) |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + """ operational function """ |
| 80 | + mheap = MinHeap() |
| 81 | + mheap.insert(5) |
| 82 | + mheap.insert(3) |
| 83 | + print(mheap.get_min()) |
| 84 | + mheap.insert(1) |
| 85 | + mheap.insert(6) |
| 86 | + mheap.insert(2) |
| 87 | + print(mheap.data) |
| 88 | + print(mheap.extract_min()) |
| 89 | + print(mheap.data) |
| 90 | + print(mheap.extract_min()) |
| 91 | + mheap.insert(3) |
| 92 | + print(mheap.extract_min()) |
| 93 | + print(mheap.extract_min()) |
| 94 | + print(mheap.extract_min()) |
| 95 | + print(mheap.data) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments