-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathheap.go
74 lines (65 loc) · 1.44 KB
/
heap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package heap
type arrayIf interface {
Swap(interface{}, interface{})
Len() int
Key(interface{}) int
Value(interface{}) interface{}
Pop() interface{}
Append(interface{})
Left(interface{}) interface{}
Right(interface{}) interface{}
Union(interface{}) interface{}
}
type binHeapArrayIf interface {
arrayIf
Last() interface{}
Head() interface{}
Prev(interface{}) interface{}
Next(interface{}) interface{}
Valid(interface{}) bool
Parent(interface{}) interface{}
}
type heapIf interface {
arrayIf
}
type binHeapIf interface {
arrayIf
MaxHeaplify(interface{})
BuildHeap()
}
type heap struct {
binHeapArrayIf
}
func (h *heap) MaxHeaplify(i interface{}) {
largest, largestIdx := h.Key(i), i
if h.Valid(h.Left(i)) && h.Key(h.Left(i)) > largest {
largest, largestIdx = h.Key(h.Left(i)), h.Left(i)
}
if h.Valid(h.Right(i)) && h.Key(h.Right(i)) > largest {
_, largestIdx = h.Key(h.Right(i)), h.Right(i)
}
if i != largestIdx {
h.Swap(largestIdx, i)
h.MaxHeaplify(largestIdx)
}
}
func (h *heap) BuildHeap() {
for i := h.Last(); h.Valid(i); i = h.Prev(i) {
h.MaxHeaplify(i)
}
}
func (h *heap) Pop() (i interface{}) {
h.Swap(h.Head(), h.Last())
i = h.binHeapArrayIf.Pop()
if h.Len() > 0 {
h.MaxHeaplify(h.Head())
}
return
}
func (h *heap) Append(i interface{}) {
h.binHeapArrayIf.Append(i)
for idx := h.Last(); h.Valid(h.Parent(idx)) && h.Key(idx) > h.Key(h.Parent(idx)); {
h.Swap(idx, h.Parent(idx))
idx = h.Parent(idx)
}
}