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 lt a = Batteries.BinaryHeap.mkHeap.loop lt (a.size / 2) a ⋯
Instances For
Inner loop for mkHeap
.
Equations
- Batteries.BinaryHeap.mkHeap.loop lt 0 x x_1 = ⟨x, ⋯⟩
- Batteries.BinaryHeap.mkHeap.loop lt i.succ x x_1 = match Batteries.BinaryHeap.mkHeap.loop lt i (Batteries.BinaryHeap.heapifyDown lt x ⟨i, ⋯⟩).val ⋯ with | ⟨a₂, h₂⟩ => ⟨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.
Instances For
Equations
- Batteries.BinaryHeap.instInhabited lt = { default := Batteries.BinaryHeap.empty lt }
Equations
- Batteries.BinaryHeap.instEmptyCollection lt = { emptyCollection := Batteries.BinaryHeap.empty lt }
O(1)
. Build a one-element heap.
Equations
- Batteries.BinaryHeap.singleton lt x = { arr := #[x] }
Instances For
O(1)
. Get the number of elements in a BinaryHeap
.
Equations
- self.size = self.arr.size
Instances For
O(1)
. Get an element in the heap by index.
Equations
- self.get i = self.arr.get i
Instances For
O(log n)
. Insert an element into a BinaryHeap
, preserving the max-heap property.
Equations
- self.insert x = { arr := let n := self.size; (Batteries.BinaryHeap.heapifyUp lt (self.arr.push x) ⟨n, ⋯⟩).val }
Instances For
O(1)
. Get the maximum element in a BinaryHeap
.
Equations
- self.max = self.arr.get? 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
- self.popMax = self.popMaxAux.val
Instances For
O(log n)
. Return and remove the maximum element from a BinaryHeap
.
Equations
- self.extractMax = (self.max, self.popMax)
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
- self.decreaseKey i x = { arr := (Batteries.BinaryHeap.heapifyDown lt (self.arr.set i x) ⟨↑i, ⋯⟩).val }
Instances For
O(log n)
. Replace the value at index i
by x
. Assumes that self.get i ≤ x
.
Equations
- self.increaseKey i x = { arr := (Batteries.BinaryHeap.heapifyUp lt (self.arr.set i x) ⟨↑i, ⋯⟩).val }
Instances For
O(n)
. Convert an unsorted array to a BinaryHeap
.
Equations
- Array.toBinaryHeap lt a = { arr := (Batteries.BinaryHeap.mkHeap lt a).val }
Instances For
O(n log n)
. Sort an array using a BinaryHeap
.
Equations
- a.heapSort lt = Array.heapSort.loop lt (Array.toBinaryHeap (flip lt) a) #[]
Instances For
Inner loop for heapSort
.
Equations
- Array.heapSort.loop lt a out = match e : a.max with | none => out | some x => let_fun this := ⋯; Array.heapSort.loop lt a.popMax (out.push x)