Added tests and docstrings to fibonacci_heap.py

This commit is contained in:
mcawezome 2024-11-18 12:12:17 +08:00
parent 071ce716e4
commit bd943b086a

View File

@ -12,7 +12,6 @@ Operations supported:
- Merge: O(1) - Merge: O(1)
""" """
class Node: class Node:
""" """
A node in a Fibonacci heap. A node in a Fibonacci heap.
@ -29,7 +28,6 @@ class Node:
degree: Number of children. degree: Number of children.
mark: Boolean indicating if node has lost a child. mark: Boolean indicating if node has lost a child.
""" """
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
self.parent = None self.parent = None
@ -233,7 +231,7 @@ class FibonacciHeap:
This is an internal method that maintains the heap's structure. This is an internal method that maintains the heap's structure.
""" """
max_degree = int(self.size**0.5) + 1 max_degree = int(self.size ** 0.5) + 1
degree_table = [None] * max_degree degree_table = [None] * max_degree
# Collect all roots # Collect all roots
@ -273,11 +271,11 @@ class FibonacciHeap:
def decrease_key(self, node, new_val): def decrease_key(self, node, new_val):
""" """
Decreases the value of a node. Decreases the value of a node.
Args: Args:
node: The node whose value should be decreased. node: The node whose value should be decreased.
new_val: The new value for the node. new_val: The new value for the node.
Raises: Raises:
ValueError: If new value is greater than current value. ValueError: If new value is greater than current value.
""" """
@ -301,9 +299,9 @@ class FibonacciHeap:
Args: Args:
node: Node to be cut. node: Node to be cut.
parent: Parent of the node to be cut. parent: Parent of the node to be cut.
""" """ """"""
Performs cascading cut operation. Performs cascading cut operation.
Args: Args:
node: Starting node for cascading cut. node: Starting node for cascading cut.
""" """
@ -326,7 +324,8 @@ class FibonacciHeap:
node: Starting node for cascading cut. node: Starting node for cascading cut.
""" """
if parent := node.parent: parent = node.parent
if parent:
if not node.mark: if not node.mark:
node.mark = True node.mark = True
else: else:
@ -360,5 +359,4 @@ class FibonacciHeap:
if __name__ == "__main__": if __name__ == "__main__":
import doctest import doctest
doctest.testmod() doctest.testmod()