I have provided 3 classes!! (BinaryTree, BinaryTreeNode, and Testing). I have co
ID: 3717609 • Letter: I
Question
I have provided 3 classes!! (BinaryTree, BinaryTreeNode, and Testing). I have completed BinaryTree and BinaryTreeNode. I'm halfway done with Testing (uses JUNIT testing) but I can't figure out the rest of the methods. I need a junit for mirror, deep copy, equals, inOrder, and combine for BOTH binaryTree class and BinaryTreeNode. Please please help I've already done half of it. This is in JAVA on eclipse!!!!
public class BinaryTree<T> {
private BinaryTreeNode<T> root;
public BinaryTree() {
this(null);
}
public BinaryTree(BinaryTreeNode<T> newRoot) {
this.root = newRoot;
}
public BinaryTreeNode<T> getRoot() {
return root;
}
public void setRoot(BinaryTreeNode<T> root) {
this.root = root;
}
@Override
public boolean equals(Object o) {
if (o instanceof BinaryTree) {
BinaryTree<T> test = new BinaryTree<T>();
return this.getRoot().equals(test.getRoot());
}
else {
return false;
}
}
public BinaryTree<T> deepCopy() {
BinaryTree<T> ans = new BinaryTree<T>();
ans.setRoot(this.getRoot().deepCopy());
return ans;
}
public BinaryTree<T> combine(BinaryTreeNode<T> newRoot, BinaryTree<T> t,
boolean left) {
BinaryTree<T> ans = new BinaryTree<T>();
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;
}
//returns integer size of tree
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(){
return this.getRoot().full();
}
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<T> {
private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;
private T data;
public BinaryTreeNode(){
this(null,null,null);
}
public BinaryTreeNode(T theData){
this(theData,null,null);
}
public BinaryTreeNode(T theData, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> 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<T> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}
public BinaryTreeNode<T> getRight() {
return right;
}
public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode<T> deepCopy(){
BinaryTreeNode<T>ans = new BinaryTreeNode<T>();
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 obj){
if (this == obj) {
return true;
}
if ( obj == null || !(obj instanceof BinaryTreeNode)) {
return false;
}
BinaryTreeNode that = (BinaryTreeNode)obj;
if (this.data.equals(that.data)) {
return false;
}
else if (this.left == null || this.left.equals(that.left)) {
return false;
}
else if (this.right == null || this.right.equals(that.right)) {
return false;
}
// must be the same
return true;
// if (o instanceof BinaryTreeNode) {
//
// BinaryTreeNode<T> test = new BinaryTreeNode<T>();
//
// if ((left == null) && (right != null)) {
// return (this.getRight().equals(test.getRight()) && this.getData().equals(test.getData()));
// } else if ((left != null) && (right == null)) {
// return (this.getLeft().equals(test.getLeft()) && this.getData().equals(test.getData()));
// } else if ((left == null) && (right == null)) {
// return this.getData().equals(test.getData());
// } else {
// return this.getData().equals(test.getData()) && this.getLeft().equals(test.getLeft())
// && this.getRight().equals(test.getRight());
// }
// } else {
// return false;
}
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(){
boolean ans = false;
if ((this.left != null) && (this.right != null)) {
ans = true;
}
return ans;
}
public void mirror(){
BinaryTreeNode<T> copy = new BinaryTreeNode<T>();
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 Testing {
public BinaryTreeNode<Integer> node1;
public BinaryTreeNode<Integer> node2;
public BinaryTreeNode<Integer> node3;
public BinaryTreeNode<Integer> node4;
public BinaryTreeNode<Integer> node5;
public BinaryTreeNode<Integer> node6;
public BinaryTreeNode<Integer> node7;
public BinaryTreeNode<Integer> node8;
public BinaryTree<Integer> b1;
public BinaryTree<Integer> b2;
public BinaryTree<Integer> b3;
@Before
public void setUp() throws Exception {
node1 = new BinaryTreeNode<Integer>(1);
node2 = new BinaryTreeNode<Integer>(2);
node3 = new BinaryTreeNode<Integer>(3, node1, node2);
node4 = new BinaryTreeNode<Integer>(10);
node5 = new BinaryTreeNode<Integer>(5);
node6 = new BinaryTreeNode<Integer>(6, node4, node5);
node7 = new BinaryTreeNode<Integer>(7, node3, node6);
node8 = new BinaryTreeNode<Integer>(10);
b1 = new BinaryTree<Integer>(node4);
b2 = new BinaryTree<Integer>(node7);
b3 = new BinaryTree<Integer>(node4);
}
//binaryTree tests
@Test
public void testFull() {
assertFalse(b1.full());
assertTrue(b2.full());
}
@Test
public void testSize() {
assertEquals(b2.size(), 7);
assertEquals(b1.size(), 1);
}
@Test
public void testHeight() {
assertEquals(b2.height(), 3);
assertEquals(b1.height(), 1);
}
//binarytreenode tests
@Test
public void testFull1( ) {
assertFalse(node4.full());
assertTrue(node6.full());
}
@Test
public void testSize1() {
assertEquals(node1.size(), 1);
assertEquals(node6.size(), 3);
}
@Test
public void testHeight1() {
assertEquals(node7.height(), 3);
assertEquals(node4.height(), 1);
}
}
Explanation / Answer
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class BinaryTreeTest {
BinaryTreeNode<Integer> node4 = new BinaryTreeNode<Integer>();
BinaryTreeNode<Integer> node9 = new BinaryTreeNode<Integer>(2);
BinaryTreeNode<Integer> node10 = new BinaryTreeNode<Integer>(3);
BinaryTreeNode<Integer> node2 = new BinaryTreeNode<Integer>(5);
BinaryTreeNode<Integer> node3 = new BinaryTreeNode<Integer>(7);
BinaryTreeNode<Integer> node7 = new BinaryTreeNode<Integer>(7, node2, null);
BinaryTreeNode<Integer> node1 = new BinaryTreeNode<Integer>(2, node2, node3);
BinaryTreeNode<Integer> node8 = new BinaryTreeNode<Integer>(2, node2, node3);
BinaryTreeNode<Integer> node5 = new BinaryTreeNode<Integer>(2, node3, node2);
BinaryTreeNode<Integer> node6 = new BinaryTreeNode<Integer>(2, node1, node5);
BinaryTreeNode<Integer> node11 = new BinaryTreeNode<Integer>(1, node9, node10);
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);
public static void main(String[] args) {
BinaryTreeNode<Integer> node4 = new BinaryTreeNode<Integer>();
BinaryTreeNode<Integer> node2 = new BinaryTreeNode<Integer>(5);
BinaryTreeNode<Integer> node3 = new BinaryTreeNode<Integer>(7);
BinaryTreeNode<Integer> node7 = new BinaryTreeNode<Integer>(7, node2, null);
BinaryTreeNode<Integer> node1 = new BinaryTreeNode<Integer>(2, node2, node3);
BinaryTreeNode<Integer> node8 = new BinaryTreeNode<Integer>(2, node2, node3);
BinaryTreeNode<Integer> node5 = new BinaryTreeNode<Integer>(2, node3, node2);
BinaryTreeNode<Integer> node6 = new BinaryTreeNode<Integer>(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.equals(tree10));
}
@Test
public void testEqualsObject() {
assertTrue(tree1.equals(tree1));
assertFalse(tree1.equals(tree2));
}
@Test
public void testEqualsObjectNode() {
assertTrue(node1.equals(node1));
assertFalse(node1.equals(node2));
}
@Test
public void testDeepCopy() {
BinaryTree copy1 = tree1.deepCopy();
assertTrue(copy1.equals(tree1));
assertFalse(copy1.equals(tree2));
}
@Test
public void testDeepCopyNode() {
BinaryTreeNode<Integer> 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());
}
}