Question: Topic : Tree Homework In this assignment, you implement a method to convert a tree to a string. Your HW8Tree class must include the BinaryNode.java
Topic : Tree Homework In this assignment, you implement a method to convert a tree to a string.
Your HW8Tree class must include the BinaryNode.java code.
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:( Help me this Black Bold)
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.
===========================(Java Programming with public boolean add, public List toList(), public String toString() methoods) 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; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
