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
- BinaryHeap.mkHeap lt a = BinaryHeap.mkHeap.loop lt (Array.size a / 2) a ⋯
Instances For
Equations
- One or more equations did not get rendered due to their size.
- BinaryHeap.mkHeap.loop lt 0 x x_1 = { val := x, property := ⋯ }
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.
Instances For
Equations
- BinaryHeap.instInhabitedBinaryHeap lt = { default := BinaryHeap.empty lt }
Equations
- BinaryHeap.instEmptyCollectionBinaryHeap lt = { emptyCollection := BinaryHeap.empty lt }
O(1). Get the number of elements in a BinaryHeap.
Equations
- BinaryHeap.size self = Array.size self.arr
Instances For
O(1). Get an element in the heap by index.
Equations
- BinaryHeap.get self i = Array.get self.arr i
Instances For
O(log n). Insert an element into a BinaryHeap, preserving the max-heap property.
Equations
- BinaryHeap.insert self x = { arr := let n := BinaryHeap.size self; (BinaryHeap.heapifyUp lt (Array.push self.arr x) { val := n, isLt := ⋯ }).val }
Instances For
O(1). Get the maximum element in a BinaryHeap.
Equations
- BinaryHeap.max self = Array.get? self.arr 0
Instances For
Auxiliary for popMax.
Equations
- One or more equations did not get rendered due to their size.
Instances For
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).val
Instances For
O(log n). Return and remove the maximum element from a BinaryHeap.
Equations
- BinaryHeap.extractMax self = (BinaryHeap.max self, BinaryHeap.popMax self)
Instances For
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.
Instances For
O(log n). Equivalent to (self.max, self.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 ≤ self.get i.
Equations
- BinaryHeap.decreaseKey self i x = { arr := (BinaryHeap.heapifyDown lt (Array.set self.arr i x) { val := ↑i, isLt := ⋯ }).val }
Instances For
O(log n). Replace the value at index i by x. Assumes that self.get i ≤ x.
Equations
- BinaryHeap.increaseKey self i x = { arr := (BinaryHeap.heapifyUp lt (Array.set self.arr i x) { val := ↑i, isLt := ⋯ }).val }
Instances For
O(n). Convert an unsorted array to a BinaryHeap.
Equations
- Array.toBinaryHeap lt a = { arr := (BinaryHeap.mkHeap lt a).val }
Instances For
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) #[]
Instances For
Equations
- Array.heapSort.loop lt a out = match e : BinaryHeap.max a with | none => out | some x => let_fun this := ⋯; Array.heapSort.loop lt (BinaryHeap.popMax a) (Array.push out x)