Private
Readonly
#internalInternal implementation of this Heap.
This class is implemented with a JS Array, defining node relationships implicitly.
Specifically, given a parent node with index i
,
its two child nodes have indices 2 * i + 1
and 2 * i + 2
.
Using an array is more performant than having nodes hold explicit references to their children.
Private
Readonly
comparatora Comparator function comparing nodes’ values
The number of nodes in this Heap.
Private
#internalPrivate
#internalPrivate
#siftPrivate
#siftReturn a shallow copy of this Heap’s internal implementation. The returned array is only a copy, so mutating it will not mutate this Heap. However, the array’s items are the same references as this Heap’s nodes, so any mutations of them will be observed.
a copy of this Heap’s array
Add the given items to this Heap and return this Heap.
Rest
...nodes: readonly T[]the items to add
this
Remove the given node.
the node to remove
[this, node]
, where node
is the removed node
if the node is not in this Heap
if this Heap is empty
Remove the first node that satisfies the given predicate.
a function to find nodes
[this, node]
, where node
is the removed node
if no node satisfies the predicate
if this Heap is empty
Remove all of the given nodes.
the nodes to remove
[this, nodes]
, where nodes
is a list of the removed nodes
if this Heap is empty
Remove all nodes that satisfy the given predicate.
a function to find nodes
[this, nodes]
, where nodes
is a list of the removed nodes
if this Heap is empty
Reorders the nodes in this Heap to a valid arrangement. Should be called whenever one or more nodes have changed/mutated in a way that affects this Heap’s “max heap” property (that is, that every node should be greater than its children). If the nodes are already in a valid order, nothing is changed.
this
A max binary heap.
A heap data structure is a tree with the property that every parent node’s value is greater than or equal to (for a max heap) the values of its children nodes. (For a min heap, parents would be less than or equal to each of their children.) Every node in a binary heap has 2 children or less. Being a tree, every node in a heap has exactly one parent, with the exception of the root node, which has no parent.
See
https://en.wikipedia.org/wiki/Heap_(data_structure)
Typeparam
T the type of node value in this Heap