SlideShare a Scribd company logo
1 of 5
Download to read offline
Packet 15, page 1
INFIX,
PREFIX, AND
POSTFIX
EXPRESSIONS
Packet 15, page 2
KHumans usually write algebraic expressions
like this:
a + b
KThis is called infix notation, because the
operator (“+”) is inside the expression
KA problem is that we need parentheses or
precedence rules to handle more
complicated expressions:
a + b * c = (a + b) * c ?
= a + (b * c) ?
Packet 15, page 3
Infix, postfix, and prefix notation
KThere is no reason we can’t place the
operator somewhere else.
Kinfix notation:
a + b
Kprefix notation:
+ a b
Kpostfix notation:
a b +
Packet 15, page 4
Other names
KPrefix notation was introduced by the
Polish logician Lukasiewicz, and is
sometimes called “Polish notation”
KPostfix notation is sometimes called
“reverse Polish notation” or RPN
»Used on some calculators
(the ones without '=' signs)
Packet 15, page 5
KQuestion: Why would anyone ever want to
use anything so “unnatural,” when infix
seems to work just fine?
KAnswer: With postfix and prefix notations,
parentheses are no longer needed!
infix postfix prefix
(a + b) * c a b + c * * + a b c
a + (b * c) a b c * + + a * b c
Packet 15, page 6
Converting
from infix notation to postfix notation
KAssume that your infix expression is of the
form
<identifier> <operator> <identifier>
KA postfix expression is created by rewriting
this as
<identifier> <identifier> <operator>
Packet 15, page 7
Convert these infix expressions to
postfix notation
Kx
Kx + y
K(x + y) - z
Kw * ((x + y) - z)
K(2 * a) / ((a + b) * (a - c))
Packet 15, page 8
Convert these postfix expressions to
infix notation
K3 r -
K1 3 r - +
Ks t * 1 3 r - + +
Kv w x y z * - + *
Packet 15, page 9
A stack-based algorithm to
evaluate a postfix expression
for each character C in a given string, proceeding left to right
{
if C is an operand
push C onto stack
else // C is an operator
{
pop item from stack, and store in Opr2
pop item from stack, and store in Opr1
result = Opr1 C Opr2, using C as an operator
push result onto stack
}
}

More Related Content

What's hot

What's hot (17)

Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9
 
Practical no 2
Practical no 2Practical no 2
Practical no 2
 
15CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 115CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 1
 
A nice 64-bit error in C
A  nice 64-bit error in CA  nice 64-bit error in C
A nice 64-bit error in C
 
Write declarations for each of the following variables: a. amounts is a...
Write declarations for each of the following variables:       a. amounts is a...Write declarations for each of the following variables:       a. amounts is a...
Write declarations for each of the following variables: a. amounts is a...
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3
 
2.7 normal forms cnf & problems
2.7 normal forms  cnf & problems2.7 normal forms  cnf & problems
2.7 normal forms cnf & problems
 
Hello world program
Hello world programHello world program
Hello world program
 
3.1,2,3 pushdown automata definition, moves &amp; id
3.1,2,3 pushdown automata   definition, moves &amp; id3.1,2,3 pushdown automata   definition, moves &amp; id
3.1,2,3 pushdown automata definition, moves &amp; id
 
Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
chapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructionschapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructions
 
Econ501a l11
Econ501a l11Econ501a l11
Econ501a l11
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
 
Cc 16
Cc 16Cc 16
Cc 16
 
arithmetic ins in 8051
arithmetic ins in 8051arithmetic ins in 8051
arithmetic ins in 8051
 

Similar to Prepostinfix

Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdf
pristiegee
 
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
zukun
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3
jateno3396
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 

Similar to Prepostinfix (18)

Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdf
 
Data structures
Data structures Data structures
Data structures
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Combinational circuit
Combinational circuitCombinational circuit
Combinational circuit
 
Stack
StackStack
Stack
 
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
ECCV2008: MAP Estimation Algorithms in Computer Vision - Part 1
 
C applications
C applicationsC applications
C applications
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
CNIT 141: 9. Elliptic Curve Cryptosystems
CNIT 141: 9. Elliptic Curve CryptosystemsCNIT 141: 9. Elliptic Curve Cryptosystems
CNIT 141: 9. Elliptic Curve Cryptosystems
 
data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3data structure and algorithm by bomboat_3
data structure and algorithm by bomboat_3
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Stack
StackStack
Stack
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Lecture6
Lecture6Lecture6
Lecture6
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 

Recently uploaded

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 

Prepostinfix

  • 1. Packet 15, page 1 INFIX, PREFIX, AND POSTFIX EXPRESSIONS Packet 15, page 2 KHumans usually write algebraic expressions like this: a + b KThis is called infix notation, because the operator (“+”) is inside the expression KA problem is that we need parentheses or precedence rules to handle more complicated expressions: a + b * c = (a + b) * c ? = a + (b * c) ?
  • 2. Packet 15, page 3 Infix, postfix, and prefix notation KThere is no reason we can’t place the operator somewhere else. Kinfix notation: a + b Kprefix notation: + a b Kpostfix notation: a b + Packet 15, page 4 Other names KPrefix notation was introduced by the Polish logician Lukasiewicz, and is sometimes called “Polish notation” KPostfix notation is sometimes called “reverse Polish notation” or RPN »Used on some calculators (the ones without '=' signs)
  • 3. Packet 15, page 5 KQuestion: Why would anyone ever want to use anything so “unnatural,” when infix seems to work just fine? KAnswer: With postfix and prefix notations, parentheses are no longer needed! infix postfix prefix (a + b) * c a b + c * * + a b c a + (b * c) a b c * + + a * b c Packet 15, page 6 Converting from infix notation to postfix notation KAssume that your infix expression is of the form <identifier> <operator> <identifier> KA postfix expression is created by rewriting this as <identifier> <identifier> <operator>
  • 4. Packet 15, page 7 Convert these infix expressions to postfix notation Kx Kx + y K(x + y) - z Kw * ((x + y) - z) K(2 * a) / ((a + b) * (a - c)) Packet 15, page 8 Convert these postfix expressions to infix notation K3 r - K1 3 r - + Ks t * 1 3 r - + + Kv w x y z * - + *
  • 5. Packet 15, page 9 A stack-based algorithm to evaluate a postfix expression for each character C in a given string, proceeding left to right { if C is an operand push C onto stack else // C is an operator { pop item from stack, and store in Opr2 pop item from stack, and store in Opr1 result = Opr1 C Opr2, using C as an operator push result onto stack } }