Skip to content

Commit bd943b0

Browse files
committed
Added tests and docstrings to fibonacci_heap.py
1 parent 071ce71 commit bd943b0

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

data_structures/heap/fibonacci_heap.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
- Merge: O(1)
1313
"""
1414

15-
1615
class Node:
1716
"""
1817
A node in a Fibonacci heap.
@@ -29,7 +28,6 @@ class Node:
2928
degree: Number of children.
3029
mark: Boolean indicating if node has lost a child.
3130
"""
32-
3331
def __init__(self, val):
3432
self.val = val
3533
self.parent = None
@@ -233,7 +231,7 @@ def __consolidate(self):
233231
234232
This is an internal method that maintains the heap's structure.
235233
"""
236-
max_degree = int(self.size**0.5) + 1
234+
max_degree = int(self.size ** 0.5) + 1
237235
degree_table = [None] * max_degree
238236

239237
# Collect all roots
@@ -273,11 +271,11 @@ def __consolidate(self):
273271
def decrease_key(self, node, new_val):
274272
"""
275273
Decreases the value of a node.
276-
274+
277275
Args:
278276
node: The node whose value should be decreased.
279277
new_val: The new value for the node.
280-
278+
281279
Raises:
282280
ValueError: If new value is greater than current value.
283281
"""
@@ -301,9 +299,9 @@ def __cut(self, node, parent):
301299
Args:
302300
node: Node to be cut.
303301
parent: Parent of the node to be cut.
304-
""" """
302+
""""""
305303
Performs cascading cut operation.
306-
304+
307305
Args:
308306
node: Starting node for cascading cut.
309307
"""
@@ -326,7 +324,8 @@ def __cascading_cut(self, node):
326324
node: Starting node for cascading cut.
327325
"""
328326

329-
if parent := node.parent:
327+
parent = node.parent
328+
if parent:
330329
if not node.mark:
331330
node.mark = True
332331
else:
@@ -360,5 +359,4 @@ def print_tree(node, level=0):
360359

361360
if __name__ == "__main__":
362361
import doctest
363-
364362
doctest.testmod()

0 commit comments

Comments
 (0)