MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / delete_min

Method delete_min

data_structures/heap/binomial_heap.py:256–357  ·  view source on GitHub ↗

delete min element and return it

(self)

Source from the content-addressed store, hash-verified

254 return self.size == 0
255
256 def delete_min(self):
257 """
258 delete min element and return it
259 """
260 # assert not self.isEmpty(), "Empty Heap"
261
262 # Save minimal value
263 min_value = self.min_node.val
264
265 # Last element in heap corner case
266 if self.size == 1:
267 # Update size
268 self.size = 0
269
270 # Update bottom root
271 self.bottom_root = None
272
273 # Update min_node
274 self.min_node = None
275
276 return min_value
277 # No right subtree corner case
278 # The structure of the tree implies that this should be the bottom root
279 # and there is at least one other root
280 if self.min_node.right is None:
281 # Update size
282 self.size -= 1
283
284 # Update bottom root
285 self.bottom_root = self.bottom_root.parent
286 self.bottom_root.left = None
287
288 # Update min_node
289 self.min_node = self.bottom_root
290 i = self.bottom_root.parent
291 while i:
292 if i.val < self.min_node.val:
293 self.min_node = i
294 i = i.parent
295 return min_value
296 # General case
297 # Find the BinomialHeap of the right subtree of min_node
298 bottom_of_new = self.min_node.right
299 bottom_of_new.parent = None
300 min_of_new = bottom_of_new
301 size_of_new = 1
302
303 # Size, min_node and bottom_root
304 while bottom_of_new.left:
305 size_of_new = size_of_new * 2 + 1
306 bottom_of_new = bottom_of_new.left
307 if bottom_of_new.val < min_of_new.val:
308 min_of_new = bottom_of_new
309 # Corner case of single root on top left path
310 if (not self.min_node.left) and (not self.min_node.parent):
311 self.size = size_of_new
312 self.bottom_root = bottom_of_new
313 self.min_node = min_of_new

Callers

nothing calls this directly

Calls 2

merge_heapsMethod · 0.95
BinomialHeapClass · 0.85

Tested by

no test coverage detected