1212import unittest
1313from collections .abc import Iterator
1414
15+ import pytest
16+
1517
1618class Node :
1719 def __init__ (self , label : int , parent : Node | None ) -> None :
@@ -78,7 +80,7 @@ def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Nod
7880 node .right = self ._put (node .right , label , node )
7981 else :
8082 msg = f"Node with label { label } already exists"
81- raise Exception (msg )
83+ raise ValueError (msg )
8284
8385 return node
8486
@@ -95,14 +97,14 @@ def search(self, label: int) -> Node:
9597 >>> node = t.search(3)
9698 Traceback (most recent call last):
9799 ...
98- Exception : Node with label 3 does not exist
100+ ValueError : Node with label 3 does not exist
99101 """
100102 return self ._search (self .root , label )
101103
102104 def _search (self , node : Node | None , label : int ) -> Node :
103105 if node is None :
104106 msg = f"Node with label { label } does not exist"
105- raise Exception (msg )
107+ raise ValueError (msg )
106108 else :
107109 if label < node .label :
108110 node = self ._search (node .left , label )
@@ -124,7 +126,7 @@ def remove(self, label: int) -> None:
124126 >>> t.remove(3)
125127 Traceback (most recent call last):
126128 ...
127- Exception : Node with label 3 does not exist
129+ ValueError : Node with label 3 does not exist
128130 """
129131 node = self .search (label )
130132 if node .right and node .left :
@@ -179,7 +181,7 @@ def exists(self, label: int) -> bool:
179181 try :
180182 self .search (label )
181183 return True
182- except Exception :
184+ except ValueError :
183185 return False
184186
185187 def get_max_label (self ) -> int :
@@ -190,15 +192,15 @@ def get_max_label(self) -> int:
190192 >>> t.get_max_label()
191193 Traceback (most recent call last):
192194 ...
193- Exception : Binary search tree is empty
195+ ValueError : Binary search tree is empty
194196
195197 >>> t.put(8)
196198 >>> t.put(10)
197199 >>> t.get_max_label()
198200 10
199201 """
200202 if self .root is None :
201- raise Exception ("Binary search tree is empty" )
203+ raise ValueError ("Binary search tree is empty" )
202204
203205 node = self .root
204206 while node .right is not None :
@@ -214,15 +216,15 @@ def get_min_label(self) -> int:
214216 >>> t.get_min_label()
215217 Traceback (most recent call last):
216218 ...
217- Exception : Binary search tree is empty
219+ ValueError : Binary search tree is empty
218220
219221 >>> t.put(8)
220222 >>> t.put(10)
221223 >>> t.get_min_label()
222224 8
223225 """
224226 if self .root is None :
225- raise Exception ("Binary search tree is empty" )
227+ raise ValueError ("Binary search tree is empty" )
226228
227229 node = self .root
228230 while node .left is not None :
@@ -359,7 +361,7 @@ def test_put(self) -> None:
359361 assert t .root .left .left .parent == t .root .left
360362 assert t .root .left .left .label == 1
361363
362- with self . assertRaises ( Exception ): # noqa: B017
364+ with pytest . raises ( ValueError ):
363365 t .put (1 )
364366
365367 def test_search (self ) -> None :
@@ -371,7 +373,7 @@ def test_search(self) -> None:
371373 node = t .search (13 )
372374 assert node .label == 13
373375
374- with self . assertRaises ( Exception ): # noqa: B017
376+ with pytest . raises ( ValueError ):
375377 t .search (2 )
376378
377379 def test_remove (self ) -> None :
@@ -517,7 +519,7 @@ def test_get_max_label(self) -> None:
517519 assert t .get_max_label () == 14
518520
519521 t .empty ()
520- with self . assertRaises ( Exception ): # noqa: B017
522+ with pytest . raises ( ValueError ):
521523 t .get_max_label ()
522524
523525 def test_get_min_label (self ) -> None :
@@ -526,7 +528,7 @@ def test_get_min_label(self) -> None:
526528 assert t .get_min_label () == 1
527529
528530 t .empty ()
529- with self . assertRaises ( Exception ): # noqa: B017
531+ with pytest . raises ( ValueError ):
530532 t .get_min_label ()
531533
532534 def test_inorder_traversal (self ) -> None :
0 commit comments