get and remove max from heap >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) >>> h.extract_max() 50 >>> h = Heap() >>> h.build_max_heap([514,5,61,57,8,99,105]) >>> h.extract_max() 514 >>> h = Heap() >>>
(self)
| 163 | self.max_heapify(i) |
| 164 | |
| 165 | def extract_max(self) -> T: |
| 166 | """ |
| 167 | get and remove max from heap |
| 168 | |
| 169 | >>> h = Heap() |
| 170 | >>> h.build_max_heap([20,40,50,20,10]) |
| 171 | >>> h.extract_max() |
| 172 | 50 |
| 173 | |
| 174 | >>> h = Heap() |
| 175 | >>> h.build_max_heap([514,5,61,57,8,99,105]) |
| 176 | >>> h.extract_max() |
| 177 | 514 |
| 178 | |
| 179 | >>> h = Heap() |
| 180 | >>> h.build_max_heap([1,2,3,4,5,6,7,8,9,0]) |
| 181 | >>> h.extract_max() |
| 182 | 9 |
| 183 | """ |
| 184 | if self.heap_size >= 2: |
| 185 | me = self.h[0] |
| 186 | self.h[0] = self.h.pop(-1) |
| 187 | self.heap_size -= 1 |
| 188 | self.max_heapify(0) |
| 189 | return me |
| 190 | elif self.heap_size == 1: |
| 191 | self.heap_size -= 1 |
| 192 | return self.h.pop(-1) |
| 193 | else: |
| 194 | raise Exception("Empty heap") |
| 195 | |
| 196 | def insert(self, value: T) -> None: |
| 197 | """ |
no test coverage detected