Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BinaryHeap<T>

Implementation of a Binary Heap that behaves like a max binary heap with numeric values by default. Nevertheless, it can be provided a custom CompareFunction to store any type of data, or to use it as a min binary heap for instance.

Type parameters

  • T

Hierarchy

  • BinaryHeap

Implements

Index

Properties

compare

compare: CompareFunction<T> = compareMethods.compare.bind(this)

The function used to compare two elements. It is determinant for the heap structure. By default, it compares numbers so it must be replaced with a custom function when storing another data type. It also allows to use a MinBinaryHeap

param

Element A

param

Element B

returns

-1 if a < b, 1 if a > b, 0 if a === b

equal

equal: equal = compareMethods.equal.bind(this)

inf

inf: inf = compareMethods.inf.bind(this)

infOrEqual

infOrEqual: infOrEqual = compareMethods.infOrEqual.bind(this)

sup

sup: sup = compareMethods.sup.bind(this)

supOrEqual

supOrEqual: supOrEqual = compareMethods.supOrEqual.bind(this)

Private values

values: T[] = []

Accessors

length

  • get length(): number
  • Returns number

Methods

Private bubbleDown

  • bubbleDown(): void
  • Returns void

Private bubbleUp

  • bubbleUp(): void
  • Returns void

clear

  • clear(): void
  • Returns void

Private hasIndex

  • hasIndex(i: number): boolean
  • Parameters

    • i: number

    Returns boolean

insert

  • insert(...values: T[]): T
  • Parameters

    • Rest ...values: T[]

    Returns T

setCompareFunction

  • Replaces the current compare function with the provided CompareFunction. A compare function has the signature: CompareFunction<T>(a: T, b: T) => -1 | 0 | 1.

    To keep its integrity, the tree is fully rebuilt.

    Parameters

    • compareFunction: CompareFunction<T>

      The function to use to compare two values.

    Returns BinaryHeap<T>

    The tree instance.

shift

  • shift(): undefined | T
  • Returns undefined | T

Private swap

  • swap(i: number, j: number): void
  • Parameters

    • i: number
    • j: number

    Returns void

toArray

  • toArray(): T[]
  • Returns T[]

Generated using TypeDoc