KEMBAR78
data structure stack appplication in python | PPTX
Infix to Prefix
STACK APPLICATION
Terminology
• Infix: The notation commonly used in mathematical formulae
• Operand: The value on which an operator is performed
• Operator: A symbol like minus that shows an operation
• Postfix: A mathematical notation in which operators follow operands
• Prefix: A mathematical notation in which operands follow operators.
• Reverse Polish Notation (RPN): A mathematical notation in which
operators follow operands
Example
• Infix: ( A+B)*C-D+F
• Prefix: -*+ABC+D
• Postfix: AB+C*D-F+
Converting an infix expression to a prefix expression and infix to postfix expression involves changing
the order of operators and operands while maintaining the precedence and associativity of the
operators.
Infix to prefix conversion using stack
• The steps are:
Reverse the infix expression.
Replace '(' with ')' and ')' with '(' to maintain the correct grouping.
Convert the reversed infix expression to postfix notation.
Reverse the postfix expression to get the prefix expression.
Steps are :
Reverse the infix expression.
 Replace '(' with ')' and ')' with ‘(‘.
Convert the reversed infix expression to postfix notation .
Reverse the postfix expression to get the prefix expression: Prefix
Expression.
Convert infix to postfix
• If character is operand then add them to the output expression.
• If character is ‘)’ then Pop operators from the stack until an open
parenthesis is encountered and add them to the output expression.
• Discard the open parenthesis.
• If character is operator then push onto the stack.
• Check the precendence of operator with stack top element is operator.
1.If precendence of operator is higher than stack top element then operator
just push onto the stack.
2. If precendence of operator is lower than stack top element then pop stack
top element operator add them to the output expression.
Infix to prefix example
• Example: (A+B)*C-D+F
• Reverse expression : F+D-C*)B+A(
• Replace ) with ( and ) with (
• Final Reverse string :F+D-C*(B+A)
Example of infix to prefix expression
Steps Current
character
Operation on stack Stack
representation
Postfix expression
1 F F
2 + PUSH + F
3 D FD
4 - PUSH – AND POP +
(Because + has higher
precedence than -.)
- FD+
5 C FD+C
6 * PUSH *
(Because - has lower
precedence than *)
-* FD+C
7 ( PUSH -*( FD+C
F+D-C*(B+A)
Example of infix to prefix expression
Steps Current
character
Operation on stack Stack
representation
Postfix expression
8 B -*( FD+CB
9 + PUSH + -*(+ FD+CB
10 A FD+CBA
11 ) POP + ,( FD+CBA+
12 POP * FD+CBA+*
POP - FD+CBA+*-
Empty stack Final postfix
expression :
FD+CBA+*-
Reverse the postfix expression we get prefix expression :
-*+ABC+DF

data structure stack appplication in python

  • 1.
  • 2.
    Terminology • Infix: Thenotation commonly used in mathematical formulae • Operand: The value on which an operator is performed • Operator: A symbol like minus that shows an operation • Postfix: A mathematical notation in which operators follow operands • Prefix: A mathematical notation in which operands follow operators. • Reverse Polish Notation (RPN): A mathematical notation in which operators follow operands
  • 3.
    Example • Infix: (A+B)*C-D+F • Prefix: -*+ABC+D • Postfix: AB+C*D-F+ Converting an infix expression to a prefix expression and infix to postfix expression involves changing the order of operators and operands while maintaining the precedence and associativity of the operators.
  • 4.
    Infix to prefixconversion using stack • The steps are: Reverse the infix expression. Replace '(' with ')' and ')' with '(' to maintain the correct grouping. Convert the reversed infix expression to postfix notation. Reverse the postfix expression to get the prefix expression.
  • 5.
    Steps are : Reversethe infix expression.  Replace '(' with ')' and ')' with ‘(‘. Convert the reversed infix expression to postfix notation . Reverse the postfix expression to get the prefix expression: Prefix Expression.
  • 6.
    Convert infix topostfix • If character is operand then add them to the output expression. • If character is ‘)’ then Pop operators from the stack until an open parenthesis is encountered and add them to the output expression. • Discard the open parenthesis. • If character is operator then push onto the stack. • Check the precendence of operator with stack top element is operator. 1.If precendence of operator is higher than stack top element then operator just push onto the stack. 2. If precendence of operator is lower than stack top element then pop stack top element operator add them to the output expression.
  • 7.
    Infix to prefixexample • Example: (A+B)*C-D+F • Reverse expression : F+D-C*)B+A( • Replace ) with ( and ) with ( • Final Reverse string :F+D-C*(B+A)
  • 8.
    Example of infixto prefix expression Steps Current character Operation on stack Stack representation Postfix expression 1 F F 2 + PUSH + F 3 D FD 4 - PUSH – AND POP + (Because + has higher precedence than -.) - FD+ 5 C FD+C 6 * PUSH * (Because - has lower precedence than *) -* FD+C 7 ( PUSH -*( FD+C F+D-C*(B+A)
  • 9.
    Example of infixto prefix expression Steps Current character Operation on stack Stack representation Postfix expression 8 B -*( FD+CB 9 + PUSH + -*(+ FD+CB 10 A FD+CBA 11 ) POP + ,( FD+CBA+ 12 POP * FD+CBA+* POP - FD+CBA+*- Empty stack Final postfix expression : FD+CBA+*- Reverse the postfix expression we get prefix expression : -*+ABC+DF