Skip to content

Commit 520c50e

Browse files
safwan-mohaTheofanis Despoudis
authored andcommitted
fixes #31: Delete-for-Binomial-Heap (#39)
* feat(issue/31): Delete-for-Binomial-Heap * issue/31: setting single pointer and unexporting findAny func
1 parent cb91ced commit 520c50e

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

binomial/binomial_heap.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package binomial
77

88
import (
9+
"math"
10+
911
heap "github.com/theodesp/go-heaps"
1012
)
1113

@@ -56,6 +58,53 @@ func (b *BinomialHeap) DeleteMin() heap.Item {
5658
return min.item
5759
}
5860

61+
// Deletes passed item from the heap.
62+
// The complexity is O(log n).
63+
func (p *BinomialHeap) Delete(item heap.Item) {
64+
found := p.findAny(item)
65+
next := found
66+
for next.parent != nil {
67+
temp := next.item
68+
next.item = next.parent.item
69+
next.parent.item = temp
70+
71+
next = next.parent
72+
}
73+
next.item = heap.Integer(math.Inf(-1))
74+
p.DeleteMin()
75+
}
76+
77+
// FindAny returns the address of item in the heap.
78+
func (b *BinomialHeap) findAny(item heap.Item) *node {
79+
next := b.root
80+
backToParent := false
81+
82+
for next != nil {
83+
if next.child != nil && !backToParent {
84+
if next.item == item {
85+
return next
86+
}
87+
next = next.child
88+
backToParent = false
89+
} else if next.sibling != nil {
90+
if next.item == item {
91+
return next
92+
}
93+
next = next.sibling
94+
backToParent = false
95+
} else if next.parent != nil {
96+
if next.item == item {
97+
return next
98+
}
99+
next = next.parent
100+
backToParent = true
101+
} else {
102+
next = nil
103+
}
104+
}
105+
return b.root
106+
}
107+
59108
// FindMin returns the smallest item in the heap.
60109
// The complexity is O(log n).
61110
func (b *BinomialHeap) FindMin() heap.Item {

binomial/binomial_heap_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,50 @@ func TestLeftistHeapInteger(t *testing.T) {
2525
}
2626
}
2727

28+
func TestBinomialHeapIntegerDelete(t *testing.T) {
29+
heap := &BinomialHeap{}
30+
31+
numbers := []int{14, 112, 15, 16 , 71, 91, 1, 12, 23, 56, 34}
32+
33+
for _, number := range numbers {
34+
heap.Insert(Int(number))
35+
}
36+
37+
heap.Delete(Int(15))
38+
heap.Delete(Int(12))
39+
40+
numbers = RemoveInts(numbers, 15)
41+
numbers = RemoveInts(numbers, 12)
42+
sort.Ints(numbers)
43+
44+
for _, number := range numbers {
45+
if Int(number) != heap.DeleteMin().(go_heaps.Integer) {
46+
t.Fail()
47+
}
48+
}
49+
}
50+
51+
func TestBinomialHeapStringDelete(t *testing.T) {
52+
heap := &BinomialHeap{}
53+
54+
strings := []string{"a", "ccc", "bb", "d"}
55+
56+
for _, str := range strings {
57+
heap.Insert(Str(str))
58+
}
59+
60+
heap.Delete(Str("ccc"))
61+
62+
strings = RemoveStrs(strings, "ccc")
63+
sort.Strings(strings)
64+
65+
for _, str := range strings {
66+
if Str(str) != heap.DeleteMin().(go_heaps.String) {
67+
t.Fail()
68+
}
69+
}
70+
}
71+
2872
func TestLeftistHeapString(t *testing.T) {
2973
heap := &BinomialHeap{}
3074

@@ -43,6 +87,20 @@ func TestLeftistHeapString(t *testing.T) {
4387
}
4488
}
4589

90+
func RemoveInts(s []int, hay int) []int {
91+
sort.Ints(s)
92+
i := sort.SearchInts(s, hay)
93+
s[i] = s[len(s)-1]
94+
return s[:len(s)-1]
95+
}
96+
97+
func RemoveStrs(s []string, hay string) []string {
98+
sort.Strings(s)
99+
i := sort.SearchStrings(s, hay)
100+
s[i] = s[len(s)-1]
101+
return s[:len(s)-1]
102+
}
103+
46104
func Int(value int) go_heaps.Integer {
47105
return go_heaps.Integer(value)
48106
}

0 commit comments

Comments
 (0)