Polish Notation Prefix Postfix Infix Notation Infix Notation

  • Slides: 47
Download presentation
Polish Notation Prefix, Postfix, Infix Notation

Polish Notation Prefix, Postfix, Infix Notation

Infix Notation • To add A, B, we write A+B • To multiply A,

Infix Notation • To add A, B, we write A+B • To multiply A, B, we write A*B • The operators ('+' and '*') go in between the operands ('A' and 'B') • This is "Infix" notation.

Prefix Notation • Instead of saying "A plus B", we could say "add A,

Prefix Notation • Instead of saying "A plus B", we could say "add A, B " and write + A B • "Multiply A, B" would be written * A B • This is Prefix notation.

Postfix Notation • Another alternative is to put the operators after the operands as

Postfix Notation • Another alternative is to put the operators after the operands as in A B + and A B * • This is Postfix notation.

Pre A In B Post • The terms infix, prefix, and postfix tell us

Pre A In B Post • The terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.

Parentheses • Evaluate 2+3*5. • + First: (2+3)*5 = 5*5 = 25 • *

Parentheses • Evaluate 2+3*5. • + First: (2+3)*5 = 5*5 = 25 • * First: 2+(3*5) = 2+15 = 17 • Infix notation requires Parentheses.

What about Prefix Notation? • + 2 * 3 5 = = + 2

What about Prefix Notation? • + 2 * 3 5 = = + 2 * 3 5 = + 2 15 = 17 • * + 2 3 5 = = * + 2 3 5 = * 5 5 = 25 • No parentheses needed!

Postfix Notation • 2 3 5 * + = = 2 3 5 *

Postfix Notation • 2 3 5 * + = = 2 3 5 * + = 2 15 + = 17 • 2 3 + 5 * = = 2 3 + 5 * = 5 5 * = 25 • No parentheses needed here either!

Conclusion: • Infix is the only notation that requires parentheses in order to change

Conclusion: • Infix is the only notation that requires parentheses in order to change the order in which the operations are done.

Fully Parenthesized Expression • A FPE has exactly one set of Parentheses enclosing each

Fully Parenthesized Expression • A FPE has exactly one set of Parentheses enclosing each operator and its operands. • Which is fully parenthesized? ( A + B ) * C ( ( A + B) * C ) ( ( A + B) * ( C ) )

Infix to Prefix Conversion Move each operator to the left of its operands &

Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( ( A + B) * ( C + D ) )

Infix to Prefix Conversion Move each operator to the left of its operands &

Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: ( + A B * ( C + D ) )

Infix to Prefix Conversion Move each operator to the left of its operands &

Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + A B ( C + D )

Infix to Prefix Conversion Move each operator to the left of its operands &

Infix to Prefix Conversion Move each operator to the left of its operands & remove the parentheses: * + A B + C D Order of operands does not change!

Infix to Prefix Conversion

Infix to Prefix Conversion

Algorithm Infix to Prefix Step 1. Push “)” onto STACK, and add “(“ to

Algorithm Infix to Prefix Step 1. Push “)” onto STACK, and add “(“ to end of the A Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty Step 3. If an operand is encountered add it to B Step 4. If a right parenthesis is encountered push it onto STACK Step 5. If an operator is encountered then: a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator. b. Add operator to STACK Step 6. If left parenthesis is encountered then a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encountered) b. Remove the left parenthesis Step 7. Exit

Infix to prefix conversion Expression = (A+B^C)*D+E^5 Step 1. Reverse the infix expression. 5^E+D*)C^B+A(

Infix to prefix conversion Expression = (A+B^C)*D+E^5 Step 1. Reverse the infix expression. 5^E+D*)C^B+A( Step 2. Make Every '(' as ')' and every ')' as '(' 5^E+D*(C^B+A) Step 3. Convert expression to postfix form. A+(B*C-(D/E-F)*G)*H

Infix to prefix conversion Cont. . Expression Stack Output Comment 5^E+D*(C^B+A) Empty - Initial

Infix to prefix conversion Cont. . Expression Stack Output Comment 5^E+D*(C^B+A) Empty - Initial ^E+D*(C^B+A) Empty 5 Print E+D*(C^B+A) ^ 5 Push +D*(C^B+A) ^ 5 E Push D*(C^B+A) + 5 E^ Pop And Push *(C^B+A) + 5 E^D Print (C^B+A) +A) +* +*( +*(^ 5 E^DC 5 E^DCB Push Print A) +*(+ 5 E^DCB^ Pop And Push ) +*(+ 5 E^DCB^A Print End +* 5 E^DCB^A+ Pop Until '(' End Empty 5 E^DCB^A+*+ Pop Every element

Infix to Postfix ( ( ( A + B ) * C ) -

Infix to Postfix ( ( ( A + B ) * C ) - ( ( D + E ) / F ) ) A B + C * D E + F / • Operand order does not change! • Operators are in order of evaluation!

Computer Algorithm FPE Infix To Postfix • Assumptions: 1. Space delimited list of tokens

Computer Algorithm FPE Infix To Postfix • Assumptions: 1. Space delimited list of tokens represents a FPE infix expression 2. Operands are single characters. 3. Operators +, -, *, /

Algorithm Infix To Postfix • Algorithm 1. Scan the infix expression from left to

Algorithm Infix To Postfix • Algorithm 1. Scan the infix expression from left to right. 2. If the scanned character is an operand, output it. 3. Else, …. . 3. 1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it. …. . 3. 2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack. ) 4. If the scanned character is an ‘(‘, push it to the stack. 5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis. 6. Repeat steps 2 -6 until infix expression is scanned. 7. Print the output 8. Pop and output from the stack until it is not empty.

FPE Infix to Postfix ( ( ( A + B ) * ( C

FPE Infix to Postfix ( ( ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: <empty> • output: []

FPE Infix to Postfix ( ( A + B ) * ( C -

FPE Infix to Postfix ( ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( • output: []

FPE Infix to Postfix ( A + B ) * ( C - E

FPE Infix to Postfix ( A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( • output: []

FPE Infix to Postfix A + B ) * ( C - E )

FPE Infix to Postfix A + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( • output: []

FPE Infix to Postfix + B ) * ( C - E ) )

FPE Infix to Postfix + B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( • output: [A]

FPE Infix to Postfix B ) * ( C - E ) ) /

FPE Infix to Postfix B ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( + • output: [A]

FPE Infix to Postfix ) * ( C - E ) ) / (

FPE Infix to Postfix ) * ( C - E ) ) / ( F + G ) ) • stack: ( ( ( + • output: [A B]

FPE Infix to Postfix * ( C - E ) ) / ( F

FPE Infix to Postfix * ( C - E ) ) / ( F + G ) ) • stack: ( ( • output: [A B + ]

FPE Infix to Postfix ( C - E ) ) / ( F +

FPE Infix to Postfix ( C - E ) ) / ( F + G ) ) • stack: ( ( * • output: [A B + ]

FPE Infix to Postfix C - E ) ) / ( F + G

FPE Infix to Postfix C - E ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + ]

FPE Infix to Postfix - E ) ) / ( F + G )

FPE Infix to Postfix - E ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + C ]

FPE Infix to Postfix E ) ) / ( F + G ) )

FPE Infix to Postfix E ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + C ]

FPE Infix to Postfix ) ) / ( F + G ) ) •

FPE Infix to Postfix ) ) / ( F + G ) ) • stack: ( ( * ( • output: [A B + C E ]

FPE Infix to Postfix ) / ( F + G ) ) • stack:

FPE Infix to Postfix ) / ( F + G ) ) • stack: ( ( * • output: [A B + C E - ]

FPE Infix to Postfix / ( F + G ) ) • stack: (

FPE Infix to Postfix / ( F + G ) ) • stack: ( • output: [A B + C E - * ]

FPE Infix to Postfix ( F + G ) ) • stack: ( /

FPE Infix to Postfix ( F + G ) ) • stack: ( / • output: [A B + C E - * ]

FPE Infix to Postfix F + G ) ) • stack: ( / (

FPE Infix to Postfix F + G ) ) • stack: ( / ( • output: [A B + C E - * ]

FPE Infix to Postfix + G ) ) • stack: ( / ( •

FPE Infix to Postfix + G ) ) • stack: ( / ( • output: [A B + C E - * F ]

FPE Infix to Postfix G ) ) • stack: ( / ( + •

FPE Infix to Postfix G ) ) • stack: ( / ( + • output: [A B + C E - * F ]

FPE Infix to Postfix ) ) • stack: ( / ( + • output:

FPE Infix to Postfix ) ) • stack: ( / ( + • output: [A B + C E - * F G ]

FPE Infix to Postfix ) • stack: ( / • output: [A B +

FPE Infix to Postfix ) • stack: ( / • output: [A B + C E - * F G + ]

FPE Infix to Postfix • stack: <empty> • output: [A B + C E

FPE Infix to Postfix • stack: <empty> • output: [A B + C E - * F G + / ]

Problem with FPE • Too many parentheses. • Establish precedence rules: My Dear Aunt

Problem with FPE • Too many parentheses. • Establish precedence rules: My Dear Aunt Sally • We can alter the previous program to use the precedence rules.

Infix to Postfix • Initialize a Stack for operators, output list • Split the

Infix to Postfix • Initialize a Stack for operators, output list • Split the input into a list of tokens. • for each token (left to right): if it is operand: append to output if it is '(': push onto Stack if it is ')': pop & append till '(' if it in '+-*/': while peek has precedence ≥ it: pop & append push onto Stack pop and append the rest of the Stack.

Evaluating Postfix Expressions • Algorithm • Step through the expression from left to right,

Evaluating Postfix Expressions • Algorithm • Step through the expression from left to right, getting one token at a time. • Whenever the token is an operand, stack the operand in the order encountered. • When an operator is encountered: • If the operator is binary, then pop the stack twice • If the operator is unary (e. g. unary minus), pop once • Perform the indicated operation on the operator(s) • Push the result back on the stack. • At the end of the expression, the top of the stack will have the correct value for the expression.

Evaluating Postfix Expressions

Evaluating Postfix Expressions