Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 thanksimage text in transcribedthe 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 implements Iterator

{

private Queue> queue = new LinkedQueue>();

public LevelOrderIterator(BinaryNode root)

{

queue.add(root);

}

public boolean hasNext()

{

return !queue.isEmpty();

}

public T next()

{

BinaryNode next = queue.remove();

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 TA

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_2

Step: 3

blur-text-image_3

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

5. How do instructional objectives help learning to occur?

Answered: 1 week ago

Question

4. Help trainees set challenging mastery or learning goals.

Answered: 1 week ago