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
- Batteries.BinaryHeap.heapifyUp lt a ⟨0, isLt⟩ = a
- Batteries.BinaryHeap.heapifyUp lt a ⟨i'.succ, hi⟩ = if lt a[i' / 2] a[⟨i'.succ, hi⟩] = true then Batteries.BinaryHeap.heapifyUp lt (a.swap (↑⟨i'.succ, hi⟩) (i' / 2) ⋯ ⋯) ⟨i' / 2, ⋯⟩ else a
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 data vector of a BinaryHeap
.
Equations
- self.vector = { toArray := self.arr, size_toArray := ⋯ }
Instances For
O(1)
. Get an element in the heap by index.
Equations
- self.get i = self.arr[i]
Instances For
O(log n)
. Insert an element into a BinaryHeap
, preserving the max-heap property.
Equations
- self.insert x = { arr := (Batteries.BinaryHeap.heapifyUp lt (Vector.push x self.vector) ⟨self.size, ⋯⟩).toArray }
Instances For
O(1)
. Get the maximum element in a BinaryHeap
.
Equations
- self.max = self.arr[0]?
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
.
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.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 => let_fun this := ⋯; Array.heapSort.loop lt a.popMax (out.push x)