Answer:
// YourBinaryTree class implementation
public class YourBinaryTree extends BinaryTree
{
// equals method implementation
Override
public boolean equals(Object other)
{
// if other is null or not a BinaryTree, then return false
if (other == null || !(other instanceof BinaryTree))
{
return false;
}
// if both classes are not same, then return false
if(getClass() != other.getClass())
return false;
// caste other object to YourBinaryTree
YourBinaryTree otherBinaryTree = (YourBinaryTree) other;
// call the equals recursive helper method
return equals(root, otherBinaryTree.root);
} // end of equals method
// equals recursive helper method implementation
private boolean equals(Node root1, Node root2)
{
// return true if the current nodes of both trees are null
if(root1 == null && root2 == null)
{
return true;
}
// return false if one current node is null and another current node is not null
if((root1 != null && root2 == null) || (root1 == null && root2 != null))
{
return false;
}
// return false if the values at the current nods of both trees are not equal
if(!(root1.value.equals(root2.value)))
return false;
else
// recursive calls to the equals recursive helper method for left and right subtrees
return (equals(root1.left, root2.left) && equals(root1.right, root2.right));
} // end of equals recursive helper method
} // end of YourBinaryTree class
Step-by-step explanation:
This works only if you cast BinaryTree instead of YourBinaryTree in the first public boolean equals(Object other)
The program takes in or accepts public class called YourBinaryTree that extends BinaryTree, override public boolean equals(Object other), and implement it as described above. If other is null or not a BinaryTree you should return false.