Skip to content

Commit

Permalink
Reset heaps after union operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksw2000 committed Jul 17, 2024
1 parent dfe9c53 commit 5d754e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func (h *Heap[K, V]) cascadingCut(y *Element[K, V]) {
}

// Union unions the two fibonacci heaps h and g, and returns the new fibonacci
// heap with amortized running time Θ(1).
// heap with amortized running time Θ(1). The heap h and g will be reset after
// unioning.
func (h *Heap[K, V]) Union(g *Heap[K, V]) *Heap[K, V] {
if h == nil || g == nil {
panic("fibheap: Union expects non-nil heap h and g")
Expand Down Expand Up @@ -295,5 +296,11 @@ func (h *Heap[K, V]) Union(g *Heap[K, V]) *Heap[K, V] {
m.min = g.min
}

// clear heap h and heap g
h.min = nil
h.elements = 0
g.min = nil
g.elements = 0

return m
}
7 changes: 7 additions & 0 deletions heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ func TestUnion(t *testing.T) {
x := k.ExtractMin()
assert(t, x.Key(), i)
}

if h.min != nil || h.elements != 0 {
t.Fatal("h should be clear after Union")
}
if g.min != nil || g.elements != 0 {
t.Fatal("g should be clear after Union")
}
}

0 comments on commit 5d754e3

Please sign in to comment.