-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue.go
110 lines (95 loc) · 1.9 KB
/
priority_queue.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
package main
import (
"fmt"
)
type MaxPriorityQueue struct {
nums []int
}
func New(n []int) *MaxPriorityQueue {
q := &MaxPriorityQueue{
nums: n,
}
q.buildMaxHeap()
return q
}
func left(i int) int {
return 2 * i
}
func right(i int) int {
return 2*i + 1
}
func swap(a []int, i, j int) {
t := a[i]
a[i] = a[j]
a[j] = t
}
func (q *MaxPriorityQueue) maxHeapfy(ind int) {
l := left(ind)
r := right(ind)
largest := ind
// 我们知道叶子节点的开始位置,所以不对下标进行检查
if l < len(q.nums) && q.nums[l] > q.nums[largest] {
largest = l
}
if r < len(q.nums) && q.nums[r] > q.nums[largest] {
largest = r
}
if largest != ind {
swap(q.nums, ind, largest)
q.maxHeapfy(largest)
}
}
// buildMaxHeap 从无序的数组中构建出"Max Heap"
func (q *MaxPriorityQueue) buildMaxHeap() {
if len(q.nums) <= 1 {
return
}
// MaxHeap是从下标1开始的
for i := (len(q.nums) - 1) / 2; i >= 1; i-- {
q.maxHeapfy(i)
}
}
func (q *MaxPriorityQueue) Maximum() int {
return q.nums[1]
}
func (q *MaxPriorityQueue) ExtractMaximum() int {
// 第一个位置是占位
if len(q.nums) < 2 {
panic("heap underflow")
}
max := q.nums[1]
q.nums[1] = q.nums[len(q.nums)-1]
q.nums = q.nums[:len(q.nums)-1]
q.maxHeapfy(1)
return max
}
func (q *MaxPriorityQueue) IncreaseKey(i, key int) {
if i <= 1 || i > len(q.nums)-1 {
panic("index out of range")
}
if key < q.nums[i] {
panic("new key is smaller than current key")
}
q.nums[i] = key
for i > 1 && q.nums[i/2] < q.nums[i] {
swap(q.nums, i, i/2)
i = i / 2
}
}
func (q *MaxPriorityQueue) Insert(key int) {
q.nums = append(q.nums, key-1)
q.IncreaseKey(len(q.nums)-1, key)
}
func main() {
maxQueue := New([]int{1, 2, 3, 4, 5})
fmt.Println(maxQueue.Maximum())
fmt.Println(maxQueue.ExtractMaximum())
fmt.Println(maxQueue.Maximum())
maxQueue.Insert(20)
fmt.Println(maxQueue.Maximum())
// Output:
// 5
// 5
// 4
// 20
}