Skip to content

Commit 541f490

Browse files
albina-astrDebasish Biswas
andauthored
Valid BST: refactoring + added unit test (TheAlgorithms#3883)
Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
1 parent d565edc commit 541f490

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

src/main/java/com/thealgorithms/datastructures/trees/ValidBSTOrNot.java

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

3+
/**
4+
* This code recursively validates whether given Binary Search Tree (BST) is balanced or not.
5+
* Trees with only distinct values are supported.
6+
* Key points:
7+
* 1. According to the definition of a BST, each node in a tree must be in range [min, max],
8+
* where 'min' and 'max' values represent the child nodes (left, right).
9+
* 2. The smallest possible node value is Integer.MIN_VALUE, the biggest - Integer.MAX_VALUE.
10+
*/
311
public class ValidBSTOrNot {
4-
5-
class Node {
6-
7-
int data;
8-
Node left, right;
9-
10-
public Node(int item) {
11-
data = item;
12-
left = right = null;
13-
}
14-
}
15-
16-
// Root of the Binary Tree
17-
18-
/* can give min and max value according to your code or
19-
can write a function to find min and max value of tree. */
20-
21-
/* returns true if given search tree is binary
22-
search tree (efficient version) */
23-
boolean isBST(Node root) {
12+
public static boolean isBST(BinaryTree.Node root) {
2413
return isBSTUtil(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
2514
}
2615

27-
/* Returns true if the given tree is a BST and its
28-
values are >= min and <= max. */
29-
boolean isBSTUtil(Node node, int min, int max) {
30-
/* an empty tree is BST */
16+
private static boolean isBSTUtil(BinaryTree.Node node, int min, int max) {
17+
// empty tree is a BST
3118
if (node == null) {
3219
return true;
3320
}
3421

35-
/* false if this node violates the min/max constraints */
3622
if (node.data < min || node.data > max) {
3723
return false;
3824
}
3925

40-
/* otherwise check the subtrees recursively
41-
tightening the min/max constraints */
42-
// Allow only distinct values
4326
return (
4427
isBSTUtil(node.left, min, node.data - 1) &&
4528
isBSTUtil(node.right, node.data + 1, max)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
/**
9+
* @author Albina Gimaletdinova on 17/02/2023
10+
*/
11+
public class ValidBSTOrNotTest {
12+
@Test
13+
public void testRootNull() {
14+
assertTrue(ValidBSTOrNot.isBST(null));
15+
}
16+
17+
@Test
18+
public void testOneNode() {
19+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{Integer.MIN_VALUE});
20+
assertTrue(ValidBSTOrNot.isBST(root));
21+
}
22+
23+
/*
24+
9
25+
/ \
26+
7 13
27+
/\ / \
28+
3 8 10 20
29+
*/
30+
@Test
31+
public void testBinaryTreeIsBST() {
32+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 20});
33+
assertTrue(ValidBSTOrNot.isBST(root));
34+
}
35+
36+
/*
37+
9
38+
/ \
39+
7 13
40+
/\ / \
41+
3 8 10 13 <--- duplicated node
42+
*/
43+
@Test
44+
public void testBinaryTreeWithDuplicatedNodesIsNotBST() {
45+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 13});
46+
assertFalse(ValidBSTOrNot.isBST(root));
47+
}
48+
49+
/*
50+
9
51+
/ \
52+
7 13
53+
/\ / \
54+
3 8 10 12 <---- violates BST rule, needs to be more than 13 (parent node)
55+
*/
56+
@Test
57+
public void testBinaryTreeIsNotBST() {
58+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 12});
59+
assertFalse(ValidBSTOrNot.isBST(root));
60+
}
61+
}

0 commit comments

Comments
 (0)