12
12
- Merge: O(1)
13
13
"""
14
14
15
+
15
16
class Node :
16
17
"""
17
18
A node in a Fibonacci heap.
@@ -28,6 +29,7 @@ class Node:
28
29
degree: Number of children.
29
30
mark: Boolean indicating if node has lost a child.
30
31
"""
32
+
31
33
def __init__ (self , val ):
32
34
self .val = val
33
35
self .parent = None
@@ -231,7 +233,7 @@ def __consolidate(self):
231
233
232
234
This is an internal method that maintains the heap's structure.
233
235
"""
234
- max_degree = int (self .size ** 0.5 ) + 1
236
+ max_degree = int (self .size ** 0.5 ) + 1
235
237
degree_table = [None ] * max_degree
236
238
237
239
# Collect all roots
@@ -271,11 +273,11 @@ def __consolidate(self):
271
273
def decrease_key (self , node , new_val ):
272
274
"""
273
275
Decreases the value of a node.
274
-
276
+
275
277
Args:
276
278
node: The node whose value should be decreased.
277
279
new_val: The new value for the node.
278
-
280
+
279
281
Raises:
280
282
ValueError: If new value is greater than current value.
281
283
"""
@@ -299,9 +301,9 @@ def __cut(self, node, parent):
299
301
Args:
300
302
node: Node to be cut.
301
303
parent: Parent of the node to be cut.
302
- """ """
304
+ """ """
303
305
Performs cascading cut operation.
304
-
306
+
305
307
Args:
306
308
node: Starting node for cascading cut.
307
309
"""
@@ -324,8 +326,7 @@ def __cascading_cut(self, node):
324
326
node: Starting node for cascading cut.
325
327
"""
326
328
327
- parent = node .parent
328
- if parent :
329
+ if parent := node .parent :
329
330
if not node .mark :
330
331
node .mark = True
331
332
else :
@@ -359,4 +360,5 @@ def print_tree(node, level=0):
359
360
360
361
if __name__ == "__main__" :
361
362
import doctest
363
+
362
364
doctest .testmod ()
0 commit comments