Question: BinaryTree public class BinaryTree { private BinaryTreeNode root; public BinaryTree() { this(null); } public BinaryTree(BinaryTreeNode newRoot) { this.root = newRoot; } public BinaryTreeNode getRoot() {

BinaryTree

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;

return this.getRoot().equals(test.getRoot());

} else {

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

return this.getRoot().getLeft().full() && this.getRoot().getRight().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;

}

}

}

BinaryTreeNode

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

if (o instanceof BinaryTreeNode) {

BinaryTreeNode test = (BinaryTreeNode) o;

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

}

}

ASSIGNMENT:

Write 2 JUNITS for all methods, excluding getters and setters

BinaryTree (equals, deepcopy, size, height, full, mirror, inorder)

BinaryTreeNode (size, deepcopy, equals, height, full, mirror, inorder)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!