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