-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeap.py
288 lines (213 loc) · 7.45 KB
/
MinHeap.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import math
"""
Class form of minimum heap.
"""
class MinHeap:
def __init__( self, list_A = [] ):
self.__heap_size = len(list_A)
self.__heap_A = list_A
if self.__heap_size > 0:
A = MinHeap.build_min_heap( self )
"""
Get the parent index of index i in a heap.
Input @para: index i.
Output i's parent index.
"""
def parent( self, i ):
# i >> 1 equals to i // 2
# >> is bit operation which means move x digit(s) to the right.
# i >> 1 will first transfer i to binary format, then move one digit to the right.
# "move one digit to the right" means "i // 2".
# "move n digits to the right" means "i // 2 ^ n", e.g. 1024 >> 10 is 1.
return i - 1 >> 1
"""
Get the left child index of index i in a heap.
Input @para: index i.
Output i's left index.
"""
def left( self, i ):
# i << 1 equals to i * 2
# << is bit operation which means move x digit(s) to the left.
# i << 1 will first transfer i to binary format, then move one digit to the left.
# "move one digit to the left" means "i * 2".
# "move n digits to the right" means "i * 2 ^ n", e.g. 1 << 10 is 1024.
return (i << 1) + 1
"""
Get the right child index of index i in a heap.
Input @para: index i.
Output i's right index.
"""
def right(self, i):
# i << 1 equals to i * 2
# << is bit operation which means move x digit(s) to the left.
# i << 1 will first transfer i to binary format, then move one digit to the left.
# "move one digit to the left" means "i * 2".
# "move n digits to the right" means "i * 2 ^ n", e.g. 1 << 10 is 1024.
return i + 1 << 1
"""
The recursive version of min_heapify.
Make sure that all the nodes in the subtree rooted at A[i] obey the min heap rule.
Start from node A[i], check A[left(i)] and A[right(i)],
Tha value of the parent node must be smaller than the value of its children nodes.
Input @para: list A and node index i.
Output @para: the maximum heapified list A from A[i].
"""
def min_heapify( self, A, i ):
# Get the initialized heap size.
heap_size = self.__heap_size
# Set sentinel
if i >= math.ceil(heap_size / 2):
return A
left_index = self.left(i)
right_index = self.right(i)
# Find the smallest index in i, left_index and right_index
if left_index < heap_size and A[left_index] < A[i]:
smallest = left_index
else:
smallest = i
if right_index < heap_size and A[right_index] < A[smallest]:
smallest = right_index
# Fix the node that doesn't obey to heap rule.
if smallest != i:
A[smallest], A[i] = A[i], A[smallest]
# recursively check the nodes below.
self.min_heapify( A, smallest )
return A
"""
Build a min heap from list A.
Input @para:
Output @para: the min heap A.
"""
def build_min_heap( self ):
# Get the initialized heap A.
A = self.__heap_A
# Build the min heap in a decreasing order.
for i in range(math.ceil(self.__heap_size / 2), -1, -1):
A = self.min_heapify( A, i )
# Return the new heap.
return A
"""
Heapsort
Input @para:
Output @para: the sorted A.
"""
def heapsort( self ):
# Initializing
heap_size = self.__heap_size
A = self.__heap_A
# Exchange the smallest element with the last element in the heap.
for i in range(heap_size - 1, 0, -1):
A[0], A[i] = A[i], A[0]
# Decreasing the heap size and update.
heap_size = heap_size - 1
self.__heap_size = heap_size
# Max heapify from the root.
self.min_heapify( A, 0 )
# Update parameters
self.__heap_A = []
self.__heap_size = 0
return A[-1: :-1]
"""
Get the minimum element in heap A.
Input @para:
Output @para: the minimum element in A (= the root of heap A).
"""
def min_node( self ):
# Load parameters
A = self.__heap_A
heap_size = self.__heap_size
# Sentinel.
assert self.__heap_size > 0, "heap is empty."
# return the root.
return A[0]
"""
Change the key of one node in the min heap A.
Input @para: heap A, index i of the node, key of the node, heap_size
Output @para: the new heap A after A[i] changed to key.
"""
def change_key( self, i, key ):
# Load parameters.
heap_size = self.__heap_size
A = self.__heap_A
# Decrease key.
if key <= A[i]:
# Bottom-top fixing.
while i > 0 and A[self.parent(i)] > key:
A[i] = A[self.parent(i)]
# Update the index.
i = self.parent(i)
A[i] = key
else: # Increase key
A[i] = key
# Top-bottom fixing.
A = self.min_heapify( A, i )
return A
"""
Insert a new element x into the min heap A.
Input @para: heap A, element x, heap_size
Output @para: the new heap A after x inserted.
"""
def insert_node( self, key ):
# Load parameters.
heap_size = self.__heap_size
A = self.__heap_A
# Add a new element in the end.
heap_size = heap_size + 1
A.append(float("inf"))
# Adjust the added element through its key.
self.change_key( heap_size - 1, key, )
# Update parameters
self.__heap_A = A
self.__heap_size = heap_size
return A
"""
Extract the minimum element in max heap A.
Input @para:
Output @para: the new heap A after extracted the max element.
"""
def extract( self ):
# Load parameters.
heap_size = self.__heap_size
A = self.__heap_A
# Sentinel
assert (heap_size > 0), "It is a empty heap."
# Extract the minimum element.
min_key = A[0]
# Update the min heap.
# Exchange the minimum element with the last element in the heap,
# then pop the minimum element.
A[0], A[heap_size - 1] = A[heap_size - 1], A[0]
# Decrease heap size by 1
heap_size = heap_size - 1
# Update heap size
self.__heap_size = heap_size
# Screening all elements to satisfied the max heap rule.
A = self.min_heapify( A, 0 )
# Update
self.__heap_A = A[:heap_size:]
return A[:heap_size:]
"""
Delete the ith node in the min heap A.
Input @para: index i of the node.
Output @para: the new heap A after deleted node A[i].
"""
def delete( self, i ):
# Load parameters.
heap_size = self.__heap_size
A = self.__heap_A
# Sentinel.
assert i < heap_size and i >= 0, "illegal index!"
# Exchange A[i] with A[heap_size - 1], then pop the last node.
A[i], A[heap_size - 1] = A[heap_size - 1], A[i]
# Update heap.
heap_size = heap_size - 1
self.__heap_size = heap_size
A = self.min_heapify( A, i )
# Update parameters
self.__heap_A = A[:heap_size:]
return A[:heap_size:]
"""
The print function of the instance of MaxHeap class.
"""
def __str__( self ):
return str( self.__heap_A )