1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:04:41 -07:00

add heap.Remove

R=gri
DELTA=14  (14 added, 0 deleted, 0 changed)
OCL=34636
CL=34687
This commit is contained in:
Russ Cox 2009-09-16 10:43:49 -07:00
parent 1bbc044df9
commit 0ee18ca816

View File

@ -52,6 +52,20 @@ func Pop(h HeapInterface) interface{} {
}
// Remove removes the element at index i from the heap.
// The complexity is O(log(n)) where n = h.Len().
//
func Remove(h HeapInterface, i int) interface{} {
n := h.Len()-1;
if n != i {
h.Swap(n, i);
down(h, i, n);
up(h, i);
}
return h.Pop();
}
func up(h HeapInterface, j int) {
for {
i := (j-1)/2;