-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.lua
212 lines (173 loc) · 5.39 KB
/
heap.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local class = require 'vendor.middleclass'
local Heap = class('Heap')
local valid_types = {
min = true,
max = true,
}
-- Initializes the heap.
-- Type can be one of 'min' or 'max'. Defaults to 'min'.
-- Returns an instance of Heap.
function Heap:initialize(type)
self._nodes = {}
type = type or 'min'
self:setType(type)
end
-- Sets the type of heap and resets the order.
-- Can be one of min or max.
-- Returns nothing [nil]
function Heap:setType(type)
assert(valid_types[type], "type must be one of 'min' or 'max'")
self._type = type
self:reset()
end
-- Clears the heap
-- Returns nothing [nil]
function Heap:clear()
self._nodes = {}
end
-- Pops the first node off the heap.
-- Returns the node unpacked: weight first then payload.
function Heap:pop()
local node = self._nodes[1]
if node then
self._nodes[1] = self._nodes[#self._nodes]
table.remove(self._nodes, #self._nodes)
if not self:isEmpty() then
self:_percolateDown(1)
end
return node.weight, node.payload
else
return nil, nil
end
end
-- Fetch a node at the given index.
-- Returns the node unpacked: weight first then payload.
function Heap:fetch(index)
if self._nodes[index] then
return self._nodes[index].weight, self._nodes[index].payload
else
return nil, nil
end
end
-- Fetch the first node on the heap.
-- Returns the node unpacked: weight first then payload.
function Heap:first()
return self:fetch(1)
end
-- Inserts a node in the heap as a table { weight = weight, payload = payload }
-- Returns nothing [nil]
function Heap:insert(weight, payload)
self._nodes[#self._nodes + 1] = { weight = weight, payload = payload }
self:_percolateUp(#self._nodes)
end
-- Resets the order of the heap.
-- Returns nothing [nil]
function Heap:reset()
self:_percolateDown(1)
end
-- Gets the size of the heap (the number of nodes stored).
-- Returns the heap size [number]
function Heap:getSize()
return #self._nodes
end
-- Checks if the heap is empty.
-- Return true or false [boolean]
function Heap:isEmpty()
return #self._nodes == 0
end
-- Checks if each node in the heap is ordered correctly.
-- Returns true on success, false on error. [boolean]
function Heap:isValid()
if self:isEmpty() then return true end
for index, node in ipairs(self._nodes) do
local left_child_index = self:_leftChildIndex(index)
local right_child_index = self:_rightChildIndex(index)
if self._nodes[left_child_index] then
if not self.sort(self._nodes[index].weight, self._nodes[left_child_index].weight) then
return false
end
end
if self._nodes[right_child_index] then
if not self.sort(self._nodes[index].weight, self._nodes[right_child_index].weight) then
return false
end
end
end
return true
end
-- private methods
-- Default sorting function.
-- Used for Min-Heaps creation.
function Heap:_sort(a, b)
assert(valid_types[self._type], "type must be one of 'min' or 'max'")
if self._type == 'min' then
return a < b
elseif self._type == 'max' then
return a > b
end
end
-- Find the index of the given payload.
-- Returns the index [number]
function Heap:_indexOf(payload)
for i, node in ipairs(self._nodes) do
if node.payload == payload then return i end
end
end
-- Calculate the the parent index of the given index.
-- Returned index may not be a valid index in the heap.
-- Returns the parent index [number]
function Heap:_parentIndex(index)
return math.floor(index / 2)
end
-- Calculate the left child index of the given index.
-- Returned index may not be a valid index in the heap.
-- Returns the left child index [number]
function Heap:_leftChildIndex(index)
return 2 * index
end
-- Calculate the right child index of the given index.
-- Returned index may not be a valid index in the heap.
-- Returns the right child index [number]
function Heap:_rightChildIndex(index)
return 2 * index + 1
end
-- Percolates up the heap recursively, ordering each node by the chosen sort method.
-- Returns nothing [nil]
function Heap:_percolateUp(index)
local parent_index = self:_parentIndex(index)
if self._nodes[parent_index] and self:_sort(self._nodes[index].weight, self._nodes[parent_index].weight) then
self._nodes[parent_index], self._nodes[index] = self._nodes[index], self._nodes[parent_index]
self:_percolateUp(parent_index) -- Recursive call from the parent index
end
end
-- Percolates down the heap recursively, ordering each node by the chosen sort method.
-- Returns nothing [nil]
function Heap:_percolateDown(index)
local test_index = index
local left_index = self:_leftChildIndex(index)
local right_index = self:_rightChildIndex(index)
if self._nodes[left_index] and self:_sort(self._nodes[left_index].weight, self._nodes[test_index].weight) then
test_index = left_index
end
if self._nodes[right_index] and self:_sort(self._nodes[right_index].weight, self._nodes[test_index].weight) then
test_index = right_index
end
if test_index ~= index then
self._nodes[index], self._nodes[test_index] = self._nodes[test_index], self._nodes[index]
self:_percolateDown(test_index) -- Recursive call from the newly shifted index
end
end
-- metamethods
-- Merging heaps with the '+' operator.
-- Returns a new heap based on h1 + h2 [Heap]
function Heap.__add(h1, h2)
local h = Heap:new()
while not h1:isEmpty() do
h:insert(h1:pop())
end
while not h2:isEmpty() do
h:insert(h2:pop())
end
return h
end
return Heap