Write a program that takes as input a fully parenthesized , arithmetic expression and convert it to a binary expression tree. Your program should display the tree in some way and also print the value associated with the root. LANGUAGE In java
Please answer...
Program:
import java.util.ArrayList;
import java.util.*;
//Stack class
class Stack<T>
{
//ArratList
ArrayList<T> stackTop;
//constructor
public Stack()
{
stackTop = new ArrayList<>();
}
//method to determine whether the stack is empty.
public boolean isEmpty()
{
if(stackTop.isEmpty())
return true;
return false;
}
//method to add newItem to the stack.
public void add(T value)
{
stackTop.add(value);
}
//method to remove the top element of the stack.
public T pop()
{
if(!isEmpty())
{
T value =
stackTop.get(stackTop.size()-1);
stackTop.remove(stackTop.size() - 1);
return value;
}
else
return null;
}
//method to return the top element of the stack.
public T top()
{
if(!isEmpty())
return
stackTop.get(stackTop.size()-1);
else
return null;
}
}
//InfixToPostfix
class InfixToPostfix
{
//method to return priority of a operator
public int getPriority(String c)
{
switch(c)
{
case "^": return
5;
case "/": return
4;
case "*": return
3;
case "+": return
2;
case "-": return
1;
case "(": return
0;
default: return
999;
}
}
//check if operator
boolean isOperator(String s)
{
switch(s)
{
case "+": case
"-": case "/": case "*": case "^":
return true;
}
return false;
}
//main method
public String infixToPostfix(String e)
{
Stack<String> stk = new
Stack();
String output = "";
stk.add("(");
String exp = e + " )";
String arr[] = exp.split(" ",
20);
//convert from infix to
postfix
for(int i=0; i<arr.length;
i++)
{
String ch =
arr[i];
//check for left
parenthesis
if(ch.equals("("))
{
stk.add(ch);
}
//check for
righr parenthesis
else
if(ch.equals(")"))
{
while(!stk.top().equals("("))
{
output = output + stk.pop() +
" ";
}
stk.pop();
}
//operator
else
if(isOperator(ch))
{
int p1 = getPriority(ch);
int p2 = getPriority(stk.top());
while(p1<=p2)
{
output = output + stk.pop() +
" ";
p2 =
getPriority(stk.top());
}
stk.add(ch);
}
//operand
else
{
output = output + ch + " ";
}
}
if(!stk.isEmpty())
return
"None";
else
return
output;
}
}
//Node class
class Node
{
String info;
Node lchild;
Node rchild;
//constructor
Node(String val)
{
info = val;
lchild = null;
rchild = null;
}
}
//ExpressionTree class
class ExpressionTree
{
//check for operator
boolean isOperator(String s)
{
switch(s)
{
case "+": case
"-": case "/": case "*": case "^":
return true;
}
return false;
}
//inorder traversal
void inorder(Node root)
{
if(root!=null)
{
inorder(root.lchild);
System.out.print(root.info + " ");
inorder(root.rchild);
}
}
//expression tree construction
Node expressionTreeConstruction(String postfix)
{
Stack<Node> stk = new
Stack();
String arr[] = postfix.split("
",20);
Node t = null;
for(int i=0; i<arr.length-1;
i++)
{
t = new
Node(arr[i]);
if(!isOperator(arr[i]))
{
stk.add(t);
}
else
{
Node r = stk.pop();
Node l = stk.pop();
t.lchild = l;
t.rchild = r;
stk.add(t);
}
}
return stk.pop();
}
//main method
public static void main (String[] args)
{
Scanner sc = new
Scanner(System.in);
System.out.println("Enter the
expression: ");
String infix = sc.nextLine();
InfixToPostfix ip = new
InfixToPostfix();
String postfix =
ip.infixToPostfix(infix);
System.out.println("Postfix
Expression: " + postfix);
if(postfix.equals("None"))
{
System.out.println("Invalid expession");
return ;
}
ExpressionTree etree = new
ExpressionTree();
Node root =
etree.expressionTreeConstruction(postfix);
System.out.println("Inorder
traversal of the Expression Tree: ");
etree.inorder(root);
}
}
Output:
Enter the expression:
( a + b ) * ( c - d )
Postfix Expression: a b + c d - *
Inorder traversal of the Expression Tree:
a + b * c - d
Write a program that takes as input a fully parenthesized , arithmetic expression and convert it...
(1) (50%) Write a C program that takes as input a fully parenthesized, arithmetic expression of binary operators +, -,*,/, and converts the expression into a binary expression tree. Your program should take input from the command line. The entire expression should be in a character string without any space in it An input string only includes floating numbers in the format of Y.YY, that is, one digit to the left of the decimal point and two digits to the...
Write a simple java program that takes input from the user as a fully parenthesized expression and converts it to a binary tree. Ex: (1+2 * (6-4)) or (A+B / (D-C)) When the tree is built, it should print the tree in some way.
python! Input: For this first part you will be given a set of strings that represents a fully parenthesized infix expression that eventually (next assignment) be differentiated. The expression will be composed of single digit integers ("A"-"E"), the variable "X", parenthesis "(" and ")', and the binary operators +, -, *, /, and ^ (exponentiation). No spaces. Process: Generate a binary parse tree from the given input. Output: (1) Echo print the input string. (2) Print out the parse tree...
Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3
Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...
Write in C++ Implement a program that can input an expression in postfix notation (see Exercise C-5.8) and output its value. As you can see, you will need to read Exercise C-5.8 to complete this programming task. Exercise C-5.8 Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if “(exp1) o (exp2)” is a normal fully parenthesized expression whose operation is “o”, then the postfix version of this is “pexp1pexp2o”, where...
Objective To acquire expertise in stack manipulation and management, subroutine linkage and return conventions, and recursive procedures. Description You are to create a MIPS programming assignment that returns postfix representation of the input and create an expression tree. Your MIPS program should make use of the expression tree to store the input and provide, by means of an adequate traversal, the value for the expression. Your solution should be structured according to the following steps: Step 1: Convert expression from...
The second project involves completing and extending the C++ program that evaluates statements of an expression language contained in the module 3 case study. The statements of that expression language consist of an arithmetic expression followed by a list of assignments. Assignments are separated from the expression and each other by commas. A semicolon terminates the expression. The arithmetic expressions are fully parenthesized infix expressions containing integer literals and variables. The valid arithmetic operators are +, –, *, /. Tokens...
Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression try{ Scanner inf = new Scanner (System.in); // Read input from KB/ File while(inf.hasNext()){ // read next infix expression iexp = inf.next(); // Assume method name to convert infix...
C++ Write a program that takes an infix expression as an input and produces a postfix expression. Use stack to convert an infix expression into postfix expression. Include a function that evaluates a postfix expression.