Skip to content

Commit 29aeb48

Browse files
committed
one more time
1 parent e403e1f commit 29aeb48

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.thealgorithms.datastructures.trees;
22

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.*;
54

65
import org.junit.jupiter.api.Test;
76

7+
/**
8+
* Unit tests for the BinaryTree class.
9+
*/
810
public class BinaryTreeTest {
911

1012
@Test
11-
void testInsertAndFind() {
13+
public void testInsertAndFind() {
1214
BinaryTree t = new BinaryTree();
1315
t.put(3);
1416
t.put(5);
@@ -22,7 +24,7 @@ void testInsertAndFind() {
2224
}
2325

2426
@Test
25-
void testRemove() {
27+
public void testRemove() {
2628
BinaryTree t = new BinaryTree();
2729
t.put(3);
2830
t.put(5);
@@ -34,24 +36,30 @@ void testRemove() {
3436
t.remove(7);
3537

3638
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+
}
3846
}
3947

4048
@Test
41-
void testRemoveReturnValue() {
49+
public void testRemoveReturnValue() {
4250
BinaryTree t = new BinaryTree();
4351
t.put(3);
4452
t.put(5);
4553
t.put(7);
4654
t.put(9);
4755
t.put(12);
4856

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");
5159
}
5260

5361
@Test
54-
void testTraversalMethods() {
62+
public void testTraversalMethods() {
5563
BinaryTree t = new BinaryTree();
5664
t.put(3);
5765
t.put(5);
@@ -66,7 +74,10 @@ void testTraversalMethods() {
6674
t.postOrder(t.getRoot());
6775

6876
// 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");
7182
}
7283
}

0 commit comments

Comments
 (0)