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