Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Heap<T>

Binary heap.

Type parameters

  • T

Hierarchy

  • Heap

Constructors

constructor

  • new Heap<T>(comparator?: Comparator<T>): Heap<T>
  • Type parameters

    • T

    Parameters

    • Optional comparator: Comparator<T>

      Elements comparator.
      Defaults to comparator which compares elements with the > and === operators.

    Returns Heap<T>

Accessors

empty

  • get empty(): boolean
  • Check if heap is empty.

    Returns boolean

size

  • get size(): number
  • Get heap size

    Returns number

Methods

clear

  • clear(): void
  • Clear heap.

    Returns void

clone

  • Clone heap.
    Notice that items are not cloned, only heap internal structure is cloned.

    Returns Heap<T>

contains

  • Check if heap contains item.

    Parameters

    • itemOrPredicate: T | ArrayEqualsPredicate<T>

      Item or function which checks for equality with that item.

    Returns boolean

findIndex

  • Find index of item in the heap.

    Parameters

    Returns number

    Index of the item.

peek

  • peek(): undefined | T
  • Peek element from heap root (does not remove it).

    Returns undefined | T

    Heap root.

pop

  • pop(): undefined | T
  • Pop element from heap root (does remove it).

    Returns undefined | T

    Heap root.

push

  • push(item: T): void
  • Push item into heap.

    Parameters

    • item: T

      Item to be pushed.

    Returns void

remove

  • remove(index: number): void
  • Remove item located at index.

    Parameters

    • index: number

      Value of the item index.

    Returns void

replaceRootWith

  • replaceRootWith(item: T): undefined | T
  • Replace heap root with another item.

    Parameters

    • item: T

      Item that needs to replace the root.

    Returns undefined | T

    Previous value of the heap root.

toArray

  • toArray(): T[]
  • Get array representation of the heap.

    Returns T[]

update

  • update(index: number, newItem: T): void
  • Update item located at index with newItem.

    Parameters

    • index: number

      Index of the element that needs to be updated.

    • newItem: T

      Value that needs to be put at that index.

    Returns void