MinHeap Class
The MinHeap class provides the main functionality of a heap, keeping the minimum on the top.
Item Index
Methods
Properties
Methods
_child
-
n
Parameters:
-
n
Object
Returns:
number
_displayNode
-
node
-
prefix
-
last
Parameters:
-
node
Object -
prefix
Object -
last
Object
Returns:
String
_parent
-
n
Parameters:
-
n
Object
Returns:
number
compare
-
first
-
second
Compare elements in order to place them correctly in the heap while sifting up.
Parameters:
-
first
ObjectThe value of the first node being compared.
-
second
ObjectThe value of the second node being compared.
Returns:
number Result of the comparison, positive integer if value1 is greater than value2, 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
ObjectThe 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
recoverFromCorruption
()
Recover from the corrupted state and allow further actions on the heap.
Returns:
void
rewind
()
Rewind iterator back to the start (no-op)
Returns:
void
shiftDown
-
i
Parameters:
-
i
Object
shiftUp
-
i
Parameters:
-
i
Object
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
_tree
Array
private
Binary tree storage array