12
12
import unittest
13
13
from collections .abc import Iterator
14
14
15
+ import pytest
16
+
15
17
16
18
class Node :
17
19
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
78
80
node .right = self ._put (node .right , label , node )
79
81
else :
80
82
msg = f"Node with label { label } already exists"
81
- raise Exception (msg )
83
+ raise ValueError (msg )
82
84
83
85
return node
84
86
@@ -95,14 +97,14 @@ def search(self, label: int) -> Node:
95
97
>>> node = t.search(3)
96
98
Traceback (most recent call last):
97
99
...
98
- Exception : Node with label 3 does not exist
100
+ ValueError : Node with label 3 does not exist
99
101
"""
100
102
return self ._search (self .root , label )
101
103
102
104
def _search (self , node : Node | None , label : int ) -> Node :
103
105
if node is None :
104
106
msg = f"Node with label { label } does not exist"
105
- raise Exception (msg )
107
+ raise ValueError (msg )
106
108
else :
107
109
if label < node .label :
108
110
node = self ._search (node .left , label )
@@ -124,7 +126,7 @@ def remove(self, label: int) -> None:
124
126
>>> t.remove(3)
125
127
Traceback (most recent call last):
126
128
...
127
- Exception : Node with label 3 does not exist
129
+ ValueError : Node with label 3 does not exist
128
130
"""
129
131
node = self .search (label )
130
132
if node .right and node .left :
@@ -179,7 +181,7 @@ def exists(self, label: int) -> bool:
179
181
try :
180
182
self .search (label )
181
183
return True
182
- except Exception :
184
+ except ValueError :
183
185
return False
184
186
185
187
def get_max_label (self ) -> int :
@@ -190,15 +192,15 @@ def get_max_label(self) -> int:
190
192
>>> t.get_max_label()
191
193
Traceback (most recent call last):
192
194
...
193
- Exception : Binary search tree is empty
195
+ ValueError : Binary search tree is empty
194
196
195
197
>>> t.put(8)
196
198
>>> t.put(10)
197
199
>>> t.get_max_label()
198
200
10
199
201
"""
200
202
if self .root is None :
201
- raise Exception ("Binary search tree is empty" )
203
+ raise ValueError ("Binary search tree is empty" )
202
204
203
205
node = self .root
204
206
while node .right is not None :
@@ -214,15 +216,15 @@ def get_min_label(self) -> int:
214
216
>>> t.get_min_label()
215
217
Traceback (most recent call last):
216
218
...
217
- Exception : Binary search tree is empty
219
+ ValueError : Binary search tree is empty
218
220
219
221
>>> t.put(8)
220
222
>>> t.put(10)
221
223
>>> t.get_min_label()
222
224
8
223
225
"""
224
226
if self .root is None :
225
- raise Exception ("Binary search tree is empty" )
227
+ raise ValueError ("Binary search tree is empty" )
226
228
227
229
node = self .root
228
230
while node .left is not None :
@@ -359,7 +361,7 @@ def test_put(self) -> None:
359
361
assert t .root .left .left .parent == t .root .left
360
362
assert t .root .left .left .label == 1
361
363
362
- with self . assertRaises ( Exception ): # noqa: B017
364
+ with pytest . raises ( ValueError ):
363
365
t .put (1 )
364
366
365
367
def test_search (self ) -> None :
@@ -371,7 +373,7 @@ def test_search(self) -> None:
371
373
node = t .search (13 )
372
374
assert node .label == 13
373
375
374
- with self . assertRaises ( Exception ): # noqa: B017
376
+ with pytest . raises ( ValueError ):
375
377
t .search (2 )
376
378
377
379
def test_remove (self ) -> None :
@@ -517,7 +519,7 @@ def test_get_max_label(self) -> None:
517
519
assert t .get_max_label () == 14
518
520
519
521
t .empty ()
520
- with self . assertRaises ( Exception ): # noqa: B017
522
+ with pytest . raises ( ValueError ):
521
523
t .get_max_label ()
522
524
523
525
def test_get_min_label (self ) -> None :
@@ -526,7 +528,7 @@ def test_get_min_label(self) -> None:
526
528
assert t .get_min_label () == 1
527
529
528
530
t .empty ()
529
- with self . assertRaises ( Exception ): # noqa: B017
531
+ with pytest . raises ( ValueError ):
530
532
t .get_min_label ()
531
533
532
534
def test_inorder_traversal (self ) -> None :
0 commit comments