Skip to content

Commit 0d25f15

Browse files
maciegmaibin
authored andcommitted
BAEL-3400 (eugenp#8113)
* BAEL-3400 * BALE-3400 | moved to algorithm-miscellaneous-5 * BAEL-3400 | added modifiers
1 parent bb2f6b3 commit 0d25f15

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
public class BalancedBinaryTree {
4+
5+
public static boolean isBalanced(Tree tree) {
6+
return isBalancedRecursive(tree, -1).isBalanced;
7+
}
8+
9+
private static Result isBalancedRecursive(Tree tree, int depth) {
10+
if (tree == null) {
11+
return new Result(true, -1);
12+
}
13+
14+
Result leftSubtreeResult = isBalancedRecursive(tree.left(), depth + 1);
15+
Result rightSubtreeResult = isBalancedRecursive(tree.right(), depth + 1);
16+
17+
boolean isBalanced = Math.abs(leftSubtreeResult.height - rightSubtreeResult.height) <= 1;
18+
boolean subtreesAreBalanced = leftSubtreeResult.isBalanced && rightSubtreeResult.isBalanced;
19+
int height = Math.max(leftSubtreeResult.height, rightSubtreeResult.height) + 1;
20+
21+
return new Result(isBalanced && subtreesAreBalanced, height);
22+
}
23+
24+
private static final class Result {
25+
private final boolean isBalanced;
26+
private final int height;
27+
28+
private Result(boolean isBalanced, int height) {
29+
this.isBalanced = isBalanced;
30+
this.height = height;
31+
}
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
public class Tree {
4+
private final int value;
5+
private final Tree left;
6+
private final Tree right;
7+
8+
public Tree(int value, Tree left, Tree right) {
9+
this.value = value;
10+
this.left = left;
11+
this.right = right;
12+
}
13+
14+
public int value() {
15+
return value;
16+
}
17+
18+
public Tree left() {
19+
return left;
20+
}
21+
22+
public Tree right() {
23+
return right;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("[%s, %s, %s]",
29+
value,
30+
left == null ? "null" : left.toString(),
31+
right == null ? "null" : right.toString()
32+
);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertFalse;
6+
7+
public class BalancedBinaryTreeUnitTest extends BinaryTreeDataProvider {
8+
9+
@Test
10+
public void givenBalancedTrees_whenCallingIsBalanced_ShouldReturnTrue() {
11+
for (Tree tree : balancedTrees()) {
12+
assertTrue(toString(tree) + " should be balanced", BalancedBinaryTree.isBalanced(tree));
13+
}
14+
}
15+
16+
@Test
17+
public void givenUnbalancedTrees_whenCallingIsBalanced_ShouldReturnFalse() {
18+
for (Tree tree : unbalancedTrees()) {
19+
assertFalse(toString(tree) + " should not be balanced", BalancedBinaryTree.isBalanced(tree));
20+
}
21+
}
22+
23+
private String toString(Tree tree) {
24+
return tree != null ? tree.toString() : "null";
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.algorithms.balancedbinarytree;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
class BinaryTreeDataProvider {
7+
8+
static Collection<Tree> balancedTrees() {
9+
return Arrays.asList(
10+
null,
11+
leaf(1),
12+
tree(1, leaf(2), leaf(3)),
13+
tree(
14+
1,
15+
leaf(2),
16+
tree(3, leaf(4), null)
17+
),
18+
tree(
19+
1,
20+
tree(
21+
2,
22+
tree(3, leaf(4), null),
23+
leaf(5)
24+
),
25+
tree(
26+
6,
27+
leaf(7),
28+
tree(8, null, leaf(9))
29+
)
30+
)
31+
);
32+
}
33+
34+
static Collection<Tree> unbalancedTrees() {
35+
return Arrays.asList(
36+
tree(
37+
1,
38+
tree(2, leaf(3), null),
39+
null
40+
),
41+
tree(
42+
1,
43+
tree(
44+
2,
45+
tree(3, leaf(4), leaf(5)),
46+
null
47+
),
48+
tree(6, leaf(7), null)
49+
),
50+
tree(
51+
1,
52+
tree(2, leaf(3), null),
53+
tree(
54+
4,
55+
tree(5, leaf(6), leaf(7)),
56+
null
57+
)
58+
)
59+
);
60+
}
61+
62+
private static Tree leaf(int value) {
63+
return new Tree(value, null, null);
64+
}
65+
66+
private static Tree tree(int value, Tree left, Tree right) {
67+
return new Tree(value, left, right);
68+
}
69+
}

0 commit comments

Comments
 (0)