new BinaryHeap(weightFuncopt, compareFuncopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
weightFunc |
function |
<optional> |
|
compareFunc |
function |
<optional> |
- Source:
Example
import { BinaryHeap } from 'cachefactory';
const queue = new BinaryHeap();
queue.push(2);
queue.push(77);
queue.push(8);
queue.push(33);
queue.push(5);
queue.pop(); // 2
queue.pop(); // 5
queue.pop(); // 8
queue.pop(); // 33
queue.pop(); // 77
const userQueue = new BinaryHeap(
(user) => user.age,
(userOne, userTwo) => userOne.id === userTwo.id
);
queue.push({ id: 1, age: 34 });
queue.push({ id: 2, age: 29 });
queue.push({ id: 3, age: 25 });
queue.push({ id: 3, age: 28 });
queue.push({ id: 3, age: 27 });
queue.push({ id: 4, age: 42 });
queue.push({ id: 5, age: 19 });
queue.pop(); // { id: 5, age: 19 }
queue.pop(); // { id: 3, age: 27 }
queue.pop(); // { id: 2, age: 29 }
queue.pop(); // { id: 1, age: 34 }
queue.pop(); // { id: 4, age: 42 }
Members
compareFunc :function
The heap's configured compare function.
Default:
function (x, y) {
return x === y;
}
Type:
- function
- Source:
heap :Array.<*>
The heap's data.
Type:
- Array.<*>
- Source:
weightFunc :function
The heap's configured weight function.
Default:
function (x) {
return x;
}
Type:
- function
- Source:
Methods
peek() → {*}
Look at the item at the front of the queue.
- Source:
Returns:
- Type
- *
pop() → {*}
Pop an item off the front of the queue.
- Source:
Returns:
- Type
- *
push(node)
Push an item into the queue.
Parameters:
Name | Type | Description |
---|---|---|
node |
* |
- Source:
remove(node)
Remove the given item from the queue.
Parameters:
Name | Type | Description |
---|---|---|
node |
* |
- Source:
removeAll()
Clear the heap.
- Source:
size() → {number}
Return the length of the queue.
- Source:
Returns:
- Type
- number