Infix-Postfix expression conversion

Rashmiranja625 3,628 views 14 slides Jan 01, 2017
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Here is a presentation of conversion of infix-postfix expression using stack


Slide Content

Seminar on “infix-postfix conversion”

contents Introduction What is expression ? Infix Expression Postfix Expression Priority table Algorithm for conversion Reference

Introduction In this presentation we are going to read about how to convert infix expression to postfix expression with the help of STACK . A S tack is a linear data structure in which item may be inserted and deleted at one end called TOP. The operational meaning of stack is LIFO i.e. Last In Fast Out. This means the last element is inserted to the stack is the first element to be deleted.

What is Expression ? An expression is any legal combination of operators and operands that represents a value. Operands are values and Operators are symbols that represent particular actions. Ex:- a+b,p *q etc.

Infix expression This expression is common expression for writing arithmetic expression. In infix expression the binary operator symbol is placed between its two operands. Ex:-A+B,P*Q,(X+Y)/Z

Postfix expression In postfix expression the binary operator symbol placed after its two operands. This expression is also called ‘Reverse polish expression’. It doesn’t require parenthesis to determine the order of precedence. Ex:-AB+,PQ*,XY+Z/

Priority table Priority level Operators 1. Unary +,Unary -,NOT 2. ^ 3. /,* 4. +,- 5. <,>,<=,>= 6. ==,!= 7. && 8. ||

Algorithm for conversion POSTFIX(Q,P):Here Q is an infix expression.This algorithm finds the equivalent postfix expression P. Step 1:Take an empty stack ‘S’. Step 2:Push ‘(’ to stack ‘S’ and add ‘)’ to given infix expression. Step 3:Scan infix expression for left to right & repeat step 4-7 for each element of infix expression until stack is empty

Step 4:If an OPERAND is encountered, add it to postfix expression. Step 5:If LEFT PARENTHESIS is encountered push it into stack .

Step 6:If an OPERATOR is encountered, then a. Repeatedly POP from stack and add to postfix expression, each operator which has same or as higher precedence then the scanned operator. b.Push the scanned operator to stack [End of step 6 if structure]

Step 7:If a RIGHT PARENTHESIS is encountered then: a.Repeatedly POP from stack and add to postfix expression,until a left parethesis is encountered. b.Remove the left parethesis from stack. [End of step 7 if structure] [End of step 3 loop] Step 8:Exit

Q:- a+c *(d-e)^(b+c)-f) Scanned item Stack Postfix expression ( a ( a + (,+ a c (,+ ac * (,+,* ac ( (,+,*,( ac d (,+,*,( acd - (,+,*,(,- acd e (,+,*,(,- acde ) (,+,* acde- ^ (,+,*,^ acde- ( (,+,*,^,( acde- b (,+,*,^,( acde-b + (,+,*,^,(,+ acde-b

Scanned item Stack Postfix expression c (,+,*,^,(,+ acde-bc ) (,+,*,^ acde-bc+ - (,- acde—bc+^*+ f (,- acde-bc +*^+f ) acde-bc +*^+f- Postfix expression is acde-bc +*^+f-

THANK YOU