Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote