0% found this document useful (0 votes)
25 views34 pages

Infix, Postfix, Prefix

The document discusses stack expressions and different notations including infix, prefix, and postfix. It provides examples and rules for converting between these notations using a stack. Applications include mathematical expressions and programming languages.

Uploaded by

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

Infix, Postfix, Prefix

The document discusses stack expressions and different notations including infix, prefix, and postfix. It provides examples and rules for converting between these notations using a stack. Applications include mathematical expressions and programming languages.

Uploaded by

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

Applications of stack

Expressions
• An expression consists of constants, variables, and symbols. Symbols
can be operators or parenthesis. All these components must be
arranged according to a set of rules so that all these expressions can
be evaluated using the set of rules.
• Examples of expressions are:
• 5+6
• A-B
• (P * 5)
What is infix notation?

• When the operator is written in between the operands, then it is


known as infix notation. Operand does not have to be always a
constant or a variable; it can also be an expression itself.
• For example,
• (p + q) * (r + s)
• In the above expression, both the expressions of the multiplication
operator are the operands, i.e., (p + q), and (r + s) are the operands.
• Syntax of infix notation is given below:
• <operand> <operator> <operand>
• the order of the operator precedence is given in the below table:

The first preference is given to the parenthesis; then next


preference is given to the exponents. In the case of multiple
exponent operators, then the operation will be applied from
right to left
• After exponent, multiplication, and division operators are evaluated. If
both the operators are present in the expression, then the operation
will be applied from left to right.
• The next preference is given to addition and subtraction. If both the
operators are available in the expression, then we go from left to
right.
What is Prefix notation?

• A prefix notation is another form of expression but it does not require


other information such as precedence and associativity, whereas an
infix notation requires information of precedence and associativity. It
is also known as polish notation. In prefix notation, an operator
comes before the operands. The syntax of prefix notation is given
below:
• <operator> <operand> <operand>
• For example, if the infix expression is 5+1, then the prefix expression
corresponding to this infix expression is +51.
• If the infix expression is:
•a*b+c
• *ab+c
• +*abc (Prefix expression)
• Consider another example:
• First scan: In the above expression, multiplication operator has a higher
precedence than the addition operator; the prefix notation of B*C would be
(*BC).
• A + *BC
• Second scan: In the second scan, the prefix would be:
• +A *BC
• In the above expression, we use two scans to convert infix to prefix
expression. If the expression is complex, then we require a greater number of
scans. We need to use that method that requires only one scan, and provides
the desired result. If we achieve the desired output through one scan, then
the algorithm would be efficient. This is possible only by using a stack.
• A+B*C/(E-F)
• +A*B/C-EF(prefix)
Postfix Expression

• The postfix expression is an expression in which the operator is written after the
operands. For example, the postfix expression of infix notation ( 2+3) can be
written as 23+.
• For example:
• If the infix expression is A + B * C
• As we know that the multiplication operator has a higher precedence than the
addition operator, so multiplication operator will move after the operands B and
C shown as below:
• A+BC*
• Once the multiplication operator is moved after the operand C, then the addition
operator will come after the multiplication operator shown as below:
• ABC*+
Postfix Expression
• Some key points regarding the postfix expression are:
• In postfix expression, operations are performed in the order in which
they have written from left to right.
• It does not any require any parenthesis.
• We do not need to apply operator precedence rules and associativity
rules.
Examples post to infix
• Input : abc++
• Output : (a + (b + c))

• Input : ab*c+
• Output : ((a*b)+c)

• bc*cd+-(postfix)
• ((b*c)-(c+d))(infix)
Example prefix to infix
• Input : Prefix : *+AB-CD
• Output : Infix : ((A+B)*(C-D))

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


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

• *+a-bc/-de+-fgh(prefix)
• (a+b-c)*(d-e)/(f-g+h)(infix)
Rules for the conversion from infix to postfix expression using
stack
• Scan all the symbols one by one from left to right in the given Infix Expression.
• If the reading symbol is operand, then immediately append it to the Postfix
Expression .
• If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
• If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack
until the respective left parenthesis is popped and append each popped symbol to
Postfix Expression.
• If the reading symbol is operator (+ , – , * , /), then Push it onto the Stack.
However, first pop the operators which are already on the stack that have higher
or equal precedence than the current operator and append them to the postfix .
• If open parenthesis is there on top of the stack then push the operator into the
stack.
• If the input is over, pop all the remaining symbols from the stack and append them
to the postfix .
Infix to prefix using stack
• initially reverse the expression given for the infix.
• One by one scan of characters.
• Is if character is an operand, then copy it to the output of the prefix notation.
• When the character is a parenthesis closing then push it to the stack.
• If the character is an opening parenthesis, pop the elements in the stack till we find the
closing parenthesis that corresponds.
• If a character scanned is operator.
• If the operator has greater or equal precedence than the top of the stack, push the
operator to the stack.
• If the operator has less precedence than the top of the stack, pop the operator and
output it to the output of the prefix notation, then check the above condition with the
new top of the stack again.
• After scanning all the characters, reverse the notation output for the prefix.

You might also like