Answered step by step
Verified Expert Solution
Link Copied!

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[]) { Stack st = 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

9. Power and politics can be destructive forces in organizations.

Answered: 1 week ago