Core operation for binary heaps, expressed directly on arrays.
Given an array which is a max-heap, push item i down to restore the max-heap property.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Core operation for binary heaps, expressed directly on arrays. Construct a heap from an unsorted array, by heapifying all the elements.
Equations
- Batteries.BinaryHeap.mkHeap a = Batteries.BinaryHeap.mkHeap.loop✝ (sz / 2) a ⋯
Instances For
Core operation for binary heaps, expressed directly on arrays.
Given an array which is a max-heap, push item i up to restore the max-heap property.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.BinaryHeap.heapifyUp a ⟨0, isLt⟩ = a
Instances For
O(1). Build a new empty heap.
Instances For
Equations
Equations
- Batteries.BinaryHeap.instEmptyCollection = { emptyCollection := Batteries.BinaryHeap.empty }
Equations
- Batteries.BinaryHeap.instMembership = { mem := fun (h : Batteries.BinaryHeap α) (x : α) => x ∈ h.arr }
O(1). Build a one-element heap.
Instances For
O(log n). Insert an element into a BinaryHeap, preserving the max-heap property.
Equations
Instances For
O(log n). Remove the maximum element from a BinaryHeap.
Call max first to actually retrieve the maximum element.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(log n). Return and remove the maximum element from a BinaryHeap.
Instances For
O(log n). Equivalent to extractMax (heap.insert x), except that extraction cannot fail.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(log n). Equivalent to (heap.max, heap.popMax.insert x).
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(log n). Replace the value at index i by x. Assumes that x ≤ heap.get i.
Equations
- heap.decreaseKey i x = { arr := (Batteries.BinaryHeap.heapifyDown (heap.vector.set (↑i) x ⋯) i).toArray }
Instances For
O(log n). Replace the value at index i by x. Assumes that heap.get i ≤ x.
Equations
- heap.increaseKey i x = { arr := (Batteries.BinaryHeap.heapifyUp (heap.vector.set (↑i) x ⋯) i).toArray }
Instances For
O(n log n). Return the contents of heap as an array sorted in descending order.
Equations
- heap.toSortedArray = Batteries.BinaryHeap.toSortedArray.loop✝ heap (Array.emptyWithCapacity heap.size)
Instances For
O(n). Convert an unsorted vector to a BinaryHeap.
Equations
- v.toBinaryHeap = { arr := (Batteries.BinaryHeap.mkHeap v).toArray }
Instances For
O(n). Convert an unsorted array to a BinaryHeap.
Equations
- a.toBinaryHeap = { arr := (Batteries.BinaryHeap.mkHeap (Vector.mk a ⋯)).toArray }