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} (lt : ααBool) (a : Array α) (i : Fin a.size) :
    { a' : Array α // a'.size = a.size }

    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
      @[simp]
      theorem Batteries.BinaryHeap.size_heapifyDown {α : Type u_1} (lt : ααBool) (a : Array α) (i : Fin a.size) :
      (Batteries.BinaryHeap.heapifyDown lt a i).val.size = a.size
      def Batteries.BinaryHeap.mkHeap {α : Type u_1} (lt : ααBool) (a : Array α) :
      { a' : Array α // a'.size = a.size }

      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} (lt : ααBool) (i : Nat) (a : Array α) :
        i a.size{ a' : Array α // a'.size = a.size }

        Inner loop for mkHeap.

        Equations
        Instances For
          @[simp]
          theorem Batteries.BinaryHeap.size_mkHeap {α : Type u_1} (lt : ααBool) (a : Array α) :
          (Batteries.BinaryHeap.mkHeap lt a).val.size = a.size
          @[irreducible]
          def Batteries.BinaryHeap.heapifyUp {α : Type u_1} (lt : ααBool) (a : Array α) (i : Fin a.size) :
          { a' : Array α // a'.size = a.size }

          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
            @[simp]
            theorem Batteries.BinaryHeap.size_heapifyUp {α : Type u_1} (lt : ααBool) (a : Array α) (i : Fin a.size) :
            (Batteries.BinaryHeap.heapifyUp lt a i).val.size = a.size
            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.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.get 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.get? 0
                      Instances For
                        def Batteries.BinaryHeap.popMaxAux {α : Type u_1} {lt : ααBool} (self : Batteries.BinaryHeap α lt) :
                        { a' : Batteries.BinaryHeap α lt // a'.size = self.size - 1 }

                        Auxiliary for popMax.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        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
                          • self.popMax = self.popMaxAux.val
                          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} (e : 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 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