CC-6 : Check Binary Tree Equality.
Description:
Two Binary Tree are equal if they have the same structure of nodes and value of corresponding nodes is also the same. Given two input Binary Trees, check if the same are equal /Identical.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
|
Are two trees equal: false
|
Pseudocode:
Method equality(root1, root2)
| If node data of root 1 and root 1 and root 2 is different return false.
|
| Recursively call method checkEquality () with left child of root1 and left child of root2. If return from these recursive calls is false, return false.
|
| Recursively call method checkEquality () with right child of root1 and right child of root2. If return from these recursive calls is false, return false.
|
Code:
public boolean checkEquality(BinTreeNode root1, BinTreeNode root2) {
if ((root1==null)&&(root2==null)){
return true;
}
if ((root1==null)||(root2==null)){
return false;
}
return ((root1.getNodeData()==root2.getNodeData())
&&checkEquality(root1.getLeftChild(), root2.getLeftChild())
&& checkEquality(root1.getRightChild(), root2.getRightChild()));
}
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 BinTreeCheckEquality {
public boolean checkEquality(BinTreeNode root1, BinTreeNode root2) {
if ((root1==null)&&(root2==null)){
return true;
}
if ((root1==null)||(root2==null)){
return false;
}
return ((root1.getNodeData()==root2.getNodeData())
&&checkEquality(root1.getLeftChild(), root2.getLeftChild())
&& checkEquality(root1.getRightChild(), root2.getRightChild()));
}
public static void main(String[] args) {
/************************
Test cases given below:
***********************/
boolean retVal;
try {
BinTreeCheckEquality eql=new BinTreeCheckEquality();
BinTreeNode root=eql.new BinTreeNode(4);
//Left L1 Children
root.setLeftChild(eql.new BinTreeNode(5));
root.setRightChild(eql.new BinTreeNode(7));
//Left L2 Children
root.getLeftChild().setLeftChild(eql.new BinTreeNode(6));
root.getLeftChild().setRightChild(eql.new BinTreeNode(78));
//Left L3 Children
root.getLeftChild().getLeftChild().setLeftChild(eql.new BinTreeNode(33));
//Right L2 Children
root.getRightChild().setLeftChild(eql.new BinTreeNode(14));
//Right L3 Children
root.getRightChild().getLeftChild().setLeftChild(eql.new BinTreeNode(8));
root.getRightChild().getLeftChild().setRightChild(eql.new BinTreeNode(16));
//Right L3 Children
root.getRightChild().getLeftChild().getRightChild().setLeftChild(eql.new BinTreeNode(18));
//Right L4 Children
root.getRightChild().getLeftChild().getRightChild().getLeftChild().setLeftChild(eql.new BinTreeNode(9));
retVal=eql.checkEquality(root, root);
System.out.println("Are two trees equal: "+retVal);
BinTreeNode root2=eql.new BinTreeNode(4);
//Left L1 Children
root2.setLeftChild(eql.new BinTreeNode(5));
root2.setRightChild(eql.new BinTreeNode(7));
//Left L2 Children
root2.getLeftChild().setLeftChild(eql.new BinTreeNode(6));
root2.getLeftChild().setRightChild(eql.new BinTreeNode(78));
//Left L3 Children
root2.getLeftChild().getLeftChild().setLeftChild(eql.new BinTreeNode(33));
retVal=eql.checkEquality(root, root2);
System.out.println("Are two trees equal: "+retVal);
BinTreeNode root3=eql.new BinTreeNode(4);
//Left L1 Children
root3.setLeftChild(eql.new BinTreeNode(5));
root3.setRightChild(eql.new BinTreeNode(7));
//Left L2 Children
root3.getLeftChild().setLeftChild(eql.new BinTreeNode(6));
root3.getLeftChild().setRightChild(eql.new BinTreeNode(78));
//Left L3 Children
root3.getLeftChild().getLeftChild().setLeftChild(eql.new BinTreeNode(33));
//root3.getLeftChild().getLeftChild().setRightChild(new BinTreeNode(66));
retVal=eql.checkEquality(root2, root3);
System.out.println("Are two trees equal: "+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;
}
}
}