0% found this document useful (0 votes)
56 views7 pages

Infix To Prefix Expression

Uploaded by

grojamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views7 pages

Infix To Prefix Expression

Uploaded by

grojamani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Convert Infix to Prefix Expression

Example:
Input: Infix expression - A + B
Output: Prefix expression- +AB

Input: Infix expression - A+B*(C^D-E)


Output: Prefix expression- +A*B-^CDE

Order of precedence of operations–


1. ^ (Exponential)
2. / *
3. + –
Note: brackets ( ) are used to override these rules.
Algorithm:
 Reverse the given infix expression. ( Note: do another reversal only
for brackets).
 Do Infix to postfix expression and get the result.
 Reverse the result to get the final expression. (prefix expression) .
please click here to read about Infix expression to postfix expression.
Please see the walkthrough of an example below for more understanding.

import java.util.Stack;
public class InfixToPreFix {
static int precedence(char c){
switch (c){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}

static StringBuilder infixToPreFix(String expression){

StringBuilder result = new StringBuilder();


/* Java StringBuilder class is used to create mutable
(modifiable) string. The Java StringBuilder class is same
as StringBuffer class In a mutable string, we can change the
value of string and JVM doesn’t create a new object. In a mutable
string, we can change the value of the string in the same object.
*/
StringBuilder input = new StringBuilder(expression);
input.reverse();
Stack<Character> stack = new Stack<Character>();

char [] charsExp = new String(input).toCharArray();


for (int i = 0; i < charsExp.length; i++) {

if (charsExp[i] == '(') {
charsExp[i] = ')';
i++;
}
else if (charsExp[i] == ')') {
charsExp[i] = '(';
i++;
}
}
for (int i = 0; i <charsExp.length ; i++) {
char c = charsExp[i];

//check if char is operator or operand


if(precedence(c)>0){
while(stack.isEmpty()==false &&
precedence(stack.peek())>=precedence(c)){
result.append(stack.pop());
}
stack.push(c);
}else if(c==')'){
char x = stack.pop();
while(x!='('){
result.append(x);
x = stack.pop();
}
}else if(c=='('){
stack.push(c);
}else{
//character is neither operator nor "("
result.append(c);
}
}

for (int i = 0; i <=stack.size() ; i++) {


result.append(stack.pop());
}
return result.reverse();
}

public static void main(String[] args) {


String exp = "A+B*(C^D-E)";
System.out.println("Infix Expression: " + exp);
System.out.println("Prefix Expression: " + infixToPreFix(exp));
}
}
OUTPUT: Infix Expression: A+B*(C^D-E)
Prefix Expression: +A*B-^CDE

Convert Prefix to Infix


Expression
Example:
Input: Prefix expression: + A B
Output: Infix expression- (A + B)

Input: Prefix expression: *-A/BC-/AKL


Output: Infix expression: ((A-(B/C))*((A/K)-L))
Approach: Use Stacks
Algorithm:
Iterate the given expression from right to left (in reverse order), one
character at a time

1. If character is operand, push it to stack.


2. If character is operator,
1. pop operand from stack, say it’s s1.
2. pop operand from stack, say it’s s2.
3. perform (s1 operator s2) and push it to stack.
3. Once the expression iteration is completed, initialize
result string and pop out from stack and add it to result.
4. Return the result.
Please walk through the example below for more understanding.

import java.util.Stack;
public class PreFixToInFix {
public String convert(String expression){
Stack<String> stack = new Stack<>();
for (int i = expression.length()-1; i >=0 ; i--) {
char c = expression.charAt(i);

if(isOperator(c)){
String s1 = stack.pop();
String s2 = stack.pop();
String temp = "("+s1+c+s2+")";
stack.push(temp);
}else{
stack.push(c+"");
}
}

String result=stack.pop();

return result;
}

boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
public static void main(String[] args) {
String exp = "*-A/BC-/AKL";
System.out.println("Prefix Expression: " + exp);
System.out.println("Infix Expression: " + new
PreFixToInFix().convert(exp));
}
}

OUTPUT: Prefix Expression: *-A/BC-/AKL


Infix Expression: ((A-(B/C))*((A/K)-L))

You might also like