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.
Core operation for binary heaps, expressed directly on arrays. Construct a heap from an unsorted array, by heapifying all the elements.
Equations
- BinaryHeap.mkHeap lt a = BinaryHeap.mkHeap.loop lt (Array.size a / 2) a (_ : Array.size a / 2 ≤ Array.size a)
Equations
- One or more equations did not get rendered due to their size.
- BinaryHeap.mkHeap.loop lt 0 x x_1 = { val := x, property := (_ : Array.size x = Array.size x) }
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.
O(1)
. Build a new empty heap.
Equations
- BinaryHeap.empty lt = { arr := #[] }
Equations
- BinaryHeap.instInhabitedBinaryHeap lt = { default := BinaryHeap.empty lt }
Equations
- BinaryHeap.instEmptyCollectionBinaryHeap lt = { emptyCollection := BinaryHeap.empty lt }
O(1)
. Build a one-element heap.
Equations
- BinaryHeap.singleton lt x = { arr := #[x] }
O(1)
. Get the number of elements in a BinaryHeap
.
Equations
- BinaryHeap.size self = Array.size self.arr
O(1)
. Get an element in the heap by index.
Equations
- BinaryHeap.get self i = Array.get self.arr i
O(log n)
. Insert an element into a BinaryHeap
, preserving the max-heap property.
Equations
- One or more equations did not get rendered due to their size.
O(1)
. Get the maximum element in a BinaryHeap
.
Equations
- BinaryHeap.max self = Array.get? self.arr 0
Auxiliary for popMax
.
Equations
- One or more equations did not get rendered due to their size.
O(log n)
. Remove the maximum element from a BinaryHeap
.
Call max
first to actually retrieve the maximum element.
Equations
- BinaryHeap.popMax self = ↑(BinaryHeap.popMaxAux self)
O(log n)
. Return and remove the maximum element from a BinaryHeap
.
Equations
- BinaryHeap.extractMax self = (BinaryHeap.max self, BinaryHeap.popMax self)
O(log n)
. Equivalent to extractMax (self.insert x)
, except that extraction cannot fail.
Equations
- One or more equations did not get rendered due to their size.
O(log n)
. Equivalent to (self.max, self.popMax.insert x)
.
Equations
- One or more equations did not get rendered due to their size.
O(log n)
. Replace the value at index i
by x
. Assumes that x ≤ self.get i≤ self.get i
.
Equations
- BinaryHeap.decreaseKey self i x = { arr := ↑(BinaryHeap.heapifyDown lt (Array.set self.arr i x) { val := ↑i, isLt := (_ : ↑i < Array.size (Array.set self.arr i x)) }) }
O(log n)
. Replace the value at index i
by x
. Assumes that self.get i ≤ x≤ x
.
Equations
- BinaryHeap.increaseKey self i x = { arr := ↑(BinaryHeap.heapifyUp lt (Array.set self.arr i x) { val := ↑i, isLt := (_ : ↑i < Array.size (Array.set self.arr i x)) }) }
O(n)
. Convert an unsorted array to a BinaryHeap
.
Equations
- Array.toBinaryHeap lt a = { arr := ↑(BinaryHeap.mkHeap lt a) }
O(n log n)
. Sort an array using a BinaryHeap
.
Equations
- Array.heapSort a lt = let gt := fun y x => lt x y; Array.heapSort.loop lt (Array.toBinaryHeap gt a) #[]
Equations
- One or more equations did not get rendered due to their size.