1
+ from __future__ import annotations
2
+
3
+
1
4
class Node :
2
5
"""A node in the Fibonacci heap.
3
6
@@ -27,7 +30,7 @@ class Node:
27
30
True
28
31
"""
29
32
30
- def __init__ (self , key ) -> None :
33
+ def __init__ (self , key : float | None ) -> None :
31
34
self .key = key or None
32
35
self .degree = 0
33
36
self .marked = False
@@ -73,7 +76,7 @@ def __init__(self) -> None:
73
76
self .min_node = Node (None )
74
77
self .total_nodes = 0
75
78
76
- def insert (self , key ) -> Node :
79
+ def insert (self , key : float | None ) -> Node :
77
80
"""Insert a new key into the heap.
78
81
79
82
Args:
@@ -105,7 +108,8 @@ def insert(self, key) -> Node:
105
108
self .total_nodes += 1
106
109
return new_node
107
110
108
- def _insert_into_circular_list (self , base_node , node_to_insert ) -> Node :
111
+ @staticmethod
112
+ def _insert_into_circular_list (base_node : Node , node_to_insert : Node ) -> Node :
109
113
"""Insert node into circular linked list.
110
114
111
115
Args:
@@ -136,7 +140,7 @@ def _insert_into_circular_list(self, base_node, node_to_insert) -> Node:
136
140
base_node .right = node_to_insert
137
141
return base_node
138
142
139
- def extract_min (self ) -> float :
143
+ def extract_min (self ) -> float | None :
140
144
"""Remove and return the minimum key from the heap.
141
145
142
146
This operation removes the node with the minimum key from the heap,
@@ -193,7 +197,7 @@ def extract_min(self) -> float:
193
197
self .total_nodes -= 1
194
198
return min_node .key
195
199
196
- def _consolidate (self ):
200
+ def _consolidate (self ) -> None :
197
201
"""Consolidate the heap after removing the minimum node.
198
202
199
203
This internal method maintains the Fibonacci heap properties by combining
@@ -258,7 +262,7 @@ def _consolidate(self):
258
262
):
259
263
self .min_node = degree_table [degree ]
260
264
261
- def decrease_key (self , node , new_key ) :
265
+ def decrease_key (self , node : Node , new_key : float | None ) -> None :
262
266
"""Decrease the key value of a given node.
263
267
264
268
This operation updates the key of a node to a new, smaller value and
@@ -296,7 +300,7 @@ def decrease_key(self, node, new_key):
296
300
if node .key < self .min_node .key :
297
301
self .min_node = node
298
302
299
- def _cut (self , child_node , parent_node ) :
303
+ def _cut (self , child_node : Node , parent_node : Node ) -> None :
300
304
"""Cut a node from its parent and add it to the root list.
301
305
302
306
This is a helper method used in decrease_key operations. When a node's key
@@ -325,7 +329,7 @@ def _cut(self, child_node, parent_node):
325
329
child_node .parent = Node (None )
326
330
child_node .marked = False
327
331
328
- def _cascading_cut (self , current_node ) -> None :
332
+ def _cascading_cut (self , current_node : Node ) -> None :
329
333
"""Perform cascading cut operation.
330
334
331
335
Args:
@@ -338,7 +342,7 @@ def _cascading_cut(self, current_node) -> None:
338
342
self ._cut (current_node , parent_node )
339
343
self ._cascading_cut (parent_node )
340
344
341
- def delete (self , node ) -> None :
345
+ def delete (self , node : Node ) -> None :
342
346
"""Delete a node from the heap.
343
347
344
348
This operation removes a given node from the heap by first decreasing
@@ -365,7 +369,7 @@ def delete(self, node) -> None:
365
369
self .decrease_key (node , float ("-inf" ))
366
370
self .extract_min ()
367
371
368
- def find_min (self ) -> float :
372
+ def find_min (self ) -> float | None :
369
373
"""Return the minimum key without removing it from the heap.
370
374
371
375
This operation provides quick access to the minimum key in the heap
@@ -400,7 +404,7 @@ def is_empty(self) -> bool:
400
404
"""
401
405
return self .min_node .key is None
402
406
403
- def merge (self , other_heap ) -> None :
407
+ def merge (self , other_heap : FibonacciHeap ) -> None :
404
408
"""Merge another Fibonacci heap into this one.
405
409
406
410
This operation combines two Fibonacci heaps by concatenating their
0 commit comments