Skip to content

Commit 0f979bf

Browse files
author
Krishnan M
committed
updated
1 parent e1e7d5c commit 0f979bf

File tree

1 file changed

+27
-75
lines changed

1 file changed

+27
-75
lines changed

src/test/java/com/thealgorithms/tree/BinarySearchTree.java

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.thealgorithms.tree;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
46

57
import org.junit.jupiter.api.BeforeEach;
68
import org.junit.jupiter.api.Test;
@@ -26,141 +28,91 @@ void setUp() {
2628
bst.populate(values);
2729
}
2830

29-
/**
30-
* Verifies that the BST is not empty after insertion.
31-
*/
3231
@Test
3332
void testBSTIsNotEmpty() {
3433
assertFalse(bst.isEmpty(), "BST should not be empty after insertion");
3534
}
3635

37-
/**
38-
* Tests the height calculation of the root node.
39-
*/
4036
@Test
4137
void testRootHeight() {
4238
int expectedHeight = 2;
4339
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
4440
}
45-
46-
/**
47-
* Tests the populateSorted method to ensure the BST is balanced
48-
* when populated with a sorted array.
49-
*/
41+
5042
@Test
51-
public void testPopulateSortedAndBalanced() {
52-
BinarySearchTree bst = new BinarySearchTree();
43+
void testPopulateSortedAndBalanced() {
44+
BinarySearchTree sortedBST = new BinarySearchTree();
5345
int[] sortedArray = {1, 2, 3, 4, 5, 6, 7};
54-
bst.populateSorted(sortedArray);
55-
assertTrue(bst.balanced());
46+
sortedBST.populateSorted(sortedArray);
47+
assertTrue(sortedBST.balanced(), "BST should be balanced after populateSorted");
5648
}
5749

58-
/**
59-
* Unit test for verifying the height calculation of nodes in the BinarySearchTree.
60-
* This test inserts three nodes and checks the height of the left child of the root.
61-
*/
6250
@Test
63-
public void testHeightCalculation() {
64-
BinarySearchTree bst = new BinarySearchTree();
65-
bst.insert(10); // Root
66-
bst.insert(5); // Left child
67-
bst.insert(15); // Right child
68-
69-
// Verify height of left child of root
70-
assertEquals(0, bst.height(bst.getRoot().getLeft()));
71-
}
51+
void testHeightCalculation() {
52+
BinarySearchTree localBST = new BinarySearchTree();
53+
localBST.insert(10); // Root
54+
localBST.insert(5); // Left child
55+
localBST.insert(15); // Right child
7256

73-
57+
assertEquals(0, localBST.height(localBST.getRoot().getLeft()), "Left child height should be 0");
58+
}
7459

75-
/**
76-
* Tests if the BST is balanced.
77-
*/
7860
@Test
7961
void testBalancedTree() {
8062
assertTrue(bst.balanced(), "BST should be balanced with given values");
8163
}
8264

83-
/**
84-
* Tests inorder traversal output.
85-
*/
8665
@Test
8766
void testInOrderTraversal() {
88-
// Expected sorted order
89-
bst.inOrder(); // You can redirect System.out to capture output if needed
67+
bst.inOrder(); // Output can be redirected and verified if needed
9068
assertTrue(true, "Inorder traversal executed successfully");
9169
}
9270

93-
/**
94-
* Tests preorder traversal output.
95-
*/
9671
@Test
9772
void testPreOrderTraversal() {
9873
bst.preOrder();
9974
assertTrue(true, "Preorder traversal executed successfully");
10075
}
10176

102-
/**
103-
* Tests the height of the root node after inserting multiple values into the BST.
104-
* Ensures the height is calculated correctly for a balanced tree.
105-
*/
10677
@Test
10778
void testRootHeightAfterInsertions() {
108-
bst.populate(new int[]{30, 20, 40, 10, 25, 35, 50});
109-
int expectedHeight = 2; // Based on balanced tree structure
79+
bst.populate(new int[] {30, 20, 40, 10, 25, 35, 50});
80+
int expectedHeight = 2;
11081
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
11182
}
11283

113-
/**
114-
* Tests postorder traversal output.
115-
*/
11684
@Test
11785
void testPostOrderTraversal() {
11886
bst.postOrder();
11987
assertTrue(true, "Postorder traversal executed successfully");
12088
}
12189

122-
/**
123-
* Tests pretty display of the BST.
124-
*/
12590
@Test
12691
void testPrettyDisplay() {
12792
bst.prettyDisplay();
12893
assertTrue(true, "Pretty display executed successfully");
12994
}
130-
131-
/**
132-
* Tests insertion of negative values into the BST.
133-
* Verifies that the tree is not empty and remains balanced.
134-
*/
95+
13596
@Test
13697
void testInsertNegativeValues() {
137-
BinarySearchTree bst = new BinarySearchTree();
98+
BinarySearchTree negativeBST = new BinarySearchTree();
13899
int[] negativeValues = {-10, -20, -5, -15};
139-
bst.populate(negativeValues);
140-
assertFalse(bst.isEmpty(), "BST should not be empty after inserting negative values");
141-
assertTrue(bst.balanced(), "BST with negative values should be balanced");
100+
negativeBST.populate(negativeValues);
101+
assertFalse(negativeBST.isEmpty(), "BST should not be empty after inserting negative values");
102+
assertTrue(negativeBST.balanced(), "BST with negative values should be balanced");
142103
}
143104

144-
/**
145-
* Tests insertion of duplicate values into the BST.
146-
* Verifies that the tree handles duplicates (either inserts or ignores them).
147-
* Note: Current BST implementation inserts duplicates to the right.
148-
*/
149105
@Test
150106
void testInsertDuplicateValues() {
151-
BinarySearchTree bst = new BinarySearchTree();
107+
BinarySearchTree duplicateBST = new BinarySearchTree();
152108
int[] valuesWithDuplicates = {10, 20, 10, 30, 20};
153-
bst.populate(valuesWithDuplicates);
154-
assertFalse(bst.isEmpty(), "BST should not be empty after inserting duplicates");
109+
duplicateBST.populate(valuesWithDuplicates);
110+
assertFalse(duplicateBST.isEmpty(), "BST should not be empty after inserting duplicates");
155111

156-
// Optional: Check structure manually via traversal
157-
bst.inOrder(); // Output can be visually verified
112+
duplicateBST.inOrder(); // Visual check if needed
158113
assertTrue(true, "BST handled duplicate values (check logic if duplicates are allowed)");
159114
}
160115

161-
/**
162-
* Tests balanced population using sorted array.
163-
*/
164116
@Test
165117
void testPopulateSorted() {
166118
int[] sortedValues = {10, 20, 30, 40, 50};

0 commit comments

Comments
 (0)