Consider the BinaryTree class partially given below. Add to the BinaryTree class
ID: 3925424 • Letter: C
Question
Consider the BinaryTree class partially given below. Add to the BinaryTree class a method Public boolean equals(Object T) which would return true if "this" tree is the same as T, otherwise it would return false.
public class BinaryTree{
public class TreeNode{
private data;
private TreeNode left;
private TreeNode right;
public TreeNode(E newData){
data = newData;
left = null;
right = null;
}
} // end class TreeNode
private TreeNode root;
public BinaryTree(){
root = null;
} // other methods
} // end class BinaryTree
Explanation / Answer
Here is the code for you:
public class BinaryTree{
public class TreeNode{
private data;
private TreeNode left;
private TreeNode right;
public TreeNode(E newData){
data = newData;
left = null;
right = null;
}
} // end class TreeNode
private TreeNode root;
public BinaryTree(){
root = null;
} // other methods
boolean equals(Object T)
{
if(T == null && data == null)
return true;
if(T == null)
return false;
if(data == null)
return false;
if(T.data != data)
return false;
return left.equals(T.left) && right.equals(T.right);
}
} // end class BinaryTree
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.