-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheap.go
96 lines (78 loc) · 1.72 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package heap
import "sync"
type Element interface {
LessThan(e Element) bool
}
type Heap struct {
sync.RWMutex
heap []Element
len int
isMin bool
}
func New(isMin bool) *Heap {
return &Heap{heap: make([]Element, 0), isMin: isMin}
}
func (h *Heap) IsEmpty() bool {
h.RLock()
defer h.RUnlock()
return len(h.heap) == 0
}
func (h *Heap) Len() int {
h.RLock()
defer h.RUnlock()
return h.len
}
func (h *Heap) Insert(e Element) {
h.Lock()
defer h.Unlock()
h.heap = append(h.heap, e)
h.len++
h.precolateUp(h.len - 1)
}
func (h *Heap) Extract() (e Element) {
h.Lock()
defer h.Unlock()
if h.len == 0 {
panic("Empty heap, cannot Extract.")
}
e, h.heap[0] = h.heap[0], h.heap[h.len-1]
h.heap = h.heap[:h.len-1]
h.len--
h.precolateDown(0)
return
}
func (h *Heap) Peek() (e Element) {
h.RLock()
defer h.RUnlock()
if h.len == 0 {
panic("Empty heap, cannot Peek.")
}
return h.heap[0]
}
func (h *Heap) precolateUp(index int) {
// 上滤,新元素在堆中上滤直到找出正确位置
needUp, parent := index, (index-1)>>1
for needUp > 0 && h.less(h.heap[needUp], h.heap[parent]) {
h.heap[parent], h.heap[needUp] = h.heap[needUp], h.heap[parent]
needUp, parent = parent, (parent-1)>>1
}
}
func (h *Heap) precolateDown(index int) {
// 下滤
for needDown, child := index, index<<1+1; needDown < h.len && child < h.len; needDown, child = child, child<<1+1 {
// find min(leftChild, rightChild)
if child+1 < h.len && h.less(h.heap[child+1], h.heap[child]) {
child++
}
if h.less(h.heap[needDown], h.heap[child]) {
break
}
h.heap[needDown], h.heap[child] = h.heap[child], h.heap[needDown]
}
}
func (h *Heap) less(a, b Element) bool {
if h.isMin {
return a.LessThan(b)
}
return b.LessThan(a)
}