Question
Please HELP me find the bug in my JAVA program!!! There are 3 classes: BinaryTree.java. BinaryTreeNode.java, and the last one is my JUNIT tests. I
Please HELP me find the bug in my JAVA program!!! There are 3 classes: BinaryTree.java. BinaryTreeNode.java, and the last one is my JUNIT tests. I cannot figure out why my equals method is not passing junits please help!!
public class BinaryTree
private BinaryTreeNode
public BinaryTree() {
this(null);
}
public BinaryTree(BinaryTreeNode
this.root = newRoot;
}
public BinaryTreeNode
return root;
}
public void setRoot(BinaryTreeNode
this.root = root;
}
@Override
public boolean equals(Object o) {
if (o instanceof BinaryTree) {
BinaryTree
if(this.root != null && test.getRoot() != null) { //both trees are not empty
return this.root.equals(test.getRoot());
}else if (this.root == null && test.getRoot() == null) { //both trees are empty
return true;
}
}
return false;
}
public BinaryTree
BinaryTree
ans.setRoot(this.getRoot().deepCopy());
return ans;
}
public BinaryTree
boolean left) {
BinaryTree
if(left) {
ans.setRoot(newRoot.deepCopy());
newRoot.setLeft(t.getRoot().deepCopy());
newRoot.setRight(this.getRoot().deepCopy());
} else {
ans.setRoot(newRoot.deepCopy());
newRoot.setRight(t.getRoot().deepCopy());
newRoot.setLeft(this.getRoot().deepCopy());
}
return ans;
}
public int size(){
if(this.getRoot() == null) {
return 0;
} else {
return this.getRoot().size();
}
}
public int height(){
if(this.getRoot() == null) {
return 0;
} else {
return this.getRoot().height();
}
}
public boolean full(){
if(this.getRoot() != null){
return this.getRoot().full();
}
return false;
}
public void mirror(){
if(this.getRoot() != null) {
this.getRoot().mirror();
}
}
public String inOrder(){
if(this.getRoot() != null) {
return this.getRoot().inOrder();
} else {
return null;
}
}
}
public class BinaryTreeNode
private BinaryTreeNode
private BinaryTreeNode
private T data;
public BinaryTreeNode(){
this(null,null,null);
}
public BinaryTreeNode(T theData){
this(theData,null,null);
}
public BinaryTreeNode(T theData, BinaryTreeNode
data = theData;
left = leftChild;
right = rightChild;
}
public int size(){
int size = 0; //the size of the tree
//The size of the tree rooted at this node is one more than the
//sum of the sizes of its children.
if(left != null){
size = size + left.size();
}
if(right != null){
size = size + right.size();
}
return size + 1; //add one to account for the current node
}
public BinaryTreeNode
return left;
}
public void setLeft(BinaryTreeNode
this.left = left;
}
public BinaryTreeNode
return right;
}
public void setRight(BinaryTreeNode
this.right = right;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode
BinaryTreeNode
ans.left = null;
ans.right = null;
ans.data = null;
if (this.getLeft() != null) {
ans.left = this.getLeft().deepCopy();
}
if (this.getRight() != null) {
ans.right = this.getRight().deepCopy();
}
if (this.getData() != null) {
ans.data = this.getData();
}
return ans;
}
@Override
public boolean equals(Object o){
// //Case:: Where both the objects are null
// if(this == o)
// return true;
//
// //Case:: Only other obj is null
// if(o == null)
// return false;
//
// //Case:: Objects are of different classes
// if(this.getClass() != o.getClass())
// return false;
//
// //Case:: Creating object of this class by parsing
// BinaryTreeNode
//
// //Case:: The data portion of the objects is not same
// if(this.data != other.data)
// return false;
//
// if(this.left != null && other.left != null){
// //Case:: Both the left links are different
// if(!this.left.equals(other.left))
// return false;
// }else if(this.left != null && other.left == null){
// return false ;
// }else if(this.left == null && other.left != null){
// return false;
// }
//
// if(this.right != null && other.left != null){
// //Case:: Both the right links are different
// if(!this.right.equals(other.right))
// return false;
// }else if(this.right != null && other.left == null){
// return false;
// }else if(this.right == null && other.left != null){
// return false;
// }
//
// return true;
// }
boolean retVal = false;
if(o instanceof BinaryTreeNode>) {
BinaryTreeNode> other = (BinaryTreeNode>) o;
if(!(this.getData().equals(other.getData()))) { //check data
return false; //if not equal, no need to check more
}
boolean leftEqual = false;
boolean rightEqual = false;
//if both left subtrees are not null..
if((this.getLeft() != null) && (other.getLeft() != null)) {
//..call equals on both left subtrees
leftEqual = this.getLeft().equals(other.getLeft());
}
//if both left subtrees ARE null..
if((this.getLeft() == null) && (other.getLeft() == null)) {
//..leftEqual is true
leftEqual = true;
}
//finally, evaluate both sides
//overall, the equality of the tree is the 'AND' of left & right
retVal = leftEqual && rightEqual;
}
return retVal;
}
public int height(){
if (this.left != null && this.right == null) {
return 1 + this.getLeft().height();
} else if (this.right != null && this.left == null) {
return 1 + this.getRight().height();
} else if (this.right != null && this.left != null) {
return 1 + Math.max(this.getRight().height(), this.getLeft().height());
} else if (this.right == null && this.left == null) {
return 1;
} else {
return 0;
}
}
public boolean full(){
if ((this.getLeft() == null) && (this.getRight() == null)) {
return true;
}
else if((this.getLeft() != null) && (this.getRight() != null)) {
return this.left.full() && this.right.full();
}
return false;
}
public void mirror(){
BinaryTreeNode
if (this.left == null && this.right != null) {
copy = this.getRight().deepCopy();
this.setLeft(copy);
this.setRight(null);
this.left.mirror();
} else if (this.left != null && this.right == null) {
copy = this.getLeft().deepCopy();
this.setLeft(null);
this.setRight(copy);
this.right.mirror();
} else if (this.left != null && this.right != null) {
copy = this.getLeft().deepCopy();
this.setLeft(this.getRight().deepCopy());
this.setRight(copy);
this.right.mirror();
this.left.mirror();
}
}
public String inOrder(){
// left, root, right
String ans = "";
if (this.left != null) {
ans = ans + this.getLeft().inOrder();
}
ans = ans + "(" + this.getData() + ")";
if (this.right != null) {
ans = ans + this.getRight().inOrder();
}
return ans;
}
}
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class BinaryTreeTesting {
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTreeNode
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
BinaryTree
public static void main(String[] args) {
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTreeNode
// BinaryTree tree1 = new BinaryTree(node1);
// BinaryTree tree7 = new BinaryTree(node8);
// BinaryTree tree2 = new BinaryTree();
// BinaryTree tree3 = new BinaryTree();
// BinaryTree tree4 = new BinaryTree(node5);
// BinaryTree tree5 = new BinaryTree(node6);
// BinaryTree tree6 = new BinaryTree(node7);
}
@Before
public void setUp() throws Exception {
}
@Test
public void testCombine() {
tree1.combine(node8, tree4, true);
assertFalse(tree5.equals(tree1));
tree9.combine(node4, tree10, false);
// assertTrue(tree9.combine(node4, tree10, false).equals(o));
assertTrue(tree9.equals(tree10));
}
@Test
public void testEqualsObject() {
assertTrue(tree2.equals(tree3));
assertFalse(tree1.equals(tree2));
assertTrue(tree1.equals(tree12));
System.out.println(tree2);
System.out.println("tree:" + tree3);
}
@Test
public void testEqualsObjectNode() {
assertTrue(node15.equals(node2));
assertFalse(node1.equals(node2));
assertTrue(node8.equals(node16));
}
@Test
public void testDeepCopy() {
BinaryTree copy1 = tree1.deepCopy();
assertTrue(copy1.equals(tree1));
assertFalse(copy1.equals(tree2));
}
@Test
public void testDeepCopyNode() {
BinaryTreeNode
assertTrue(copy1.equals(node1));
assertFalse(copy1.equals(node2));
}
@Test
public void testSize() {
assertEquals(0, tree2.size());
assertEquals(3, tree1.size());
}
@Test
public void testHeight() {
assertEquals(0, tree2.height());
assertEquals(2, tree1.height());
}
@Test
public void testHeightNode() {
assertEquals(1, node2.height());
assertEquals(2, node1.height());
}
@Test
public void testFull() {
assertTrue(tree1.full());
assertFalse(tree6.full());
}
@Test
public void testFullNode() {
assertTrue(node2.full());
assertFalse(node7.full());
}
@Test
public void testMirror() {
tree1.mirror();
assertEquals(tree4, tree1);
assertFalse(tree1.equals(tree7));
}
@Test
public void testMirrorNode() {
node1.mirror();
assertEquals(node1, node5);
assertFalse(node1.equals(node8));
}
@Test
public void testInOrder() {
assertEquals("(2)(1)(3)", tree8.inOrder());
assertEquals("(5)(2)(7)", tree7.inOrder());
}
// do
@Test
public void testInOrderNode() {
assertEquals("(2)(1)(3)", node11.inOrder());
assertEquals("(5)(7)", node7.inOrder());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started