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 (sz / 2) a ⋯
Instances For
Inner loop for mkHeap.
Equations
- Batteries.BinaryHeap.mkHeap.loop lt 0 x x_3 = x
- Batteries.BinaryHeap.mkHeap.loop lt i.succ x h = Batteries.BinaryHeap.mkHeap.loop lt i (Batteries.BinaryHeap.heapifyDown lt x ⟨i, ⋯⟩) ⋯
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
Instances For
O(1). Build a new empty heap.
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.
Instances For
O(1). Get the number of elements in a BinaryHeap.
Instances For
O(1). Get data vector of a BinaryHeap.
Instances For
O(log n). Insert an element into a BinaryHeap, preserving the max-heap property.
Equations
Instances For
O(1). Get the maximum element in a BinaryHeap.
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 (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.vector.set (↑i) x ⋯) i).toArray }
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.vector.set (↑i) x ⋯) i).toArray }
Instances For
O(n). Convert an unsorted vector to a BinaryHeap.
Equations
- Batteries.Vector.toBinaryHeap lt v = { arr := (Batteries.BinaryHeap.mkHeap lt v).toArray }
Instances For
O(n). Convert an unsorted array to a BinaryHeap.
Equations
- Array.toBinaryHeap lt a = { arr := (Batteries.BinaryHeap.mkHeap lt { toArray := a, size_toArray := ⋯ }).toArray }
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 => have this := ⋯; Array.heapSort.loop lt a.popMax (out.push x)