Consider the BinaryTree class partially given below Add to t

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

Solution

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

Consider the BinaryTree class partially given below. Add to the BinaryTree class a method Public boolean equals(Object T) which would return true if \
Consider the BinaryTree class partially given below. Add to the BinaryTree class a method Public boolean equals(Object T) which would return true if \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site