Skip to content

Commit 1f0a6b6

Browse files
committed
Fix MyPy type annotation errors in splay_tree.py
- Add explicit type annotations for current and parent variables - Add type annotation for result lists in traversal methods - Add null check for parent before accessing its key - Fixes all 6 MyPy errors reported by pre-commit hooks
1 parent 2f5dbad commit 1f0a6b6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

data_structures/binary_tree/splay_tree.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ def insert(self, key: Any) -> None:
237237
self.root = node
238238
return
239239

240-
current = self.root
241-
parent = None
240+
current: Node | None = self.root
241+
parent: Node | None = None
242242

243243
# Standard BST insertion
244244
while current is not None:
@@ -253,7 +253,7 @@ def insert(self, key: Any) -> None:
253253
return
254254

255255
node.parent = parent
256-
if key < parent.key:
256+
if parent is not None and key < parent.key:
257257
parent.left = node
258258
else:
259259
parent.right = node
@@ -291,8 +291,8 @@ def search(self, key: Any) -> bool:
291291
if self.root is None:
292292
return False
293293

294-
current = self.root
295-
parent = None
294+
current: Node | None = self.root
295+
parent: Node | None = None
296296

297297
while current is not None:
298298
if key == current.key:
@@ -448,7 +448,7 @@ def inorder(self) -> list[Any]:
448448
>>> tree.inorder()
449449
[3, 5, 7, 10, 15]
450450
"""
451-
result = []
451+
result: list[Any] = []
452452
self._inorder_helper(self.root, result)
453453
return result
454454

@@ -476,7 +476,7 @@ def preorder(self) -> list[Any]:
476476
>>> sorted(tree.preorder()) # Tree structure varies after inserts
477477
[5, 10, 15]
478478
"""
479-
result = []
479+
result: list[Any] = []
480480
self._preorder_helper(self.root, result)
481481
return result
482482

0 commit comments

Comments
 (0)