Question: Help with java tree to string conversion! In this assignment, you implement a method to convert a tree to a string. Your HW8Tree class must

Help with java tree to string conversion!

In this assignment, you implement a method to convert a tree to a string.

Your HW8Tree class must include the BinaryNode.java code **see below:

BinaryNode.java

private static class BinaryNode { private T item; private BinaryNode left; private BinaryNode right; /** * constructor to build a node with no subtrees * @param the value to be stored by this node */  private BinaryNode(T value) { item = value; left = null; right = null; } /** * constructor to build a node with a specified (perhaps null) subtrees * @param the value to be stored by this node * @param the left subtree for this node * @param the right subtree for this node */ private BinaryNode(T value, BinaryNode l, BinaryNode r) { item = value; left = l; right = r; } } 

Your HW8Tree class must have an instance variable that refers to (points to) the root of a tree. This class must provide these recursive methods:

a public boolean add(E item, boolean[] left) method to add values to the tree. Starting from the root, at each node, the method must go down to the left if the corresponding position in the array is true, and otherwise must go down to the right. Once a null pointer is reached, the item must be added to the tree in place of the null pointer.

It is an error if the array of booleans is too short. In this case, the add method should throw a RuntimeException.

a public List toList() method that stores in a list a reference to each of the objects in the tree, as visited in an in-order traversal. You can choose what kind of list to use, as long as it satisfies the List interface.

a public String toString() method that converts the tree to a string, in the same way that folders are printed: the root is first, followed by the children of the node indented 4 spaces, the grandchildren indented 8 spaces, and so on.

Each of these methods must use recursion to traverse the tree. In each case, the recursion may be in a helper method.

For example, consider a tree with root "hello". "hello" has two children, "foo" and "bar". "foo" has children "world", and "baz", and "bar" has children "bug" and "loop".

The toString method converts the tree to the following string:

hello foo world baz bar bug loop 

The above example shows what the string looks like when it is printed. As a string, it actually looks like this:

"hello foo world baz bar bug loop " 

As many of you already know, " " is the newline character in Java.

Calling toList should return a list with contents world, foo, baz, hello, bug, bar, loop. Only this order correctly reflects an in-order traversal.

To add the string "fum" below and to the right of "bug", we would call

 boolean[] lefts = { false, true, false}; HW8Tree myTree = new HW8Tree (); // fill in the tree until it matches the example above, then myTree.add ("fum", lefts); 

You should also create a test class that lets you test your code. You do not need to turn in the test class.

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!