1
1
package com .thealgorithms .datastructures .trees ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
3
+ import static org .junit .jupiter .api .Assertions .*;
5
4
6
5
import org .junit .jupiter .api .Test ;
7
6
7
+ /**
8
+ * Unit tests for the BinaryTree class.
9
+ */
8
10
public class BinaryTreeTest {
9
11
10
12
@ Test
11
- void testInsertAndFind () {
13
+ public void testInsertAndFind () {
12
14
BinaryTree t = new BinaryTree ();
13
15
t .put (3 );
14
16
t .put (5 );
@@ -22,7 +24,7 @@ void testInsertAndFind() {
22
24
}
23
25
24
26
@ Test
25
- void testRemove () {
27
+ public void testRemove () {
26
28
BinaryTree t = new BinaryTree ();
27
29
t .put (3 );
28
30
t .put (5 );
@@ -34,24 +36,30 @@ void testRemove() {
34
36
t .remove (7 );
35
37
36
38
assertNotNull (t .getRoot (), "Root should not be null after removals" );
37
- assertEquals (9 , t .getRoot ().data , "Root value should be 9 after removals" );
39
+
40
+ // Check if root is not null before accessing its data
41
+ if (t .getRoot () != null ) {
42
+ assertEquals (9 , t .getRoot ().data , "Root value should be 9 after removals" );
43
+ } else {
44
+ fail ("Root should not be null after removals, but it is." );
45
+ }
38
46
}
39
47
40
48
@ Test
41
- void testRemoveReturnValue () {
49
+ public void testRemoveReturnValue () {
42
50
BinaryTree t = new BinaryTree ();
43
51
t .put (3 );
44
52
t .put (5 );
45
53
t .put (7 );
46
54
t .put (9 );
47
55
t .put (12 );
48
56
49
- assertEquals ( true , t .remove (9 ), "Removing existing node 9 should return true" );
50
- assertEquals ( false , t .remove (398745987 ), "Removing non-existing node should return false" );
57
+ assertTrue ( t .remove (9 ), "Removing existing node 9 should return true" );
58
+ assertFalse ( t .remove (398745987 ), "Removing non-existing node should return false" );
51
59
}
52
60
53
61
@ Test
54
- void testTraversalMethods () {
62
+ public void testTraversalMethods () {
55
63
BinaryTree t = new BinaryTree ();
56
64
t .put (3 );
57
65
t .put (5 );
@@ -66,7 +74,10 @@ void testTraversalMethods() {
66
74
t .postOrder (t .getRoot ());
67
75
68
76
// Ensure removal functionality is still working
69
- assertEquals (true , t .remove (9 ), "Removing existing node 9 should return true" );
70
- assertEquals (false , t .remove (398745987 ), "Removing non-existing node should return false" );
77
+ assertTrue (t .remove (9 ), "Removing existing node 9 should return true" );
78
+ assertFalse (t .remove (398745987 ), "Removing non-existing node should return false" );
79
+
80
+ // Check that the root is not null
81
+ assertNotNull (t .getRoot (), "Root should not be null after operations" );
71
82
}
72
83
}
0 commit comments