nv-data-binary-heap
v1.0.0
Published
nv-data-binary-heap ============ - nv-data-binary-heap - wrap of https://github.com/nodejs/node/blob/178e52a7ead2ef9ffb6eb5e17be167f17beb45a8/lib/internal/priority\_queue.js
Downloads
1
Readme
nv-data-binary-heap
- nv-data-binary-heap
- wrap of https://github.com/nodejs/node/blob/178e52a7ead2ef9ffb6eb5e17be167f17beb45a8/lib/internal/priority_queue.js
install
- npm install nv-data-binary-heap
usage
- the code is from nodejs lib/internal/priority_queue.js
- this structure is used in nodejs as a timerListQueue
- its useful when you modify nodejs
example
const {Heap} = require("nv-data-binary-heap")
var heap = new Heap(4)
### constructor
/*
> heap
Heap { [Symbol(heap)]: [ <4 empty items> ], [Symbol(size)]: 0 }
heap.insert(0)
heap.insert(1)
heap.insert(2)
heap.insert(3)
> heap.size()
4
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3 ],
[Symbol(size)]: 4
}
> heap.insert(4)
undefined
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 4 ],
[Symbol(size)]: 5
}
>
*/
### insert
/*
> heap.insert(5)
undefined
>
>
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 5 ],
[Symbol(size)]: 5
}
>
*/
### peak
/*
> heap.peek()
0
> heap.insert(4)
undefined
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 5, 4 ],
[Symbol(size)]: 6
}
> heap.insert(5)
undefined
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 5, 4, 5 ],
[Symbol(size)]: 7
}
> heap.peek()
0
>
*/
### remove
/*
> heap.remove(5)
true
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 5, 4, undefined ],
[Symbol(size)]: 6
}
>
*/
### removeAt
/*
> heap.insert(5)
undefined
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 3, 5, 4, 5 ],
[Symbol(size)]: 7
}
>
> heap.removeAt(4)
undefined
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 0, 1, 2, 5, 5, 4, undefined ],
[Symbol(size)]: 6
}
>
*/
### shift
/*
> heap.shift()
0
> heap.shift()
1
> heap
Heap {
[Symbol(heap)]: [ <1 empty item>, 2, 4, 5, 5, undefined, undefined, undefined ],
[Symbol(size)]: 4
}
>
*/
### parent
/*
> var heap = new Heap(4)
undefined
> heap.parent(1)
undefined
> heap.insert(10)
undefined
> heap.insert(20)
undefined
> heap.insert(30)
undefined
> heap.insert(40)
undefined
>
> heap.peek()
10
> heap.parent(1)
undefined
> heap.parent(2)
10
> heap.parent(3)
10
> heap.parent(4)
20
> heap.parent(5)
20
> heap.parent(6)
30
>
*/
API
class
- Heap(heapSize,comparator, setPosition)
- PriorityQueue(comparator, setPosition,{heapSize})
method
- insert(value)
- peek()
- percolateDown(pos)
- percolateUp(pos)
- removeAt(pos)
- remove(value)
- shift()
- size()
- parent(pos)
- indexOf(value)
function
- compareTimersLists(a, b)
- setPosition(node, pos)
LICENSE
- ISC