From 5d754e35efa5e13275b44196bdc6d539f7455120 Mon Sep 17 00:00:00 2001 From: ksw2000 <13825170+ksw2000@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:46:21 +0800 Subject: [PATCH] Reset heaps after union operation. --- heap.go | 9 ++++++++- heap_test.go | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/heap.go b/heap.go index 2c76eaf..0e8fd23 100644 --- a/heap.go +++ b/heap.go @@ -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") @@ -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 } diff --git a/heap_test.go b/heap_test.go index 30f07be..aca1ea0 100644 --- a/heap_test.go +++ b/heap_test.go @@ -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") + } }