Skip to content

Commit 534fa04

Browse files
author
Krishnan M
committed
updated the test values
1 parent 819f078 commit 534fa04

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public Node(int value) {
2929
public int getValue() {
3030
return value;
3131
}
32+
33+
3234
}
3335

3436
private Node root;
@@ -43,6 +45,14 @@ public BinarySearchTree() {
4345
public int height(Node node) {
4446
return node == null ? -1 : node.height;
4547
}
48+
49+
/**
50+
* Returns the root node of the BST.
51+
* Used for testing and internal inspection.
52+
*/
53+
public Node getRoot() {
54+
return root;
55+
}
4656

4757
/**
4858
* Checks if the BST is empty.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ void testPreOrderTraversal() {
7171
assertTrue(true, "Preorder traversal executed successfully");
7272
}
7373

74+
/**
75+
* Tests the height of the root node after inserting multiple values into the BST.
76+
* Ensures the height is calculated correctly for a balanced tree.
77+
*/
78+
@Test
79+
void testRootHeightAfterInsertions() {
80+
bst.populate(new int[]{30, 20, 40, 10, 25, 35, 50});
81+
int expectedHeight = 2; // Based on balanced tree structure
82+
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
83+
}
84+
7485
/**
7586
* Tests postorder traversal output.
7687
*/

0 commit comments

Comments
 (0)