Conversion: Prefix To Postfix and Infix

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

CONVERSION:

PREFIX TO POSTFIX
AND INFIX
BY- RIYA GUPTA 20CSU269

SANWAR SHARDA 20CSU291


Infix notation is the common arithmetic and
logical formula notation, in which operators
are written infix-style between the operands
they act on
INFIX
NOTATION
E.g. A + B
PREFIX NOTATION

The Infix expression


In Prefix notation, the
A+B will be written as Prefix is also called
operator comes before
+AB in its Prefix 'Polish Notation'
the operand.
Notation.
PREFIX TO
INFIX
ALGORITHM

Scan the given prefix But if the character is an


If the character is an operand,
expression from right to operator, pop the top two
push it into the stack.
left character by character. values from stack.

Concatenate this operator Repeat this process untill the


with these two values (1st top Now push this resulting end of prefix expression.
value+operator+2nd top string back into the stack. Now the value in the stack is
value) to get a new string. the desired infix expression.
LET’S TAKE AN EXAMPLE…
PREFIX:
*+abc cba+*

a
b a+b
c c a+b*c
public static String convert(String str){
    Stack<String> stack = new Stack<>();

CODE     int l = str.length();


    for(int i = l - 1; i >= 0; i--){
        char c = str.charAt(i);
        if (isOperator(c))
        {
            String op1 = stack.pop();
static    boolean isOperator(char x)             String op2 = stack.pop();
{              
    switch(x)            String temp = "(" + op1 + c + op2 +")";
    {             stack.push(temp);
        case '+':         }
        case '-':         else
        case '*':         {
        case '/':           
            return true;             stack.push(c + "");
    }         }
    return false;     }
}     return stack.pop();
}
In Postfix notation, the operator comes
after the Operand.

POSTFIX For example, the Infix expression A+B will


NOTATION be written as AB+ in its Postfix Notation.

Postfix is also called 'Reverse Polish


Notation'
PREFIX TO POSTFIX
Scan the given prefix expression from right to left character by
character.

If the character is an operand, push it into the stack.

But if the character is an operator, pop the top two values from stack.

ALGORITHM Concatenate this operator with these two values (operator+1stst top


value+2nd
nd top value) to get a new string.

Now push this resulting string back into the stack.

Repeat this process untill the end of prefix expression. Now the value in
the stack is the desired postfix expression.
LET’S TAKE AN EXAMPLE…
PREFIX:
*+ab+cd dc+ba+*

a
c b ab+
d cd+ cd+ ab+ cd+*
public static String convert(String str){
    Stack<String> stack = new Stack<>();

CODE     int l = str.length();


    for(int i = l - 1; i >= 0; i--){
        char c = str.charAt(i);
        if (isOperator(c))
        {
            String op1 = stack.pop();
static    boolean isOperator(char x)             String op2 = stack.pop();
{              
    switch(x)            String temp = op1 + op2 + c ;
    {             stack.push(temp);
        case '+':         }
        case '-':         else
        case '*':         {
        case '/':             stack.push(c + "");
            return true;         }
    }     }
    return false;     return stack.pop();
} }
Thank you!!!

You might also like