-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmin_pq.go
138 lines (111 loc) · 2.59 KB
/
min_pq.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pq
type MinPQ struct {
items []Item
n int
BinaryHeap
}
// factory method
func NewMinPQN(n int) *MinPQ {
items := make([]Item, n+1)
heap := NewBinaryHeap()
return &MinPQ{items, 0, heap}
}
func NewMinPQ() *MinPQ {
items := make([]Item, 1)
return &MinPQ{items, 0, NewBinaryHeap()}
}
// Implement interface PriorityQueue
func (pq *MinPQ) Insert(item Item) {
// double size of array if necessary
if pq.n == len(pq.items)-1 {
pq.resize(2 * len(pq.items))
}
// add item, and percolate it up to maintain heap invariant
pq.n++
lastLeaf := pq.GetLastLeafIndex(pq.n)
pq.items[lastLeaf] = item
pq.swim(lastLeaf, lastLeaf)
}
// Loop vs Recursive: almost the same
// see bench_test.go
func (pq *MinPQ) swim(child, max int) {
root := pq.GetRootIndex()
for child > root {
parent := pq.GetParentIndex(child, max)
if pq.isHigherPriority(parent, child) {
break
}
swap(pq.items, parent, child)
child = parent
}
}
// if pq.items[i] higher than pq.items[j]
func (pq *MinPQ) isHigherPriority(i, j int) bool {
i1 := pq.items[i]
i2 := pq.items[j]
// different from MaxPQ
return i1.CompareTo(i2) <= 0
}
func (pq *MinPQ) Delete() Item {
if pq.IsEmpty() {
panic("The priority queue is empty")
}
rootIdx := pq.GetRootIndex()
lastLeaf := pq.GetLastLeafIndex(pq.n)
item := pq.items[rootIdx]
swap(pq.items, rootIdx, lastLeaf)
pq.sink(rootIdx, lastLeaf-1)
pq.n--
if pq.n > 0 && pq.n == (len(pq.items)-1)/4 {
pq.resize(len(pq.items) / 2)
}
return item
}
func (pq *MinPQ) sink(parent, max int) {
for {
higherPriorityChild := pq.getHighPriorityChild(parent, max)
// if the left and right child do not exist
// stop sinking
if higherPriorityChild == -1 {
break
}
if pq.isHigherPriority(parent, higherPriorityChild) {
break
}
swap(pq.items, higherPriorityChild, parent)
parent = higherPriorityChild
}
}
func (pq *MinPQ) getHighPriorityChild(parent, max int) int {
leftChild := pq.GetLeftChildIndex(parent, max)
rightChild := pq.GetRightChildIndex(parent, max)
if leftChild != -1 && rightChild != -1 {
if pq.isHigherPriority(leftChild, rightChild) {
return leftChild
} else {
return rightChild
}
} else if leftChild != -1 {
return leftChild
} else if rightChild != -1 {
return rightChild
} else {
return -1
}
}
func (pq *MinPQ) IsEmpty() bool {
return pq.n == 0
}
func (pq *MinPQ) Size() int {
return pq.n
}
func (pq *MinPQ) resize(capacity int) {
t := make([]Item, capacity)
copy(t, pq.items)
pq.items = t
}
func (pq *MinPQ) GetItems() []Item {
begin := pq.GetRootIndex()
end := pq.GetLastLeafIndex(pq.n)
return pq.items[begin : end+1]
}