-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpriority_queue.go
75 lines (58 loc) · 1.34 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
package priority_queue
import (
"github.com/xiaomeng79/go-algorithm/data-structures/heap"
"github.com/xiaomeng79/go-algorithm/data-structures/queue"
)
//优先队列与队列区别:按照优先级自动排序
//抽象的数据类型,具有队列的所有特性,包括基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的
type Item struct {
Value interface{}
Priority int
}
func NewItem(value interface{}, priority int) (i *Item) {
return &Item{
Value: value,
Priority: priority,
}
}
func (x Item) Less(than heap.Item) bool {
return x.Priority < than.(Item).Priority
}
type PQ struct {
data heap.Heap
}
func NewMax() (q *PQ) {
return &PQ{
data: *heap.NewMax(),
}
}
func NewMin() (q *PQ) {
return &PQ{
data: *heap.NewMin(),
}
}
func (pq *PQ) Len() int {
return pq.data.Len()
}
func (pq *PQ) Insert(el Item) {
pq.data.Insert(heap.Item(el))
}
func (pq *PQ) Extract() (el Item) {
return pq.data.Extract().(Item)
}
func (pq *PQ) ChangePriority(val interface{}, priority int) {
var storage = queue.New()
popped := pq.Extract()
for val != popped.Value {
if pq.Len() == 0 {
panic("Item not found")
}
storage.Push(popped)
popped = pq.Extract()
}
popped.Priority = priority
pq.data.Insert(popped)
for storage.Len() > 0 {
pq.data.Insert(storage.Shift().(heap.Item))
}
}