Skip to content

Commit 369994d

Browse files
committed
Added type hints and warning fixes
1 parent fe95a31 commit 369994d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

data_structures/heap/fibonacci_heap.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
14
class Node:
25
"""A node in the Fibonacci heap.
36
@@ -27,7 +30,7 @@ class Node:
2730
True
2831
"""
2932

30-
def __init__(self, key) -> None:
33+
def __init__(self, key: float | None) -> None:
3134
self.key = key or None
3235
self.degree = 0
3336
self.marked = False
@@ -73,7 +76,7 @@ def __init__(self) -> None:
7376
self.min_node = Node(None)
7477
self.total_nodes = 0
7578

76-
def insert(self, key) -> Node:
79+
def insert(self, key: float | None) -> Node:
7780
"""Insert a new key into the heap.
7881
7982
Args:
@@ -105,7 +108,8 @@ def insert(self, key) -> Node:
105108
self.total_nodes += 1
106109
return new_node
107110

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:
109113
"""Insert node into circular linked list.
110114
111115
Args:
@@ -136,7 +140,7 @@ def _insert_into_circular_list(self, base_node, node_to_insert) -> Node:
136140
base_node.right = node_to_insert
137141
return base_node
138142

139-
def extract_min(self) -> float:
143+
def extract_min(self) -> float | None:
140144
"""Remove and return the minimum key from the heap.
141145
142146
This operation removes the node with the minimum key from the heap,
@@ -193,7 +197,7 @@ def extract_min(self) -> float:
193197
self.total_nodes -= 1
194198
return min_node.key
195199

196-
def _consolidate(self):
200+
def _consolidate(self) -> None:
197201
"""Consolidate the heap after removing the minimum node.
198202
199203
This internal method maintains the Fibonacci heap properties by combining
@@ -258,7 +262,7 @@ def _consolidate(self):
258262
):
259263
self.min_node = degree_table[degree]
260264

261-
def decrease_key(self, node, new_key):
265+
def decrease_key(self, node: Node, new_key: float | None) -> None:
262266
"""Decrease the key value of a given node.
263267
264268
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):
296300
if node.key < self.min_node.key:
297301
self.min_node = node
298302

299-
def _cut(self, child_node, parent_node):
303+
def _cut(self, child_node: Node, parent_node: Node) -> None:
300304
"""Cut a node from its parent and add it to the root list.
301305
302306
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):
325329
child_node.parent = Node(None)
326330
child_node.marked = False
327331

328-
def _cascading_cut(self, current_node) -> None:
332+
def _cascading_cut(self, current_node: Node) -> None:
329333
"""Perform cascading cut operation.
330334
331335
Args:
@@ -338,7 +342,7 @@ def _cascading_cut(self, current_node) -> None:
338342
self._cut(current_node, parent_node)
339343
self._cascading_cut(parent_node)
340344

341-
def delete(self, node) -> None:
345+
def delete(self, node: Node) -> None:
342346
"""Delete a node from the heap.
343347
344348
This operation removes a given node from the heap by first decreasing
@@ -365,7 +369,7 @@ def delete(self, node) -> None:
365369
self.decrease_key(node, float("-inf"))
366370
self.extract_min()
367371

368-
def find_min(self) -> float:
372+
def find_min(self) -> float | None:
369373
"""Return the minimum key without removing it from the heap.
370374
371375
This operation provides quick access to the minimum key in the heap
@@ -400,7 +404,7 @@ def is_empty(self) -> bool:
400404
"""
401405
return self.min_node.key is None
402406

403-
def merge(self, other_heap) -> None:
407+
def merge(self, other_heap: FibonacciHeap) -> None:
404408
"""Merge another Fibonacci heap into this one.
405409
406410
This operation combines two Fibonacci heaps by concatenating their

0 commit comments

Comments
 (0)