1 answer

Return a method as an expression tree Hi guys. I need to return a method as...

Question:

Return a method as an expression tree

Hi guys.

I need to return a method as an expression tree, it's currently returning null.

public static ExpressionTree getExpressionTree(String expression)
throws Exception {      
char[] charArray = expression.toCharArray();
Node root = et.constructTree(charArray);
System.out.println("infix expression is");
et.inorder(root);
return et;
}

In the above method, et needs to have a value. -- Take a look at the complete class below.

; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS = +-*/() // A utility function to

t = new Node (postfix[i]); // Pop two top nodes // Store top tl = st.pop(); // Remove top t2 = st.pop(); // make them childre

private ExpressionTree (Object rootData, ExpressionTree left, ExpressionTree right) throws Exception { super (rootData, left,

Kindly assist.

; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS = " +-*/() // A utility function to check if c' // is an operator static boolean isOperator (char c) { if (c == "+' IIC == - II C == *** II C == '/' Il c == "") { return true; return false; // Utility function to do inorder traversal static void inorder (Node t) { if (t != null) { inorder (t.left); System.out.print (t.value + " "); inorder (t.right); // Returns root of constructed tree for given // postfix expression static Node constructTree (char postfix[]) { Stack<Node> st = new Stack(); Node t, ti, t2; // Traverse through every character of // input expression for (int i = 0; i < postfix.length; i++) { // If operand, simply push into stack if (!isOperator (postfix[i])) { t = new Node (postfix[i]); st.push(t); } else // operator
t = new Node (postfix[i]); // Pop two top nodes // Store top tl = st.pop(); // Remove top t2 = st.pop(); // make them children t.right = tl; t.left = t2; // System.out.println(tl + "" + t2); // Add this subexpression to stack st.push(t); // only element will be root of expression // tree t = st. peek(); st.pop(); return t; public static ExpressionTree getExpressionTree (String expression) throws Exception { char[] charArray = expression.toCharArray(); Node root = et.constructTree (charArray); System.out.println("infix expression is"); et.inorder (root); return et; private ExpressionTree (Object x){ super (x);
private ExpressionTree (Object rootData, ExpressionTree left, ExpressionTree right) throws Exception { super (rootData, left, right); } // end constructor @Override public String to PreFixString() { return to PreFixString (this); } // end toPreFixString private String to PreFixString (BinaryTree t) { return ; } // end to PreFixString @Override public String toInFixString() { return toInFixString (this); } // end toInFixString private String toinFixString (BinaryTree t) { return ; } // end toInFixString @Override public String to Post FixString() { return to PostFixString (this); } // end toPostFixString private String to PostFixString (BinaryTree t) { return ""; } // end to Post FixString public String intermediateCode () { String out = intermediateCode (this); return out + "Pop result\n"; } // end intermediateCode

Answers

First of All, the "Node" class is missing, you should add Node class then only you can access the object of the node.

A node class is given below and the properties and Constructor is given below.

So now you can access the Node object and you can return the expression tree without any exception.

//Code start Here

import java.util.Stack; // You need to add this class Node to your script // your accessing Node object without node class // so it throws exception class Node {      char value;     Node left, right;      Node(char item) {         value = item;         left = right = null;     } }  /* The real ExpressionTree is started here write your code for expression tree  Node constructTree(char postfix[]) {  .. .. .. return t; */  //Also add main function in your code this will increase the chance to return your expecting //Expression Tree      public static void main(String args[]) {          ExpressionTree et = new ExpressionTree();         String postfix = "ab+ef*g*-";         char[] charArray = postfix.toCharArray();         Node root = et.constructTree(charArray);         System.out.println("infix expression is");         et.inorder(root);      } }  // This code has been contributed by Mayank Jaiswal 

//Code ends here

Other than this every code in this file is correct. What you want to do is just Add the Node class to your script...

Thank you

.

Similar Solved Questions

1 answer
The following reaction has a small, positive AG and takes place slowly at room temperature: 4....
The following reaction has a small, positive AG and takes place slowly at room temperature: 4. H-OH H3C-OH H-CI H3C-CI (a) The mechanism for the reaction is shown below. Which bond is being broken in the slowest step? Which bond is being made? slow Hас -он; Н-О...
1 answer
Please help me with this 2 questions I need to know how to solve the complexity...
please help me with this 2 questions I need to know how to solve the complexity by inner loop and outer look and how to get the O after that (in Data Structure and Algorithms) book by adam drozdek thanks ? We were unable to transcribe this imageQuestion 2 For each of the following pairs of functions...
1 answer
O Emalsursu kedu MAT103 - Fall 2019 - Teason - 95546 Sam Pecie 6 | 11/20/19...
O Emalsursu kedu MAT103 - Fall 2019 - Teason - 95546 Sam Pecie 6 | 11/20/19 10:54 PM Time Remaining:00:50:47 Submit Tes Test: Chapter 6 Test This Question: 1 pt 3 of 90 complete This Test: 9 pts possit 58.2. 3.5and n=100 Match the level of confidence, 0.90, with its representation on the numberine g...
1 answer
Question 14 2 pts What is the mass of 1.47 x 1023 atoms of sulfur, S?...
Question 14 2 pts What is the mass of 1.47 x 1023 atoms of sulfur, S? 7.83 gs 19.1gs 13.5gs 22.99 g 5 12.5g5...
1 answer
A firm considers a project with the following cash flows: the initial cost at time 0...
A firm considers a project with the following cash flows: the initial cost at time 0 is $1,000 (negative), the following cash flows received are $1,000 $100, $50, $50 in Year 1 to Year 4, respectively. What's its MIRR if WACC=11%? A) 12.40% B) 13.08% OC) 11.00% D) 10.72% E) 11.96%...
1 answer
Performance Lawn Equipment (PLE) is considering changing their process to make mower blades. They conducted seven...
Performance Lawn Equipment (PLE) is considering changing their process to make mower blades. They conducted seven production runs and recorded costs. Will the new process save PLE money? Use α = 0.05. (Show all six steps of hypothesis testing.) Current New Process $242.00 $242.00 ...
1 answer
What are the complete and the net ionic equations for the reaction between silver nitrate and sodium chloride?
What are the complete and the net ionic equations for the reaction between silver nitrate and sodium chloride?...
1 answer
Major product when tree with strong and weak base
What is the major product obtained treating when you treat 2-bromo-2,3-dimethylbutane with NaOEt?What is the major product obtained treating when you treat 2-bromo-2,3-dimethylbutane with t-BuOK?...
1 answer
What do fungi and arthropods have in common?
What do fungi and arthropods have in common?...
1 answer
Draw the structure(s) of the major organic produces) of the following reaction.
Draw the structure(s) of the major organic produces) of the following reaction....
1 answer
ACTIVE LEARNING TEMPLATE Basic Concept: Information Management Systems ACTIVE LEARNING TEMPLATE: Basic Concept STUDENT NAME REVIEW...
ACTIVE LEARNING TEMPLATE Basic Concept: Information Management Systems ACTIVE LEARNING TEMPLATE: Basic Concept STUDENT NAME REVIEW MODULE CHAPTER CONCEPT In formation Managment Systems Nursing Interventions Related Content Underlying Principles WHO? WHEN? WHY? HOW? (E.G. DELEGATION, LEVELS OF PREVE...
2 answers
The hypotenuse of a right triangle measures 10m
The hypotenuse of a right triangle measures 10m. One leg of the triangle is 2m longer then the other. Find the lengths of the legs.How would you solve this equation?...
1 answer
2. List two characters/traits you share with ALL animals that are NOT shared by ANY plants....
2. List two characters/traits you share with ALL animals that are NOT shared by ANY plants. 2a. Consider the traits you listed in the previous question. Would any of them help you to distinguish an animal (e.g., you) from a plant (e.g., a pine tree)? Choose one: YES NO (highlight in color and bold) ...
1 answer
PREPARATION OF FINANCIAL STATEMENTS STATEMENT OF FINANCIAL POSITION STATEMENT OF FINANCIAL PERFORMANCE ISM Industries provided the...
PREPARATION OF FINANCIAL STATEMENTS STATEMENT OF FINANCIAL POSITION STATEMENT OF FINANCIAL PERFORMANCE ISM Industries provided the following account balance on December 31, 2019 Share capital 5,000,000.00 Share premium 500,000.00 Retained Earnings 880,000.00 Serial bonds pa...
1 answer
Problem 9-7 WACC Shi Import-Export's balance sheet shows $300 million in debt, $50 million in preferred stock, and...
Problem 9-7 WACC Shi Import-Export's balance sheet shows $300 million in debt, $50 million in preferred stock, and $250 million in total common equity. Shi's tax rate is 35%, -7%, -5.7%, and r, 10%. If Shi has a target capital structure of 30% debt, 5% preferred stock, and 65% common stock w...
1 answer
Could you please explain 52 and 53? section S. Short Answer and Problems (points indicated). Clearty write weur work for following in the space provided. Include units where appropriate. Show Sect...
Could you please explain 52 and 53? section S. Short Answer and Problems (points indicated). Clearty write weur work for following in the space provided. Include units where appropriate. Show Section 5 Points: Clearly write the answer to each of the your work for full credit. 52. (4 points) Expla...