11package com .thealgorithms .datastructures .trees ;
22
3- import static org .junit .jupiter .api .Assertions .*;
4-
3+ import org .junit .jupiter .api .Assertions ;
54import org .junit .jupiter .api .Test ;
65
76/**
@@ -11,73 +10,69 @@ public class BinaryTreeTest {
1110
1211 @ Test
1312 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 );
2019
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" );
2423 }
2524
2625 @ Test
2726 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 );
3936
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" );
4340 } 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." );
4542 }
4643 }
4744
4845 @ Test
4946 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 );
5653
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" );
5956 }
6057
6158 @ Test
6259 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 );
6966
7067 // 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 ());
7572
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" );
7975
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" );
8277 }
8378}
0 commit comments