Skip to content

Commit a36faba

Browse files
author
Krishnan M
committed
updated
1 parent abf0319 commit a36faba

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public Node(int value) {
2929
public int getValue() {
3030
return value;
3131
}
32+
33+
public Node getLeft() {
34+
return left;
35+
}
36+
37+
public Node getRight() {
38+
return right;
39+
}
40+
41+
public int getHeight() {
42+
return height;
43+
}
44+
3245

3346

3447
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ void testRootHeight() {
4343
assertEquals(expectedHeight, bst.height(bst.getRoot()), "Height of root node should be 2");
4444
}
4545

46+
/**
47+
* Tests the populateSorted method to ensure the BST is balanced
48+
* when populated with a sorted array.
49+
*/
50+
@Test
51+
public void testPopulateSortedAndBalanced() {
52+
BinarySearchTree bst = new BinarySearchTree();
53+
int[] sortedArray = {1, 2, 3, 4, 5, 6, 7};
54+
bst.populateSorted(sortedArray);
55+
assertTrue(bst.balanced());
56+
}
57+
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+
*/
62+
@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+
}
72+
73+
4674

4775
/**
4876
* Tests if the BST is balanced.

0 commit comments

Comments
 (0)