Question: I only need help on the bolded portion of this excercise. I'm breaking it down so that it has one question per post. If the
I only need help on the bolded portion of this excercise. I'm breaking it down so that it has one question per post. If the answer is too long please just respond with some kind of answer and leave a comment when you do to let me know that the method is not complete and that I need to ask another question in order to get the rest of the code. The question is what code do I need to enter in between the bolded area in order to complete the method? Please answer as I need to have this complete by the end of today and I am struggling.
For this excercise we will be using a Binary Tree ADT (Abstract Data Type).
In the exercise, we developed the In Order and Post Order traversals of the binary tree using the Pre Order traversal as a template.
You need to decide which of those traversal methods to use to print an indented list, such as you might find in a file directory listing or table of contents, of the elements of the tree.
Create a new method, indented(), to implement this algorithm. The method will print the current element preceded by a fixed number of spaces to indicate the relative position within the tree with respect to parent and child nodes. E.g., the root node will be indented 0 spaces, its children will be indented 2 spaces relative to the parent, and so forth.
Use the TreeDriver.java attached to this excercise. Only put your code in between the bolded area in the TreeDriver.java file.
public class TreeDriver { /** * This method constructs an arithmetic expression tree of an expression s in * prefix notation by simply calling the recursive version of the same method. * * Prefix Notation: Operators are written before the operands, e.g + 3 4 */ public static void preorderAET(LinkedBinaryTree T, String s) { preorderAET(T, T.addRoot(null), s); } /** * This method recursively constructs an arithmetic expression tree of an * arithmetic expression s in prefix notation starting at a position v. */ public static String preorderAET(LinkedBinaryTree T, Position v, String s) { if (s.length() == 0) return ""; if (s.charAt(0) == ' ') return preorderAET(T, v, s.substring(1)); // Skip spaces. else if (Character.isDigit(s.charAt(0))) { T.set(v, Character.valueOf(s.charAt(0))); return s.substring(1); } else { T.set(v, Character.valueOf(s.charAt(0))); T.addLeft(v, null); T.addRight(v, null); String t = preorderAET(T, T.left(v), s.substring(1)); return preorderAET(T, T.right(v), t); } } // ******************************* // Add your indented() methods here // ******************************* public static void main(String[] args) { LinkedBinaryTree bt = new LinkedBinaryTree<>(); preorderAET(bt, "- / * + 3 1 3 + - 9 5 2 + * 3 - 7 4 6"); // expression in prefix notation indented(bt); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
