Heap

protocol Heap

The Heap protocol

  • T

    The type to store in the Heap

    Declaration

    Swift

    associatedtype T
  • The storage array of the Heap

    Declaration

    Swift

    var storage: [T] { get set }
  • size Default implementation

    The size of the Heap

    Default Implementation

    The internal storage’s Array.size property

    Declaration

    Swift

    var size: Int { get }
  • isEmpty Default implementation

    True if the Heap is empty it is true and otherwise is false.

    Default Implementation

    The internal storage’s Array.isEmpty property

    Declaration

    Swift

    var isEmpty: Bool { get }
  • A function implementing the heapifyUp algorithm

    Declaration

    Swift

    mutating func heapifyUp()
  • A function implementing the heapifyDown algorithm

    Declaration

    Swift

    mutating func heapifyDown()
  • peak() Extension method

    Read the next item off the Heap without removing it.

    Declaration

    Swift

    func peak() -> T?

    Return Value

    The next item of type T from the Heap or, if the Heap.isEmpty return’s nil.

  • pull() Extension method

    Read and remove the next item off Heap.

    Declaration

    Swift

    mutating func pull() -> T?

    Return Value

    The next item of type T from the Heap or, if the Heap.isEmpty return’s nil.

  • add(_:) Extension method

    Add an item to the Heap.

    Declaration

    Swift

    mutating func add(_ elem: T)

    Parameters

    elem

    Item to add to the Heap.

  • getLeftChildIndex(_:) Extension method

    Calculates the left child index for any given index

    Declaration

    Swift

    internal static func getLeftChildIndex(_ parent: Int) -> Int

    Parameters

    parent

    the parent index

    Return Value

    left child index

  • getRightChildIndex(_:) Extension method

    Calculates the right child index for any given index

    Declaration

    Swift

    internal static func getRightChildIndex(_ parent: Int) -> Int

    Parameters

    parent

    the parent index

    Return Value

    right child index

  • getParentIndex(_:) Extension method

    Calculates the parent index for any given index

    Declaration

    Swift

    internal static func getParentIndex(_ child: Int) -> Int

    Parameters

    child

    the parent index

    Return Value

    right child index

  • hasLeftChild(_:) Extension method

    Checks to see if there is a left child

    Declaration

    Swift

    internal func hasLeftChild(_ parent: Int) -> Bool

    Parameters

    parent

    the parent index

    Return Value

    true if there is a left child otherwise it returns false.

  • hasRightChild(_:) Extension method

    Checks to see if there is a right child

    Declaration

    Swift

    internal func hasRightChild(_ parentIndex: Int) -> Bool

    Parameters

    parent

    the parent index

    Return Value

    true if there is a right child otherwise it returns false.

  • hasParent(_:) Extension method

    Checks to see if there is a parent

    Declaration

    Swift

    internal func hasParent(_ child: Int) -> Bool

    Parameters

    child

    The child index.

    Return Value

    True if there is a parent otherwise it returns false.

  • getLeftChild(_:) Extension method

    Retrieve the left child

    Declaration

    Swift

    internal func getLeftChild(_ parent: Int) -> T

    Parameters

    parent

    The parent index.

    Return Value

    The left child

  • getRightChild(_:) Extension method

    Retrieve the right child

    Declaration

    Swift

    internal func getRightChild(_ parent: Int) -> T

    Parameters

    parent

    the parent index

    Return Value

    The right child.

  • getParent(_:) Extension method

    Retrieve the parent

    Declaration

    Swift

    internal func getParent(_ child: Int) -> T

    Parameters

    child

    the child index

    Return Value

    the parent

  • swap(_:_:) Extension method

    Swap element’s indecies

    Declaration

    Swift

    internal mutating func swap(_ first: Int, _ second: Int)

    Parameters

    first

    The first element’s index.

    second

    The second element’s index.