Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 root;

public BinaryTree() {

this(null);

}

public BinaryTree(BinaryTreeNode newRoot) {

this.root = newRoot;

}

public BinaryTreeNode getRoot() {

return root;

}

public void setRoot(BinaryTreeNode root) {

this.root = root;

}

@Override

public boolean equals(Object o) {

if (o instanceof BinaryTree) {

BinaryTree test = (BinaryTree) o;

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 deepCopy() {

BinaryTree ans = new BinaryTree();

ans.setRoot(this.getRoot().deepCopy());

return ans;

}

public BinaryTree combine(BinaryTreeNode newRoot, BinaryTree t,

boolean left) {

BinaryTree ans = new 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 left;

private BinaryTreeNode right;

private T data;

public BinaryTreeNode(){

this(null,null,null);

}

public BinaryTreeNode(T theData){

this(theData,null,null);

}

public BinaryTreeNode(T theData, BinaryTreeNode leftChild, BinaryTreeNode rightChild){

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 getLeft() {

return left;

}

public void setLeft(BinaryTreeNode left) {

this.left = left;

}

public BinaryTreeNode getRight() {

return right;

}

public void setRight(BinaryTreeNode right) {

this.right = right;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public BinaryTreeNode deepCopy(){

BinaryTreeNode ans = new 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 other = (BinaryTreeNode)o ;

//

// //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 copy = new 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 node4 = new BinaryTreeNode();

BinaryTreeNode node9 = new BinaryTreeNode(2);

BinaryTreeNode node10 = new BinaryTreeNode(3);

BinaryTreeNode node2 = new BinaryTreeNode(5);

BinaryTreeNode node3 = new BinaryTreeNode(7);

BinaryTreeNode node7 = new BinaryTreeNode(7, node2, null);

BinaryTreeNode node1 = new BinaryTreeNode(2, node2, node3);

BinaryTreeNode node8 = new BinaryTreeNode(2, node2, node3);

BinaryTreeNode node5 = new BinaryTreeNode(2, node3, node2);

BinaryTreeNode node6 = new BinaryTreeNode(2, node1, node5);

BinaryTreeNode node11 = new BinaryTreeNode(1, node9, node10);

BinaryTreeNode node15 = new BinaryTreeNode(5);

BinaryTreeNode node16 = new BinaryTreeNode(2, node2, node3);

BinaryTree tree8 = new BinaryTree(node11);

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);

BinaryTree tree9 = new BinaryTree(node4);

BinaryTree tree10 = new BinaryTree(node4);

BinaryTree tree12 = new BinaryTree(node1);

public static void main(String[] args) {

// BinaryTreeNode node4 = new BinaryTreeNode();

// BinaryTreeNode node2 = new BinaryTreeNode(5);

// BinaryTreeNode node3 = new BinaryTreeNode(7);

// BinaryTreeNode node7 = new BinaryTreeNode(7, node2, null);

// BinaryTreeNode node1 = new BinaryTreeNode(2, node2, node3);

// BinaryTreeNode node8 = new BinaryTreeNode(2, node2, node3);

// BinaryTreeNode node5 = new BinaryTreeNode(2, node3, node2);

// BinaryTreeNode node6 = new BinaryTreeNode(2, node1, node5);

// 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 copy1 = node1.deepCopy();

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions