Transform list into a heap, in-place, in O(len(x)) time.
(x)
| 169 | return item |
| 170 | |
| 171 | def heapify(x): |
| 172 | """Transform list into a heap, in-place, in O(len(x)) time.""" |
| 173 | n = len(x) |
| 174 | # Transform bottom-up. The largest index there's any point to looking at |
| 175 | # is the largest with a child index in-range, so must have 2*i + 1 < n, |
| 176 | # or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so |
| 177 | # j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is |
| 178 | # (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1. |
| 179 | for i in reversed(range(n//2)): |
| 180 | _siftup(x, i) |
| 181 | |
| 182 | def heappop_max(heap): |
| 183 | """Maxheap version of a heappop.""" |