-
Notifications
You must be signed in to change notification settings - Fork 121
/
linkedHeap.go
173 lines (154 loc) · 3.96 KB
/
linkedHeap.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package heap
type linkedHeapListElement struct {
parent, left, right, next, prev *linkedHeapListElement
Value interface{}
}
type linkedHeapList struct {
root linkedHeapListElement
len int
}
type linkedHeap struct {
linkedHeapList
heap
}
func (h *linkedHeapList) Init() {
h.root.prev = &h.root
h.root.next = &h.root
h.root.parent = &h.root
h.root.left = &h.root
h.root.right = &h.root
h.len = 0
}
func (h *linkedHeapList) Parent(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.parent
}
func (h *linkedHeapList) Left(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.left
}
func (h *linkedHeapList) Right(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.right
}
func (h *linkedHeapList) Prev(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.prev
}
func (h *linkedHeapList) Next(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.next
}
func (h *linkedHeapList) Last() interface{} {
return h.root.prev
}
func (h *linkedHeapList) Head() interface{} {
return h.root.next
}
func (h *linkedHeapList) Valid(i interface{}) bool {
iE := i.(*linkedHeapListElement)
return iE != &h.root && iE != nil
}
func (h *linkedHeapList) Swap(i interface{}, j interface{}) {
iE := i.(*linkedHeapListElement)
jE := j.(*linkedHeapListElement)
iE.Value, jE.Value = jE.Value, iE.Value
}
func (h *linkedHeapList) Key(i interface{}) int {
iE := i.(*linkedHeapListElement)
return iE.Value.(int)
}
func (h *linkedHeapList) Value(i interface{}) interface{} {
iE := i.(*linkedHeapListElement)
return iE.Value
}
func (h *linkedHeapList) Len() int {
return h.len
}
func (h *linkedHeapList) Pop() (i interface{}) {
last := h.Last().(*linkedHeapListElement)
lastParent := last.parent
lastPrev := last.prev
lastPrev.next = &h.root
h.root.prev = lastPrev
if lastParent.left == last {
lastParent.left = nil
} else {
lastParent.right = nil
}
h.len--
return last.Value
}
func (h *linkedHeapList) Append(i interface{}) {
newE := new(linkedHeapListElement)
newE.Value = i
newE.next = &h.root
last := h.Last().(*linkedHeapListElement)
lastParent := last.parent
last.next = newE
newE.prev = last
h.root.prev = newE
if lastParent.right == nil {
lastParent.right = newE
newE.parent = lastParent
} else {
lastParent.next.left = newE
newE.parent = lastParent.next
}
h.len++
}
//O(n)
func (h *linkedHeapList) Union(i interface{}) interface{} {
var midNode *linkedHeapListElement
_i := i.(*linkedHeapList)
if h.Len() > _i.Len() {
midNode = _i.Head().(*linkedHeapListElement)
h.Last().(*linkedHeapListElement).next = midNode
midNode.prev = h.Last().(*linkedHeapListElement)
h.root.prev = _i.Last().(*linkedHeapListElement)
_i.Last().(*linkedHeapListElement).next = &h.root
} else {
midNode := h.Head().(*linkedHeapListElement)
_i.Last().(*linkedHeapListElement).next = midNode
midNode.prev = _i.Last().(*linkedHeapListElement)
h.root.next = _i.Head().(*linkedHeapListElement)
_i.Head().(*linkedHeapListElement).prev = &h.root
}
for iNode := midNode; h.Valid(iNode); iNode = h.Next(iNode).(*linkedHeapListElement) {
iNode.parent = nil
iNode.left = nil
iNode.right = nil
prev := iNode.prev
prevParent := prev.parent
if prevParent.right == nil {
prevParent.right = iNode
iNode.parent = prevParent
} else {
prevParent.next.left = iNode
iNode.parent = prevParent.next
}
}
h.len += _i.Len()
return *h
}
func newLinkedHeap() *linkedHeap {
h := new(linkedHeap)
h.linkedHeapList = linkedHeapList{}
h.linkedHeapList.Init()
h.heap.binHeapArrayIf = &h.linkedHeapList
return h
}
//merge:O(n)
//rebuild:O(n)
//T(n)=O(n)
func (h *linkedHeap) Union(i interface{}) interface{} {
h.linkedHeapList = h.linkedHeapList.Union(&(i.(*linkedHeap).linkedHeapList)).(linkedHeapList)
h.heap.BuildHeap()
return h
}
func (h *linkedHeap) Pop() (i interface{}) {
return h.heap.Pop()
}
func (h *linkedHeap) Append(i interface{}) {
h.heap.Append(i)
}