Class Heap<T>

Heap

Type Parameters

  • T

Hierarchy

  • Heap

Implements

  • Iterable<T>

Constructors

  • Heap instance constructor.

    Type Parameters

    • T

    Parameters

    • compare: Comparator<T> = Heap.minComparator

      Optional comparison function, defaults to Heap.minComparator

    Returns Heap<T>

Properties

_limit: number = 0
compare: Comparator<T> = Heap.minComparator

Optional comparison function, defaults to Heap.minComparator

element: (() => undefined | T) = ...

Type declaration

    • (): undefined | T
    • Alias of __type

      Returns undefined | T

      See

      peek

heapArray: T[] = []
offer: ((element) => boolean) = ...

Type declaration

    • (element): boolean
    • Alias of __type

      Parameters

      • element: T

      Returns boolean

      See

      add

poll: (() => undefined | T) = ...

Type declaration

    • (): undefined | T
    • Alias of __type

      Returns undefined | T

      See

      pop

removeAll: (() => void) = ...

Type declaration

    • (): void
    • Alias of __type

      Returns void

      See

      clear

Accessors

  • get length(): number
  • Length of the heap. Aliases: size.

    Returns number

    See

    size

  • get limit(): number
  • Get length limit of the heap. Use setLimit or limit to set the limit.

    Returns number

    See

    setLimit

  • set limit(_l): void
  • Set length limit of the heap. Same as using setLimit.

    Parameters

    • _l: number

      Limit, defaults to 0 (no limit). Negative, Infinity, or NaN values set the limit to 0.

    Returns void

    Description

    If the heap is longer than the limit, the needed amount of leafs are removed.

    See

    setLimit

Methods

  • Iterator interface

    Returns Iterator<T, any, undefined>

  • Limit heap size if needed

    Returns void

  • Return the bottom (lowest value) N elements of the heap, without corner cases, unsorted

    Parameters

    • n: number

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Returns the inverse to the comparison function.

    Parameters

    • a: T
    • b: T

    Returns number

  • Move a node to a new index, switching places

    Parameters

    • j: number

      First node index

    • k: number

      Another node index

    Returns void

  • Move a node down the tree (to the leaves) to find a place where the heap is sorted.

    Parameters

    • i: number

      Index of the node

    Returns void

  • Move a node up the tree (to the root) to find a place where the heap is sorted.

    Parameters

    • i: number

      Index of the node

    Returns void

  • Return index of the top element

    Parameters

    • list: T[]

    Returns number

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: init + push.

    Parameters

    • n: number

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: heap.

    Parameters

    • n: number

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Return the top (highest value) N elements of the heap, without corner cases, unsorted Implementation: push.

    Parameters

    • n: number

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Return the top element

    Parameters

    • Rest ...list: T[]

    Returns undefined | T

  • Adds an element to the heap. Aliases: offer. Same as: push(element).

    Parameters

    • element: T

      Element to be added

    Returns boolean

    true

  • Adds an array of elements to the heap. Similar as: push(element, element, ...).

    Parameters

    • elements: T[]

      Elements to be added

    Returns boolean

    true

  • Return the bottom (lowest value) N elements of the heap.

    Parameters

    • n: number = 1

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Check if the heap is sorted, useful for testing purposes.

    Returns undefined | T

    Returns an element if something wrong is found, otherwise it's undefined

  • Remove all of the elements from this heap.

    Returns void

  • Returns true if this queue contains the specified element.

    Parameters

    • o: T

      Element to be found

    • callbackFn: IsEqual<T> = Heap.defaultIsEqual

      Optional comparison function, receives (element, needle)

    Returns boolean

  • Get the element at the given index.

    Parameters

    • i: number

      Index to get

    Returns T

    Element at that index

  • Get the elements of these node's children

    Parameters

    • idx: number

      Node index

    Returns T[]

    Children elements

  • Get the element of this node's parent

    Parameters

    • idx: number

      Node index

    Returns undefined | T

    Parent element

  • Get the index of the first occurrence of the element in the heap (using the comparator).

    Parameters

    • element: T

      Element to be found

    • callbackFn: IsEqual<T> = Heap.defaultIsEqual

      Optional comparison function, receives (element, needle)

    Returns number

    Index or -1 if not found

  • Get the indexes of the every occurrence of the element in the heap (using the comparator).

    Parameters

    • element: T

      Element to be found

    • callbackFn: IsEqual<T> = Heap.defaultIsEqual

      Optional comparison function, receives (element, needle)

    Returns number[]

    Array of indexes or empty array if not found

  • Initialize a heap, sorting nodes

    Parameters

    • Optional array: T[]

      Optional initial state array

    Returns void

  • Test if the heap has no elements.

    Returns boolean

    True if no elements on the heap

  • Returns an iterator. To comply with Java interface.

    Returns Iterable<T>

  • Get the leafs of the tree (no children nodes). See also: getChildrenOf and bottom.

    Returns T[]

    See

    • getChildrenOf
    • bottom
  • Top node. Aliases: element. Same as: top(1)[0].

    Returns undefined | T

    Top node

    See

    top

  • Extract the top node (root). Aliases: poll.

    Returns undefined | T

    Extracted top node, undefined if empty

  • Pushes element(s) to the heap. See also: __type and addAll.

    Parameters

    • Rest ...elements: T[]

      Elements to insert

    Returns boolean

    True if elements are present

  • Same as push & pop in sequence, but faster

    Parameters

    • element: T

      Element to insert

    Returns T

    Extracted top node

  • Remove the first occurrence of an element from the heap.

    Parameters

    • Optional o: T

      Element to be found

    • callbackFn: IsEqual<T> = Heap.defaultIsEqual

      Optional equality function, receives (element, needle)

    Returns boolean

    True if the heap was modified

  • Pop the current peek value, and add the new item.

    Parameters

    • element: T

      Element to replace peek

    Returns T

    Old peek

  • Set length limit of the heap. Same as assigning to limit but returns NaN if the value was invalid.

    Parameters

    • _l: number

      Limit. Negative, Infinity, or NaN values set the limit to 0.

    Returns number

    The limit or NaN if the value was negative, or NaN.

    See

    limit

  • Size of the heap

    Returns number

  • Clone the heap's internal array

    Returns T[]

  • String output, call to Array.prototype.toString()

    Returns string

  • Return the top (highest value) N elements of the heap.

    Parameters

    • n: number = 1

      Number of elements.

    Returns T[]

    Array of length <= N.

  • Default equality function.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns boolean

    True if equal, false otherwise

  • Gets children indices for given index.

    Parameters

    • idx: number

      Parent index

    Returns number[]

    Array of children indices

  • Gets parent index for given index.

    Parameters

    • idx: number

      Children index

    Returns number

    Parent index, -1 if idx is 0

  • Gets sibling index for given index.

    Parameters

    • idx: number

      Children index

    Returns number

    Sibling index, -1 if idx is 0

  • Return the n least valuable elements of a heap-like Array

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array, should be an array-heap

    • n: number = 1

      Max number of elements

    • Optional compare: Comparator<N>

      Optional compare function

    Returns N[]

    Elements

  • Converts an array into an array-heap, in place

    Type Parameters

    • N

    Parameters

    • arr: N[]

      Array to be modified

    • Optional compare: Comparator<N>

      Optional compare function

    Returns Heap<N>

    For convenience, it returns a Heap instance

  • Extract the peek of an array-heap

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • Optional compare: Comparator<N>

      Optional compare function

    Returns undefined | N

    Returns the extracted peek

  • Pushes a item into an array-heap

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item to push

    • Optional compare: Comparator<N>

      Optional compare function

    Returns void

  • Push followed by pop, faster

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item to push

    • Optional compare: Comparator<N>

      Optional compare function

    Returns N

    Returns the extracted peek

  • Replace peek with item

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array to be modified, should be a heap

    • item: N

      Item as replacement

    • Optional compare: Comparator<N>

      Optional compare function

    Returns N

    Returns the extracted peek

  • Return the n most valuable elements of a heap-like Array

    Type Parameters

    • N

    Parameters

    • heapArr: N[]

      Array, should be an array-heap

    • n: number = 1

      Max number of elements

    • Optional compare: Comparator<N>

      Optional compare function

    Returns N[]

    Elements

  • Max heap comparison function.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns number

    0 if they're equal, positive if a goes up, negative if b goes up

  • Max number heap comparison function.

    Parameters

    • a: number

      First element

    • b: number

      Second element

    Returns number

    0 if they're equal, positive if a goes up, negative if b goes up

  • Min heap comparison function, default.

    Type Parameters

    • N

    Parameters

    • a: N

      First element

    • b: N

      Second element

    Returns number

    0 if they're equal, positive if a goes up, negative if b goes up

  • Min number heap comparison function, default.

    Parameters

    • a: number

      First element

    • b: number

      Second element

    Returns number

    0 if they're equal, positive if a goes up, negative if b goes up

  • Return the n most valuable elements of an iterable

    Type Parameters

    • N

    Parameters

    • n: number

      Max number of elements

    • iterable: Iterable<N>
    • Optional compare: Comparator<N>

      Optional compare function

    Returns N[]

    Elements

  • Return the n least valuable elements of an iterable

    Type Parameters

    • N

    Parameters

    • n: number

      Max number of elements

    • iterable: Iterable<N>
    • Optional compare: Comparator<N>

      Optional compare function

    Returns N[]

    Elements

  • Prints a heap.

    Type Parameters

    • N

    Parameters

    • heap: Heap<N>

      Heap to be printed

    Returns string

Generated using TypeDoc