41
Prefix, Postfix, Infix Notation

Prefix, Postfix, Infix Notation. Infix Notation To add A, B, we write A+B To multiply A, B, we write A*B The operators ('+' and '*') go in between

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Prefix, Postfix, Infix Notation

Page 2: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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.

Page 3: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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.

Page 4: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Postfix Notation Another alternative is to put the

operators after the operands as inA B +

andA B *

This is Postfix notation.

Page 5: Prefix, Postfix, Infix Notation. 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 terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.

Pre A In B Post

Page 6: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Parentheses Evaluate 2+3*5. + First:

(2+3)*5 = 5*5 = 25 * First:

2+(3*5) = 2+15 = 17 Infix notation requires Parentheses.

Page 7: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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!

Page 8: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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!

Page 9: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Conclusion: Infix is the only notation that

requires parentheses in order to change the order in which the operations are done.

Page 10: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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 ) )

Page 11: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

operands & remove the parentheses:

( ( A + B) * ( C + D ) )

Page 12: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

operands & remove the parentheses:

( + A B * ( C + D ) )

Page 13: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

operands & remove the parentheses:

* + A B ( C + D )

Page 14: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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!

Page 15: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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!

Page 16: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Computer Algorithm FPE Infix To Postfix

Assumptions: 1. Space delimited list of tokens

represents a FPE infix expression2. Operands are single characters.3. Operators +,-,*,/

Page 17: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE 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 '('

Page 18: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: <empty> output: []

Page 19: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( output: []

Page 20: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( output: []

Page 21: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to PostfixA + B ) * ( C - E ) ) / ( F + G ) )

stack: ( ( ( output: []

Page 22: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( ( output: [A]

Page 23: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( ( + output: [A]

Page 24: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( ( + output: [A B]

Page 25: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( output: [A B + ]

Page 26: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( * output: [A B + ]

Page 27: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( * ( output: [A B + ]

Page 28: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( * ( output: [A B + C ]

Page 29: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to PostfixE ) ) / ( F + G ) )

stack: ( ( * ( - output: [A B + C ]

Page 30: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( * ( - output: [A B + C E ]

Page 31: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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

stack: ( ( * output: [A B + C E - ]

Page 32: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix/ ( F + G ) )

stack: ( output: [A B + C E - * ]

Page 33: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix( F + G ) )

stack: ( / output: [A B + C E - * ]

Page 34: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to PostfixF + G ) )

stack: ( / ( output: [A B + C E - * ]

Page 35: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix+ G ) )

stack: ( / ( output: [A B + C E - * F ]

Page 36: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix G ) )

stack: ( / ( + output: [A B + C E - * F ]

Page 37: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix) )

stack: ( / ( + output: [A B + C E - * F G ]

Page 38: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix)

stack: ( / output: [A B + C E - * F G + ]

Page 39: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

FPE Infix to Postfix

stack: <empty> output: [A B + C E - * F G + / ]

Page 40: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

Problem with FPE Too many parentheses. Establish precedence rules:

My Dear Aunt Sally We can alter the previous program

to use the precedence rules.

Page 41: Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between

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 Stackpop and append the rest of the Stack.