|
10 | 10 | from __future__ import annotations |
11 | 11 |
|
12 | 12 | from sys import maxsize |
13 | | -from typing import Generic, TypeVar |
| 13 | +from typing import TypeVar # Keep only TypeVar import, remove Generic |
14 | 14 |
|
15 | 15 | T = TypeVar("T") |
16 | 16 |
|
@@ -47,7 +47,7 @@ def get_child_right_position(position: int) -> int: |
47 | 47 | return (2 * position) + 2 |
48 | 48 |
|
49 | 49 |
|
50 | | -class MinPriorityQueue(Generic[T]): |
| 50 | +class MinPriorityQueue[T]: # Updated: use square brackets for generic class |
51 | 51 | """ |
52 | 52 | Minimum Priority Queue Class |
53 | 53 |
|
@@ -90,7 +90,6 @@ def __init__(self) -> None: |
90 | 90 |
|
91 | 91 | def __len__(self) -> int: |
92 | 92 | return self.elements |
93 | | - |
94 | 93 | def __repr__(self) -> str: |
95 | 94 | return str(self.heap) |
96 | 95 |
|
@@ -156,35 +155,8 @@ def _bubble_down(self, elem: T) -> None: |
156 | 155 | _, child_left_weight = self.heap[child_left_position] |
157 | 156 | _, child_right_weight = self.heap[child_right_position] |
158 | 157 | if child_right_weight < child_left_weight and child_right_weight < weight: |
159 | | - self._swap_nodes(child_right_position, curr_pos) |
160 | | - return self._bubble_down(elem) |
161 | | - if child_left_position < self.elements: |
162 | | - _, child_left_weight = self.heap[child_left_position] |
163 | | - if child_left_weight < weight: |
164 | | - self._swap_nodes(child_left_position, curr_pos) |
165 | | - return self._bubble_down(elem) |
166 | | - else: |
167 | | - return None |
168 | | - if child_right_position < self.elements: |
169 | | - _, child_right_weight = self.heap[child_right_position] |
170 | | - if child_right_weight < weight: |
171 | | - self._swap_nodes(child_right_position, curr_pos) |
172 | | - return self._bubble_down(elem) |
173 | | - return None |
174 | | - |
175 | | - def _swap_nodes(self, node1_pos: int, node2_pos: int) -> None: |
176 | | - # Swap the nodes at the given positions |
177 | | - node1_elem = self.heap[node1_pos][0] |
178 | | - node2_elem = self.heap[node2_pos][0] |
179 | | - self.heap[node1_pos], self.heap[node2_pos] = ( |
180 | | - self.heap[node2_pos], |
181 | | - self.heap[node1_pos], |
182 | | - ) |
183 | | - self.position_map[node1_elem] = node2_pos |
184 | | - self.position_map[node2_elem] = node1_pos |
185 | | - |
186 | 158 |
|
187 | | -class GraphUndirectedWeighted(Generic[T]): |
| 159 | +class GraphUndirectedWeighted[T]: # Updated: use square brackets for generic class |
188 | 160 | """ |
189 | 161 | Graph Undirected Weighted Class |
190 | 162 |
|
@@ -217,7 +189,7 @@ def add_edge(self, node1: T, node2: T, weight: int) -> None: |
217 | 189 | self.connections[node2][node1] = weight |
218 | 190 |
|
219 | 191 |
|
220 | | -def prims_algo( |
| 192 | +def prims_algo[T]( # Updated: add type parameter for generic function |
221 | 193 | graph: GraphUndirectedWeighted[T], |
222 | 194 | ) -> tuple[dict[T, int], dict[T, T | None]]: |
223 | 195 | """ |
|
0 commit comments