insert a new value into the max heap >>> h = Heap() >>> h.insert(10) >>> h [10] >>> h = Heap() >>> h.insert(10) >>> h.insert(10) >>> h [10, 10] >>> h = Heap() >>> h.insert(10) >>> h.insert(10.
(self, value: T)
| 194 | raise Exception("Empty heap") |
| 195 | |
| 196 | def insert(self, value: T) -> None: |
| 197 | """ |
| 198 | insert a new value into the max heap |
| 199 | |
| 200 | >>> h = Heap() |
| 201 | >>> h.insert(10) |
| 202 | >>> h |
| 203 | [10] |
| 204 | |
| 205 | >>> h = Heap() |
| 206 | >>> h.insert(10) |
| 207 | >>> h.insert(10) |
| 208 | >>> h |
| 209 | [10, 10] |
| 210 | |
| 211 | >>> h = Heap() |
| 212 | >>> h.insert(10) |
| 213 | >>> h.insert(10.1) |
| 214 | >>> h |
| 215 | [10.1, 10] |
| 216 | |
| 217 | >>> h = Heap() |
| 218 | >>> h.insert(0.1) |
| 219 | >>> h.insert(0) |
| 220 | >>> h.insert(9) |
| 221 | >>> h.insert(5) |
| 222 | >>> h |
| 223 | [9, 5, 0.1, 0] |
| 224 | """ |
| 225 | self.h.append(value) |
| 226 | idx = (self.heap_size - 1) // 2 |
| 227 | self.heap_size += 1 |
| 228 | while idx >= 0: |
| 229 | self.max_heapify(idx) |
| 230 | idx = (idx - 1) // 2 |
| 231 | |
| 232 | def heap_sort(self) -> None: |
| 233 | size = self.heap_size |
no test coverage detected