* binaryheap_remove_first * * Removes the first (root, topmost) node in the heap and returns a * pointer to it after rebalancing the heap. The caller must ensure * that this routine is not used on an empty heap. O(log n) worst * case. */
| 171 | * case. |
| 172 | */ |
| 173 | Datum |
| 174 | binaryheap_remove_first(binaryheap *heap) |
| 175 | { |
| 176 | Assert(!binaryheap_empty(heap) && heap->bh_has_heap_property); |
| 177 | |
| 178 | if (heap->bh_size == 1) |
| 179 | { |
| 180 | heap->bh_size--; |
| 181 | return heap->bh_nodes[0]; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Swap the root and last nodes, decrease the size of the heap (i.e. |
| 186 | * remove the former root node) and sift the new root node down to its |
| 187 | * correct position. |
| 188 | */ |
| 189 | swap_nodes(heap, 0, heap->bh_size - 1); |
| 190 | heap->bh_size--; |
| 191 | sift_down(heap, 0); |
| 192 | |
| 193 | return heap->bh_nodes[heap->bh_size]; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * binaryheap_replace_first |
no test coverage detected