Class MinHeap
Defined in: minheap.js.
Constructor Attributes | Constructor Name and Description |
---|---|
MinHeap(array, comparator)
Implementation of a min heap allowing for a comparator
to be provided so that the heap may contain objects that involve a
more complex comparison.
|
Method Attributes | Method Name and Description |
---|---|
getMin()
Returns the minimum value contained within the heap.
|
|
insert(item)
Insert an item into the heap.
|
|
pop()
Pop the minimum valued item off of the heap.
|
|
push(item)
Place an item in the heap.
|
|
remove()
Remove the minimum item from the heap.
|
|
size()
Return the current number of elements within the heap.
|
Class Detail
MinHeap(array, comparator)
Implementation of a min heap allowing for a comparator
to be provided so that the heap may contain objects that involve a
more complex comparison.
This constructor constructs a MinHeap instance and takes two optional parameters: an array and comparator. If the array is provided, it will be used as the backing store for the heap. Therefore, all operations on the heap will be reflected in the provided array. Usage
This constructor constructs a MinHeap instance and takes two optional parameters: an array and comparator. If the array is provided, it will be used as the backing store for the heap. Therefore, all operations on the heap will be reflected in the provided array. Usage
Sample Usage: var heapq = new MinHeap(); heapq.push(5); heapq.push(2); heapq.push(1); heapq.pop()); // returns 1 heapq.pop()); // returns 2
- Parameters:
- array
- Array to use heapify or null to start with an empty heap.
- comparator
- alternate comparator used to compare each item within the heap. If not provided, the default will perform a simple comparison on the item.
- Returns:
- instance of MinHeap
Method Detail
getMin()
Returns the minimum value contained within the heap. This will
not remove the value from the heap.
- Returns:
- the minimum value within the heap.
insert(item)
Insert an item into the heap.
- Parameters:
- item
pop()
Pop the minimum valued item off of the heap. The heap is then updated
to float the next smallest item to the top of the heap.
- Returns:
- the minimum value contained within the heap.
push(item)
Place an item in the heap.
- Parameters:
- item
remove()
Remove the minimum item from the heap.
- Returns:
- the minimum value contained within the heap.
size()
Return the current number of elements within the heap.
- Returns:
- size of the heap.