Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I want to add user input to this java code and im not quite sure how to go about it. package binaryTree; //Java program to
I want to add user input to this java code and im not quite sure how to go about it.
package binaryTree; //Java program to construct an expression tree import java.util.Stack; //Java program for expression tree class Node { char value; Node left, right; Node(char item) { value = item; left = right = null; } } class ExpressionTree { // A utility function to check if 'c' // is an operator boolean isOperator(char c) { if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') { return true; } return false; } // Utility function to do inorder traversal 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 Node constructTree(char postfix[]) { Stackst = new Stack (); Node t, t1, 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 t1 = st.pop(); // Remove top t2 = st.pop(); // make them children t.right = t1; t.left = t2; // System.out.println(t1 + "" + 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 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); } }
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