PriorityQueue Class
The PriorityQueue class provides the main functionality of an prioritized queue, implemented using a max heap.
Item Index
Methods
Methods
_child
-
n
Get index of left child element in binary tree stored in array
Parameters:
-
n
Object
Returns:
number
_displayNode
-
node
-
prefix
-
last
Visually display heap tree
Parameters:
-
node
Object -
prefix
Object -
last
Object
Returns:
String
_parent
-
n
Get index of parent element in binary tree stored in array
Parameters:
-
n
Object
Returns:
number
_siftDown
-
i
Sift down elements in binary tree
Parameters:
-
i
Object
_siftUp
-
i
Sift elements in binary tree
Parameters:
-
i
Object
_swap
-
first
-
second
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
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 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.
dequeue
()
Dequeues a node from the queue
Returns:
any The value of the dequeued node.
enqueue
-
value
-
priority
Adds an element to the queue
Parameters:
-
value
ObjectThe value to enqueue.
-
priority
ObjectThe priority of value.
Returns:
void
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
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