Question
This program take infix as an input, then convert into postfix and prefix. Please add a method to calculate the value of the INFIX expression
This program take infix as an input, then convert into postfix and prefix. Please add a method to calculate the value of the INFIX expression in Java and at the end print output of infix, postfix prefix and the result. Thank you
import java.io.*;
class Node
{
public char data;
public Node lftChild;
public Node rhtChild;
public Node(char x)
{
data = x;
}
public void displayNode()
{
System.out.print(data);
}
}
class Stack
{
private Node[] a;
private int top, m;
public Stack(int max)
{
m = max;
a = new Node[m];
top = -1;
}
public void push(Node key)
{
a[++top] = key;
}
public Node pop()
{
return (a[top--]);
}
public boolean isEmpty()
{
return (top == -1);
}
}
class Stacktwo
{
private char[] a;
private int top, m;
public Stacktwo(int max)
{
m = max;
a = new char[m];
top = -1;
}
public void push(char key)
{
a[++top] = key;
}
public char pop()
{
return (a[top--]);
}
public boolean isEmpty()
{
return (top == -1);
}
}
class Conversion
{
private Stacktwo s;
private String input;
private String output = "";
public Conversion(String str)
{
input = str;
s = new Stacktwo(str.length());
}
public String inToPost()
{
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
switch (ch)
{
case '+':
case '-':
gotOperator(ch, 1);
break;
case '*':
case '/':
gotOperator(ch, 2);
break;
case '(':
s.push(ch);
break;
case ')':
gotParenthesis();
break;
default:
output = output + ch;
}
}
while (!s.isEmpty())
output = output + s.pop();
return output;
}
private void gotOperator(char opThis, int prec1)
{
while (!s.isEmpty())
{
char opTop = s.pop();
if (opTop == '(')
{
s.push(opTop);
break;
} else
{
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1)
{
s.push(opTop);
break;
} else
output = output + opTop;
}
}
s.push(opThis);
}
private void gotParenthesis()
{
while (!s.isEmpty())
{
char ch = s.pop();
if (ch == '(')
break;
else
output = output + ch;
}
}
}
class Tree
{
private Node root;
public Tree()
{
root = null;
}
public void insert(String s)
{
Conversion c = new Conversion(s);
s = c.inToPost();
Stack stack = new Stack(s.length());
s = s + "#";
int i = 0;
char symbol = s.charAt(i);
Node newNode;
while (symbol != '#')
{
if (symbol >= '0' && symbol <= '9' || symbol >= 'A'
&& symbol <= 'Z' || symbol >= 'a' && symbol <= 'z')
{
newNode = new Node(symbol);
stack.push(newNode);
} else if (symbol == '+' || symbol == '-' || symbol == '/'
|| symbol == '*')
{
Node ptr1 = stack.pop();
Node ptr2 = stack.pop();
newNode = new Node(symbol);
newNode.lftChild = ptr2;
newNode.rhtChild = ptr1;
stack.push(newNode);
}
symbol = s.charAt(++i);
}
root = stack.pop();
}
public void traverse(int type)
{
switch (type)
{
case 1:
System.out.print("Preorder Expression: ");
preOrder(root);
break;
case 2:
System.out.print("Inorder Expression: ");
inOrder(root);
break;
case 3:
System.out.print("Postorder Expression: ");
postOrder(root);
break;
default:
System.out.println("Invalid Choice");
}
}
private void preOrder(Node localRoot)
{
if (localRoot != null)
{
localRoot.displayNode();
preOrder(localRoot.lftChild);
preOrder(localRoot.rhtChild);
}
}
private void inOrder(Node localRoot)
{
if (localRoot != null)
{
inOrder(localRoot.lftChild);
localRoot.displayNode();
inOrder(localRoot.rhtChild);
}
}
private void postOrder(Node localRoot)
{
if (localRoot != null)
{
postOrder(localRoot.lftChild);
postOrder(localRoot.rhtChild);
localRoot.displayNode();
}
}
}
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