Documentation

Batteries.Data.BinaryHeap

structure Batteries.BinaryHeap (α : Type u_1) (lt : ααBool) :
Type u_1

A max-heap data structure.

Instances For
    @[irreducible]
    def Batteries.BinaryHeap.heapifyDown {α : Type u_1} {sz : Nat} (lt : ααBool) (a : Vector α sz) (i : Fin sz) :
    Vector α sz

    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
      def Batteries.BinaryHeap.mkHeap {α : Type u_1} {sz : Nat} (lt : ααBool) (a : Vector α sz) :
      Vector α sz

      Core operation for binary heaps, expressed directly on arrays. Construct a heap from an unsorted array, by heapifying all the elements.

      Equations
      Instances For
        def Batteries.BinaryHeap.mkHeap.loop {α : Type u_1} {sz : Nat} (lt : ααBool) (i : Nat) (a : Vector α sz) :
        i szVector α sz

        Inner loop for mkHeap.

        Equations
        Instances For
          @[irreducible]
          def Batteries.BinaryHeap.heapifyUp {α : Type u_1} {sz : Nat} (lt : ααBool) (a : Vector α sz) (i : Fin sz) :
          Vector α sz

          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
            def Batteries.BinaryHeap.empty {α : Type u_1} (lt : ααBool) :

            O(1). Build a new empty heap.

            Equations
            Instances For
              def Batteries.BinaryHeap.singleton {α : Type u_1} (lt : ααBool) (x : α) :

              O(1). Build a one-element heap.

              Equations
              Instances For
                def Batteries.BinaryHeap.size {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :

                O(1). Get the number of elements in a BinaryHeap.

                Equations
                • self.size = self.arr.size
                Instances For
                  def Batteries.BinaryHeap.vector {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :
                  Vector α self.size

                  O(1). Get data vector of a BinaryHeap.

                  Equations
                  • self.vector = { toArray := self.arr, size_toArray := }
                  Instances For
                    def Batteries.BinaryHeap.get {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (i : Fin self.size) :
                    α

                    O(1). Get an element in the heap by index.

                    Equations
                    • self.get i = self.arr[i]
                    Instances For
                      def Batteries.BinaryHeap.insert {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (x : α) :

                      O(log n). Insert an element into a BinaryHeap, preserving the max-heap property.

                      Equations
                      Instances For
                        @[simp]
                        theorem Batteries.BinaryHeap.size_insert {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (x : α) :
                        (self.insert x).size = self.size + 1
                        def Batteries.BinaryHeap.max {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :

                        O(1). Get the maximum element in a BinaryHeap.

                        Equations
                        • self.max = self.arr[0]?
                        Instances For
                          def Batteries.BinaryHeap.popMax {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :

                          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
                            @[simp]
                            theorem Batteries.BinaryHeap.size_popMax {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :
                            self.popMax.size = self.size - 1
                            def Batteries.BinaryHeap.extractMax {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :

                            O(log n). Return and remove the maximum element from a BinaryHeap.

                            Equations
                            • self.extractMax = (self.max, self.popMax)
                            Instances For
                              theorem Batteries.BinaryHeap.size_pos_of_max {α : Type u_1} {lt : ααBool} {x : α} {self : Batteries.BinaryHeap α lt} (h : self.max = some x) :
                              0 < self.size
                              def Batteries.BinaryHeap.insertExtractMax {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (x : α) :

                              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
                                def Batteries.BinaryHeap.replaceMax {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (x : α) :

                                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
                                  def Batteries.BinaryHeap.decreaseKey {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (i : Fin self.size) (x : α) :

                                  O(log n). Replace the value at index i by x. Assumes that x ≤ self.get i.

                                  Equations
                                  Instances For
                                    def Batteries.BinaryHeap.increaseKey {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) (i : Fin self.size) (x : α) :

                                    O(log n). Replace the value at index i by x. Assumes that self.get i ≤ x.

                                    Equations
                                    Instances For
                                      def Batteries.Vector.toBinaryHeap {α : Type u_1} {n : Nat} (lt : ααBool) (v : Vector α n) :

                                      O(n). Convert an unsorted vector to a BinaryHeap.

                                      Equations
                                      Instances For
                                        def Array.toBinaryHeap {α : Type u_1} (lt : ααBool) (a : Array α) :

                                        O(n). Convert an unsorted array to a BinaryHeap.

                                        Equations
                                        Instances For
                                          @[specialize #[]]
                                          def Array.heapSort {α : Type u_1} (a : Array α) (lt : ααBool) :

                                          O(n log n). Sort an array using a BinaryHeap.

                                          Equations
                                          Instances For
                                            @[irreducible]
                                            def Array.heapSort.loop {α : Type u_1} (lt : ααBool) (a : Batteries.BinaryHeap α (flip lt)) (out : Array α) :

                                            Inner loop for heapSort.

                                            Equations
                                            Instances For