0% found this document useful (0 votes)
407 views

Prefix, Postfix, Infix Notation: University College of Engineering, Kanchipuram

The document discusses different notations for representing mathematical expressions: infix, prefix, and postfix. In infix notation, operators are between operands (e.g. A + B). In prefix notation, operators come before operands (e.g. + A B). In postfix notation, operators follow operands (e.g. A B +). It also describes how to convert between these notations and discusses precedence rules to avoid needing parentheses.

Uploaded by

Sathish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
407 views

Prefix, Postfix, Infix Notation: University College of Engineering, Kanchipuram

The document discusses different notations for representing mathematical expressions: infix, prefix, and postfix. In infix notation, operators are between operands (e.g. A + B). In prefix notation, operators come before operands (e.g. + A B). In postfix notation, operators follow operands (e.g. A B +). It also describes how to convert between these notations and discusses precedence rules to avoid needing parentheses.

Uploaded by

Sathish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Prefix, Postfix, Infix Notation

University College of Engineering, Kanchipuram

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,B " and write +AB "Multiply A,B" would be written *AB This is Prefix notation.

Postfix Notation
Another

alternative is to put the operators after the operands as in AB+ and AB* This is Postfix notation.

The

terms infix, prefix, and postfix tell us whether the operators go between, before, or after the operands.

Parentheses
Evaluate +

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

First:

2+3*5.

What about Prefix Notation?


+2*35= =+2*35 = + 2 15 = 17 * +2 35 = =*+235 = * 5 5 = 25 No parentheses needed!

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

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 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 & remove the parentheses: ( ( A + B) * ( C + D ) )

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 & remove the parentheses: *+A B (C+D)

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


1. 2. 3.

Assumptions: Space delimited list of tokens represents a FPE infix expression Operands are single characters. Operators +,-,*,/

FPE Infix To Postfix


Initialize

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

a Stack for operators,

FPE Infix to Postfix


(((A+B)*(C-E))/(F+G))
stack:

<empty> output: []

FPE Infix to Postfix


((A+B)*(C-E))/(F+G))
stack:

( output: []

FPE Infix to Postfix


(A+B)*(C-E))/(F+G))
stack:

(( output: []

FPE Infix to Postfix


A+B)*(C-E))/(F+G))
stack:

((( output: []

FPE Infix to Postfix


+B)*(C-E))/(F+G))
stack:

((( output: [A]

FPE Infix to Postfix


B)*(C-E))/(F+G))
stack:

(((+ output: [A]

FPE Infix to Postfix


)*(C-E))/(F+G))
stack:

(((+ output: [A B]

FPE Infix to Postfix


*(C-E))/(F+G))
stack:

(( output: [A B + ]

FPE Infix to Postfix


(C-E))/(F+G))
stack:

((* output: [A B + ]

FPE Infix to Postfix


C-E))/(F+G))
stack:

((*( output: [A B + ]

FPE Infix to Postfix


-E))/(F+G))
stack:

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

FPE Infix to Postfix


E))/(F+G))
stack:

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

FPE Infix to Postfix


))/(F+G))
stack:

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

FPE Infix to Postfix


)/(F+G))
stack:

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

FPE Infix to Postfix


/(F+G))
stack:

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

FPE Infix to Postfix


(F+G))
stack:

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

FPE Infix to Postfix


F+G))
stack:

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

FPE Infix to Postfix


+G))
stack:

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

FPE Infix to Postfix


G))
stack:

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

FPE Infix to Postfix


))
stack:

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

FPE Infix to Postfix


)
stack:

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

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 Sally We can alter the previous program to use the precedence rules.

Infix to Postfix
Initialize

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.

a Stack for operators, output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy