CC-5 : Check if Binary Tree is balanced.
Description:
A Binary Tree is balanced if difference of height between left and right subtree of each node is either 0 or 1. Given an input Binary Tree, check if it is balanced.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
|
Balanced? true
|
Pseudocode:
| Define a Boolean variable named balanced at the class level.
|
Method checkBalance(NodeTreeNode node):
| Method checkBalance(NodeTreeNode node)
|
| Define rightHeight=checkBalance(node.getRightChild()).
|
| If difference of leftHeight and rightHeight is >=2, Binary Tree is not balanced, set balanced false.
|
| If leftHeight > rightHeight return leftHeight from the method.
|
| If rightHeight > leftHeight return rightHeight from the method.
|
Code:
{
private boolean balanced=true;
public boolean checkBalanced(BinTreeNode root) throws Exception{
balanced=true;
checkBalance(root);
return balanced;
}
private int checkBalance(BinTreeNode node) {
if (node==null) {return 0;}
int leftHeight=checkBalance(node.getLeftChild())+1;
int rightHeight=checkBalance(node.getRightChild())+1;
if (Math.abs(leftHeight-rightHeight) >=2) {
balanced=false;
}
if (leftHeight > rightHeight ) {
return leftHeight;
}else {
return rightHeight;
}
}
}
Click here to download and run code and test cases !
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
Note: for easy reference BinTreeNode is included as internal class in program below. Ideally it should be in a separate file.
public class BinTreeCheckBalanced
{
private boolean balanced=true;
public boolean checkBalanced(BinTreeNode root) throws Exception{
balanced=true;
checkBalance(root);
return balanced;
}
private int checkBalance(BinTreeNode node) {
if (node==null) {return 0;}
int leftHeight=checkBalance(node.getLeftChild())+1;
int rightHeight=checkBalance(node.getRightChild())+1;
if (Math.abs(leftHeight-rightHeight) >=2) {
balanced=false;
}
if (leftHeight > rightHeight ) {
return leftHeight;
}else {
return rightHeight;
}
}
public static void main(String[] args) {
/***********************
Test cases given below
**********************/
boolean retVal=false;
try {
BinTreeCheckBalanced chk=new BinTreeCheckBalanced();
BinTreeNode root=chk.new BinTreeNode(4);
//Left L1 Children
root.setLeftChild(chk.new BinTreeNode(5));
root.setRightChild(chk.new BinTreeNode(7));
//Left L2 Children
root.getLeftChild().setLeftChild(chk.new BinTreeNode(6));
//root.getLeftChild().setRightChild(chk.new BinTreeNode(78));
//Left L3 Children
//root.getLeftChild().getLeftChild().setLeftChild(chk.new BinTreeNode(33));
//Right L2 Children
root.getRightChild().setLeftChild(chk.new BinTreeNode(14));
//Right L3 Children
//root.getRightChild().getLeftChild().setLeftChild(chk.new BinTreeNode(8));
//root.getRightChild().getLeftChild().setRightChild(chk.new BinTreeNode(16));
//Right L3 Children
//root.getRightChild().getLeftChild().getRightChild().setLeftChild(chk.new BinTreeNode(18));
//Right L4 Children
//root.getRightChild().getLeftChild().getRightChild().getLeftChild().setLeftChild(chk.new BinTreeNode(9));
retVal=chk.checkBalanced(root);
System.out.println("Balanced? "+retVal);
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
exception.printStackTrace();
}
}
public class BinTreeNode {
private BinTreeNode leftNode=null;
private BinTreeNode rightNode=null;
private int nodeData=0;
public BinTreeNode(int data) {
nodeData=data;
}
public BinTreeNode getLeftChild() {
return leftNode;
}
public void setLeftChild(BinTreeNode leftNode) {
this.leftNode = leftNode;
}
public BinTreeNode getRightChild() {
return rightNode;
}
public void setRightChild(BinTreeNode rightNode) {
this.rightNode = rightNode;
}
public int getNodeData() {
return nodeData;
}
public void setNodeData(int nodeData) {
this.nodeData = nodeData;
}
}
}