build max heap from an unsorted array >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) >>> h [50, 40, 20, 20, 10] >>> h = Heap() >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0]) >>> h [9, 8, 7, 4, 5, 6, 3, 2, 1, 0] >
(self, collection: Iterable[T])
| 132 | self.max_heapify(violation) |
| 133 | |
| 134 | def build_max_heap(self, collection: Iterable[T]) -> None: |
| 135 | """ |
| 136 | build max heap from an unsorted array |
| 137 | |
| 138 | >>> h = Heap() |
| 139 | >>> h.build_max_heap([20,40,50,20,10]) |
| 140 | >>> h |
| 141 | [50, 40, 20, 20, 10] |
| 142 | |
| 143 | >>> h = Heap() |
| 144 | >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0]) |
| 145 | >>> h |
| 146 | [9, 8, 7, 4, 5, 6, 3, 2, 1, 0] |
| 147 | |
| 148 | >>> h = Heap() |
| 149 | >>> h.build_max_heap([514,5,61,57,8,99,105]) |
| 150 | >>> h |
| 151 | [514, 57, 105, 5, 8, 99, 61] |
| 152 | |
| 153 | >>> h = Heap() |
| 154 | >>> h.build_max_heap([514,5,61.6,57,8,9.9,105]) |
| 155 | >>> h |
| 156 | [514, 57, 105, 5, 8, 9.9, 61.6] |
| 157 | """ |
| 158 | self.h = list(collection) |
| 159 | self.heap_size = len(self.h) |
| 160 | if self.heap_size > 1: |
| 161 | # max_heapify from right to left but exclude leaves (last level) |
| 162 | for i in range(self.heap_size // 2 - 1, -1, -1): |
| 163 | self.max_heapify(i) |
| 164 | |
| 165 | def extract_max(self) -> T: |
| 166 | """ |
no test coverage detected