Skip to content

Commit 996eab9

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

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ void setUp() {
2828
bst.populate(values);
2929
}
3030

31+
/**
32+
* Verifies that the BST is not empty after insertion.
33+
*/
3134
@Test
3235
void testBSTIsNotEmpty() {
3336
assertFalse(bst.isEmpty(), "BST should not be empty after insertion");
3437
}
3538

39+
/**
40+
* Tests the height calculation of the root node.
41+
*/
3642
@Test
3743
void testRootHeight() {
3844
int expectedHeight = 2;
3945
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
4046
}
4147

48+
/**
49+
* Tests the populateSorted method to ensure the BST is balanced
50+
* when populated with a sorted array.
51+
*/
4252
@Test
4353
void testPopulateSortedAndBalanced() {
4454
BinarySearchTree sortedBST = new BinarySearchTree();
@@ -47,52 +57,78 @@ void testPopulateSortedAndBalanced() {
4757
assertTrue(sortedBST.balanced(), "BST should be balanced after populateSorted");
4858
}
4959

60+
/**
61+
* Unit test for verifying the height calculation of nodes in the BinarySearchTree.
62+
* This test inserts three nodes and checks the height of the left child of the root.
63+
*/
5064
@Test
5165
void testHeightCalculation() {
5266
BinarySearchTree localBST = new BinarySearchTree();
5367
localBST.insert(10); // Root
5468
localBST.insert(5); // Left child
5569
localBST.insert(15); // Right child
56-
5770
assertEquals(0, localBST.height(localBST.getRoot().getLeft()), "Left child height should be 0");
5871
}
59-
72+
73+
/**
74+
* Tests if the BST is balanced.
75+
*/
6076
@Test
6177
void testBalancedTree() {
6278
assertTrue(bst.balanced(), "BST should be balanced with given values");
6379
}
6480

81+
/**
82+
* Tests inorder traversal output.
83+
*/
6584
@Test
6685
void testInOrderTraversal() {
6786
bst.inOrder(); // Output can be redirected and verified if needed
6887
assertTrue(true, "Inorder traversal executed successfully");
6988
}
7089

90+
/**
91+
* Tests preorder traversal output.
92+
*/
7193
@Test
7294
void testPreOrderTraversal() {
7395
bst.preOrder();
7496
assertTrue(true, "Preorder traversal executed successfully");
7597
}
7698

99+
/**
100+
* Tests the height of the root node after inserting multiple values into the BST.
101+
* Ensures the height is calculated correctly for a balanced tree.
102+
*/
77103
@Test
78104
void testRootHeightAfterInsertions() {
79105
bst.populate(new int[] {30, 20, 40, 10, 25, 35, 50});
80106
int expectedHeight = 2;
81107
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
82108
}
83109

110+
/**
111+
* Tests postorder traversal output.
112+
*/
84113
@Test
85114
void testPostOrderTraversal() {
86115
bst.postOrder();
87116
assertTrue(true, "Postorder traversal executed successfully");
88117
}
89118

119+
/**
120+
* Tests pretty display of the BST.
121+
*/
90122
@Test
91123
void testPrettyDisplay() {
92124
bst.prettyDisplay();
93125
assertTrue(true, "Pretty display executed successfully");
94126
}
95127

128+
/**
129+
* Tests insertion of negative values into the BST.
130+
* Verifies that the tree is not empty and remains balanced.
131+
*/
96132
@Test
97133
void testInsertNegativeValues() {
98134
BinarySearchTree negativeBST = new BinarySearchTree();
@@ -102,6 +138,11 @@ void testInsertNegativeValues() {
102138
assertTrue(negativeBST.balanced(), "BST with negative values should be balanced");
103139
}
104140

141+
/**
142+
* Tests insertion of duplicate values into the BST.
143+
* Verifies that the tree handles duplicates (either inserts or ignores them).
144+
* Note: Current BST implementation inserts duplicates to the right.
145+
*/
105146
@Test
106147
void testInsertDuplicateValues() {
107148
BinarySearchTree duplicateBST = new BinarySearchTree();
@@ -112,12 +153,15 @@ void testInsertDuplicateValues() {
112153
duplicateBST.inOrder(); // Visual check if needed
113154
assertTrue(true, "BST handled duplicate values (check logic if duplicates are allowed)");
114155
}
115-
156+
157+
/**
158+
* Tests balanced population using sorted array.
159+
*/
116160
@Test
117161
void testPopulateSorted() {
118162
int[] sortedValues = {10, 20, 30, 40, 50};
119163
BinarySearchTree sortedBST = new BinarySearchTree();
120164
sortedBST.populateSorted(sortedValues);
121165
assertTrue(sortedBST.balanced(), "BST populated with sorted array should be balanced");
122166
}
123-
}
167+
}

0 commit comments

Comments
 (0)