Question
please use java thanks the BinaryNode code is: import java.util.Iterator; public class BinaryNode implements Iterable { private T data; private BinaryNode left; private BinaryNode right;
please use java thanksthe BinaryNode code is:
import java.util.Iterator;
public class BinaryNode implements Iterable
{
private T data;
private BinaryNode left;
private BinaryNode right;
public BinaryNode getLeft() { return left; }
public BinaryNode getRight() { return right; }
public T getData() { return data; }
public void setLeft(BinaryNode left) { this.left = left; }
public void setRight(BinaryNode right) { this.right = right; }
public void setData(T data) { this.data = data; }
public String preOrder(String separator)
{
return data
+ (left == null ? "" : separator + left.preOrder(separator))
+ (right == null ? "" : separator + right.preOrder(separator));
}
public String postOrder(String separator)
{
return (left == null ? "" : left.postOrder(separator) + separator)
+ (right == null ? "" : right.postOrder(separator) + separator)
+ data;
}
public String inOrder(String separator)
{
return (left == null ? "" : left.inOrder(separator) + separator)
+ data
+ (right == null ? "" : separator + right.inOrder(separator));
}
public Iterator iterator()
{
return new LevelOrderIterator(this);
}
public int height()
{
return 1 + Math.max(
left == null ? 0 : left.height(),
right == null ? 0 : right.height());
}
public int size()
{
return 1 +
(left == null ? 0 : left.size()) +
(right == null ? 0 : right.size());
}
public T first()
{
return left == null ? data : left.first();
}
public T last()
{
return right == null ? data : right.first();
}
public static void main(String[] args)
{
BinaryNode root = new BinaryNode();
// // In-class exercise
// root.setData(8);
// root.setLeft(new BinaryNode());
// root.getLeft().setData(3);
// root.setRight(new BinaryNode());
// root.getRight().setData(10);
// root.getLeft().setLeft(new BinaryNode());
// root.getLeft().getLeft().setData(1);
// root.getLeft().setRight(new BinaryNode());
// root.getLeft().getRight().setData(6);
// root.getRight().setRight(new BinaryNode());
// root.getRight().getRight().setData(14);
// root.getLeft().getRight().setLeft(new BinaryNode());
// root.getLeft().getRight().getLeft().setData(4);
// root.getLeft().getRight().setRight(new BinaryNode());
// root.getLeft().getRight().getRight().setData(7);
// root.getRight().getRight().setLeft(new BinaryNode());
// root.getRight().getRight().getLeft().setData(13);
root.setData(2);
root.setLeft(new BinaryNode());
root.getLeft().setData(7);
root.setRight(new BinaryNode());
root.getRight().setData(5);
root.getLeft().setLeft(new BinaryNode());
root.getLeft().getLeft().setData(2);
root.getLeft().setRight(new BinaryNode());
root.getLeft().getRight().setData(6);
root.getRight().setRight(new BinaryNode());
root.getRight().getRight().setData(9);
root.getLeft().getRight().setLeft(new BinaryNode());
root.getLeft().getRight().getLeft().setData(5);
root.getLeft().getRight().setRight(new BinaryNode());
root.getLeft().getRight().getRight().setData(11);
root.getRight().getRight().setLeft(new BinaryNode());
root.getRight().getRight().getLeft().setData(4);
System.out.println("Height: " + root.height());
System.out.println("Pre-order: " + root.preOrder(", "));
System.out.println("Post-order: " + root.postOrder(", "));
System.out.println("In-order: " + root.inOrder(", "));
System.out.print("Level-order:");
for (int i : root)
{
System.out.print(" " + i);
}
System.out.println();
}
}
LevelOrderIterator code is:
import java.util.Iterator;
public class LevelOrderIterator
{
private Queue
public LevelOrderIterator(BinaryNode
{
queue.add(root);
}
public boolean hasNext()
{
return !queue.isEmpty();
}
public T next()
{
BinaryNode
if (next.getLeft() != null)
{
queue.add(next.getLeft());
}
if (next.getRight() != null)
{
queue.add(next.getRight());
}
return next.getData();
}
}
2 Modified in-order traversal In lecture, we developed a method for performing an in-order traversal of a binary tree that formats the contents of the tree as a String. Write a similar method in BinaryNode.java that returns the contents of the tree as an array. That is, the first element in the array returned should contain the first element visited in the traversal, the second element in the array should be the second one visited. etc. The signature for this method should be: public Object[] inOrder () Write a test case or two for this method and demonstrate that it works to a TAStep 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