Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.util.*; public class PoD { //============================================================================= /** * Creates a mathematical equation based on the contents of a strict binary tree: * - If
import java.util.*; public class PoD { //============================================================================= /** * Creates a mathematical equation based on the contents of a strict binary tree: * - If the tree is empty, outputs: "0" * - If the left tree ( and therefore right as well) is empty, returns tree data. * - Otherwise: Outputs a mathematical expression of the form: * * "( leftEquation mathSymbol rightEquation )" * * where leftEquation and rightEquation are mathematical equations themselves * based on the leftSubtree and rightSubtree, respectively. * * * mathSymbol * / \ * / \ * leftSubtree rightSubtree * * * @param tree strict BinaryTree of interest * @return String of mathematicial equation */ public static String equationMaker(BinaryTree tree) { /* If the tree is empty, output: 0 Otherwise: Output a mathemematical expression of the form: ( leftSide math rightSide ) where leftSide and rightSide can themselves be mathematical equations. (math) / \ / \ leftSide rightSide e.g. the following tree * / \ / \ + - / \ / \ 3 4 2 + / \ 1 3 becomes ( ( 3 + 4 ) * ( 2 - ( 1 + 3 ) ) ) */ String mathEquation; return mathEquation; } //============================================================================= public static void main(String[] args) { Scanner in = new Scanner(System.in); BinaryTree newTree = buildTree(in); String treeEquation = equationMaker(newTree); System.out.println(treeEquation); in.close(); System.out.print("END OF OUTPUT"); } /** * Reads in data to form a binary tree: * N ( L R ) * N = node data * L = Left binary tree data (possibly of same form) * R = Right binary tree data (possibly of same form) * expects all nodes until reaching null * * e.g. input: a ( b ( null null ) c ( null null ) ) * creates tree: * a * / \ * b c */ public static BinaryTree buildTree(Scanner in) { String data = in.next(); BinaryTree left = null; BinaryTree right = null; //System.out.println(data); if (data.equals("null")) { return null;} if (!in.next().equals("(")) { System.out.println("BAD INPUT: (L"); } left = buildTree(in); right = buildTree(in); if (!in.next().equals(")")) { System.out.println("BAD INPUT: R)"); } return new BinaryTree(data, left, right); } }Today you are going to create a mathematical equation based on the contents of a strict binary tree Details Download the ZIP files named FilesNeeded.zip for use in IntelliJ. When you are happy with your solution, upload the files into the src folder and run. You are going to finish off PoD.java to finish the equationMaker method Input The main method (already completed) creates a strict binary tree and passes it to the method equationMaker Processing You are going to complete the details of the equationMaker method. Details of the method and its expected parameters are described in the Javadoc comments preceding it. The method should take in a binary tree create a mathematical equation based on the contents of the tree o If the tree is empty. output: 0 o If the left tree (and therefore right as well) is empty, return contents of tree. Otherwise: Output a mathematical expression of the form: "(leftEquation mathSymbol rightEquation)" where leftEquation and rightEquation are mathematical equations themselves based on the leftSubtree and rightSubtree, respectively nathSynbol
Step 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