Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

No alterations can happen with the methods, my code is below, I can only have fi

ID: 3674667 • Letter: N

Question

No alterations can happen with the methods, my code is below, I can only have five along with the nested class. This is the jest what I have to do:

Implement a class called ExpressionTree . The constructor to ExpressionTree will take in a String that contains a postfix expression. The operands will be integers and the binary operators will be restricted to +, -, *, and divide. Individual tokens, that is the operands and operators, will be delimited by spaces. So for example:

34 2 - 5 *

would mean (34-2)*5.

Your constructor will run the stack based algorithm we discussed in class to build an expression tree. ExpressionNodes will be a nested class within ExpressionTree. You may use any code posted on canvas or from the Weiss textbook as a starting point for this class.

Once you have the ExpressionTree constructed you should provide the following four methods:

public int eval() - this method, when invoked on an expression tree object will return the integer value associated with evaluating the expression tree. It will need to call a private recursive method that takes in the root.

public String postfix() - this method, when invoked on an expression tree object will return a String that contains the corresponding postfix expression. It will need to call a private recursive method that takes in the root.

public String prefix() - this method, when invoked on an expression tree object will return a String that contains the corresponding prefix expression. It will need to call a private recursive method that takes in the root.

public String infix() - this method, when invoked on an expression tree object will return a String that contains the corresponding correct infix expression. Keep in mind that parenthesis will be needed (excessive parenthesis will be tolerated as long as it is correct). It will need to call a private recursive method that takes in the root.

Write a class called Problem1.java that instantiates an expression, and demonstrate your methods.

public class ExpressionTree {

   char token;
   boolean isNumber = true;

   private ExpressionNode root;

   public static class ExpressionNode {
       ExpressionNode left;
       char ch;
       ExpressionNode right;

       ExpressionNode(ExpressionNode left, char ch, ExpressionNode right) {
           this.left = left;
           this.ch = ch;
           this.right = right;
       }
   }

   private final String postfixExp;

   /**
   * Takes in a valid postfix expression and later its used to construct the
   * expression tree. The posfix expression, if invalid, leads to invalid
   * results
   *
   * @param postfix
   * the postfix expression.
   */
   public ExpressionTree(String postfixExp) {
       if (postfixExp == null) {
           throw new NullPointerException("The posfix should not be null");
       }
       if (postfixExp.length() == 0) {
           throw new IllegalArgumentException("The postfix should not be empty");
       }
       this.postfixExp = postfixExp;

       int i = 0, n = postfixExp.length();

       // loop until i reaches the end of the string
       while (i != n) {
           // skip blanks and tabs in the expression
           while (postfixExp.charAt(i) == ' ' || postfixExp.charAt(i) == ' ')
               i++;

           // if the expression has trailing whitespace ie. the end of the
           // String
           if (i == n)
               break;

           // extract the current token and increment i
           token = postfixExp.charAt(i);
           i++;

           // checking to see if the token is an operator or an operand
           if (token == '+' || token == '-' || token == '*' || token == '/') {
               return;
           } else if (isNumber) { // is operand
               return;
           }
       }
   }

   public int eval() {

   }

   /**
   * Constructs an expression tree, using the postfix expression
   */
   public String postfix() {
       final MyStack<ExpressionNode> nodes = new MyStack<ExpressionNode>();
       for (int i = 0; i < postfixExp.length(); i++) {
           char ch = postfixExp.charAt(i);
           if (token == '+' || token == '-' || token == '*' || token == '/') {
               ExpressionNode rightNode = nodes.pop();
               ExpressionNode leftNode = nodes.pop();
               nodes.push(new ExpressionNode(leftNode, ch, rightNode));
           } else {
               nodes.add(new ExpressionNode(null, ch, null));
           }
       }
       root = nodes.pop();
   }

   /**
   * Returns the prefix notation
   *
   * @return the prefix notation
   */
   public String prefix(ExpressionNode node) {
       if (root == null) {
           throw new NoSuchElementException("The root is empty, the tree has not yet been constructed.");
       }
       if (node.left != null) {
           prefix(node.left);
       }
       if (node.right != null) {
           prefix(node.right);
       }

   }

   /**
   * Returns the infix expression
   *
   * @return the string of infix.
   */
   public String infix() {
       if (root == null) {
           throw new NoSuchElementException("The root is empty, the tree has not yet been constructed.");
       }
       final StringBuilder infix = new StringBuilder();
       inOrder(root, infix);
       return infix.toString();
   }

   public static void main(String[] args) {
       ExpressionTree expressionTree1 = new ExpressionTree("AB*CD/+");
       expressionTree1.createExpressionTree();
       assertEquals("+*AB/CD", expressionTree1.prefix());
       assertEquals("A*B+C/D", expressionTree1.infix());

       ExpressionTree expressionTree2 = new ExpressionTree("ABC+*D/");
       expressionTree2.createExpressionTree();
       assertEquals("/*A+BCD", expressionTree2.prefix());
       assertEquals("A*B+C/D", expressionTree2.infix());

       ExpressionTree expressionTree3 = new ExpressionTree("ABCD/+*");
       expressionTree3.createExpressionTree();
       assertEquals("*A+B/CD", expressionTree3.prefix());
       assertEquals("A*B+C/D", expressionTree3.infix());
   }
}

Explanation / Answer

public class ExpressionTree { private final String postfix; private TreeNode root; /** * Takes in a valid postfix expression and later its used to construct the expression tree. * The posfix expression, if invalid, leads to invalid results * * @param postfix the postfix expression. */ public ExpressionTree(String postfix) { if (postfix == null) { throw new NullPointerException("The posfix should not be null"); } if (postfix.length() == 0) { throw new IllegalArgumentException("The postfix should not be empty"); } this.postfix = postfix; } private static class TreeNode { TreeNode left; char ch; TreeNode right; TreeNode(TreeNode left, char ch, TreeNode right) { this.left = left; this.ch = ch; this.right = right; } } private boolean isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } /** * Constructs an expression tree, using the postfix expression */ public void createExpressionTree() { final Stack nodes = new Stack(); for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote