API Docs for: 0.1.21
Show:

Heap Class

The Heap class provides the main functionality of a Heap.

Methods

_child

(
  • n
)
private

Get index of left child element in binary tree stored in array

Parameters:

  • n Object

Returns:

number

_displayNode

(
  • node
  • prefix
  • last
)
private

Visually display heap tree

Parameters:

  • node Object
  • prefix Object
  • last Object

Returns:

String

_parent

(
  • n
)
private

Get index of parent element in binary tree stored in array

Parameters:

  • n Object

Returns:

number

_siftDown

(
  • i
)
private

Sift down elements in binary tree

Parameters:

  • i Object

_siftUp

(
  • i
)
private

Sift elements in binary tree

Parameters:

  • i Object

_swap

(
  • first
  • second
)
private

Swap 2 elements in binary tree

Parameters:

  • first Object
  • second Object

compare

(
  • first
  • second
)

Compare elements in order to place them correctly in the heap while sifting up.

Parameters:

  • first Object

    The value of the first node being compared.

  • second Object

    The value of the second node being compared.

Returns:

number Result of the comparison, positive integer if first is greater than second, 0 if they are equal, negative integer otherwise. Having multiple elements with the same value in a Heap is not recommended. They will end up in an arbitrary relative position.

count

()

Counts the number of elements in the heap

Returns:

number the number of elements in the heap.

current

()

Return current node pointed by the iterator

Returns:

any The current node value.

extract

()

Extracts a node from top of the heap and sift up

Returns:

any The value of the extracted node.

insert

(
  • value
)

Inserts an element in the heap by sifting it up

Parameters:

  • value Object

    The value to insert.

Returns:

void

isEmpty

()

Checks whether the heap is empty

Returns:

boolean whether the heap is empty.

key

()

Return current node index

Returns:

any The current node index.

next

()

Move to the next node

Returns:

void

prev

()

Move to previous entry

Returns:

void

rewind

()

Rewind iterator back to the start (no-op)

Returns:

void

toArray

()

Serializes the heap to array

Returns:

Array The serialized array.

top

()

Peeks at the node from the top of the heap

Returns:

any The value of the node on the top.

toString

()

Serializes the heap to string

Returns:

string The serialized string.

valid

()

Check whether the heap contains more nodes

Returns:

boolean true if the heap contains any more nodes, false otherwise.

Properties

_key

Number private

Iteration pointer

_tree

Array private

Binary tree storage array

_type

Number private

Heap type

MAX

Number static

Max heap flag

MIN

Number static

Min heap flag