@@ -18,43 +18,84 @@ def bst():
18
18
def test_delete_empty_tree ():
19
19
"""
20
20
Tests the delete operation on an empty tree.
21
- Deleting a key from an empty BST should not cause any errors .
21
+ Deleting a key from an empty BST should return False .
22
22
"""
23
23
bst = BinarySearchTree ()
24
- bst .delete (10 )
25
- assert bst .root is None , "Deleting from an empty tree should do nothing."
24
+ assert not bst .delete (10 ), "Deleting from an empty tree should return False."
26
25
27
26
def test_delete_leaf_node (bst ):
28
27
"""
29
28
Tests the deletion of a leaf node (node with no children).
30
29
"""
31
- bst .delete (5 )
32
- assert bst .search (5 ) is None , "Leaf node 5 should be deleted."
30
+ assert bst .delete (5 ), "Leaf node 5 should be successfully deleted."
31
+ assert bst .search (5 ) is None , "Leaf node 5 should no longer be in the tree."
32
+
33
+ def test_delete_non_existent_key (bst ):
34
+ """
35
+ Tests deleting a key that does not exist in the BST.
36
+ Should return False without modifying the tree.
37
+ """
38
+ assert not bst .delete (100 ), "Deleting a non-existent key should return False."
39
+
40
+ def test_delete_non_integer (bst ):
41
+ """
42
+ Tests deleting a key that isn't an integer (string).
43
+ Should raise a TypeError.
44
+ """
45
+ with pytest .raises (TypeError ):
46
+ bst .delete ("100" )
47
+
48
+ def test_delete_none_key (bst ):
49
+ """
50
+ Tests deleting a None key.
51
+ Should raise a TypeError.
52
+ """
53
+ with pytest .raises (TypeError ):
54
+ bst .delete (None )
55
+
56
+ def test_delete_none_node (bst ):
57
+ """
58
+ Tests deleting a key that does not exist in the BST.
59
+ Should return False without modifying the tree.
60
+ """
61
+ bst = BinarySearchTree ()
62
+ assert not bst .delete (100 ), "Deleting a non-existent key should return False."
63
+
64
+ def test_delete_node_with_only_left_child ():
65
+ """
66
+ Tests deleting a node that has only a left child.
67
+ Ensures it reaches the `elif node.right is None: return node.left` condition.
68
+ """
69
+ bst = BinarySearchTree ()
70
+ bst .insert (10 )
71
+ bst .insert (5 ) # Left child only
72
+ assert bst .delete (10 ), "Node 10 should be successfully deleted."
73
+ assert bst .root .key == 5 , "Node 5 should replace node 10."
33
74
34
75
def test_delete_traverse_right_subtree (bst ):
35
76
"""
36
77
Tests the deletion of a node located in the right subtree.
37
78
"""
38
- bst .delete (30 )
39
- assert bst .search (30 ) is None , "Node 30 should be deleted ."
79
+ assert bst .delete (30 ), "Node 30 should be successfully deleted."
80
+ assert bst .search (30 ) is None , "Node 30 should no longer be in the tree ."
40
81
assert bst .root .right .key == 35 , "Node 35 should replace node 30 in the right subtree."
41
82
42
83
def test_delete_node_with_one_child (bst ):
43
84
"""
44
85
Tests the deletion of a node with only one child.
45
86
The child node should replace the deleted node.
46
87
"""
47
- bst .delete (10 )
48
- assert bst .search (10 ) is None , "Node 10 should be deleted ."
88
+ assert bst .delete (10 ), "Node 10 should be successfully deleted."
89
+ assert bst .search (10 ) is None , "Node 10 should no longer be in the tree ."
49
90
assert bst .root .left .key == 15 , "Node 15 should replace node 10."
50
91
51
92
def test_delete_node_with_two_children (bst ):
52
93
"""
53
94
Tests the deletion of a node that has two children.
54
95
The node should be replaced by its in-order successor.
55
96
"""
56
- bst .delete (20 )
57
- assert bst .search (20 ) is None , "Node 20 should be deleted ."
97
+ assert bst .delete (20 ), "Node 20 should be successfully deleted."
98
+ assert bst .search (20 ) is None , "Node 20 should no longer be in the tree ."
58
99
assert bst .root .key == 25 , "Root should now be replaced by in-order successor 25."
59
100
60
101
def test_delete_root_node ():
@@ -64,14 +105,14 @@ def test_delete_root_node():
64
105
"""
65
106
bst = BinarySearchTree ()
66
107
bst .insert (10 )
67
- bst .delete (10 )
68
- assert bst .search (10 ) is None , "Root node 10 should be deleted ."
108
+ assert bst .delete (10 ), "Root node 10 should be successfully deleted."
109
+ assert bst .search (10 ) is None , "Root node 10 should no longer be in the tree ."
69
110
70
111
def test_delete_complex_tree (bst ):
71
112
"""
72
113
Tests the deletion of a node in a complex tree structure.
73
114
Ensures the BST maintains correct structure after deletion.
74
115
"""
75
- bst .delete (20 ) # Deleting root with two children
76
- assert bst .search (20 ) is None , "Root 20 should be deleted ."
116
+ assert bst .delete (20 ), "Root 20 should be successfully deleted."
117
+ assert bst .search (20 ) is None , "Root 20 should no longer be in the tree ."
77
118
assert bst .root .key == 25 , "Root should now be replaced by in-order successor 25."
0 commit comments