SlideShare a Scribd company logo
1 of 3
Infix, Postfix and Prefix
Infix, Postfix and Prefix notations are three different but equivalent ways of writing
expressions. It is easiest to demonstrate the differences by looking at examples of
operators that take two operands.
Infix notation: X + Y
Operators are written in-between their operands. This is the usual way we write
expressions. An expression such as A * ( B + C ) / D is usually taken to mean
something like: "First add B and C together, then multiply the result by A, then
divide by D to give the final answer."
Infix notation needs extra information to make the order of evaluation of the
operators clear: rules built into the language about operator precedence and
associativity, and brackets ( ) to allow users to override these rules. For
example, the usual rules for associativity say that we perform operations from
left to right, so the multiplication by A is assumed to come before the division
by D. Similarly, the usual rules for precedence say that we perform
multiplication and division before we perform addition and subtraction.
(see CS2121 lecture).
Postfix notation (also known as "Reverse Polish notation"): X Y +
Operators are written after their operands. The infix expression given above is
equivalent to A B C + * D /
The order of evaluation of operators is always left-to-right, and brackets cannot
be used to change this order. Because the "+" is to the left of the "*" in the
example above, the addition must be performed before the multiplication.
Operators act on values immediately to the left of them. For example, the "+"
above uses the "B" and "C". We can add (totally unnecessary) brackets to make
this explicit:
( (A (B C +) *) D /)
Thus, the "*" uses the two values immediately preceding: "A", and the result of
the addition. Similarly, the "/" uses the result of the multiplication and the "D".
Prefix notation (also known as "Polish notation"): + X Y
Operators are written before their operands. The expressions given above are
equivalent to / * A + B C D
As for Postfix, operators are evaluated left-to-right and brackets are
superfluous. Operators act on the two nearest values on the right. I have again
added (totally unnecessary) brackets to make this clear:
(/ (* A (+ B C) ) D)
Although Prefix "operators are evaluated left-to-right", they use values to their
right, and if these values themselves involve computations then this changes the
order that the operators have to be evaluated in. In the example above, although
the division is the first operator on the left, it acts on the result of the
multiplication, and so the multiplication has to happen before the division (and
similarly the addition has to happen before the multiplication).
Because Postfix operators use values to their left, any values involving
computations will already have been calculated as we go left-to-right, and so
the order of evaluation of the operators is not disrupted in the same way as in
Prefix expressions.
In all three versions, the operands occur in the same order, and just the operators have
to be moved to keep the meaning correct. (This is particularly important for
asymmetric operators like subtraction and division: A - Bdoes not mean the same
as B - A; the former is equivalent to A B - or - A B, the latter to B A - or - B A).
Examples:
Infix Postfix Prefix Notes
A * B + C / D A B * C D / + + * A B / C D
multiply A and B,
divide C by D,
add the results
A * (B + C) / D A B C + * D / / * A + B C D
add B and C,
multiply by A,
divide by D
A * (B + C / D) A B C D / + * * A + B / C D
divide C by D,
add B,
multiply by A
Converting between these notations
The most straightforward method is to start by inserting all the implicit brackets that
show the order of evaluation e.g.:
Infix Postfix Prefix
( (A * B) + (C / D) ) ( (A B *) (C D /) +) (+ (* A B) (/ C D) )
((A * (B + C) ) / D) ( (A (B C +) *) D /) (/ (* A (+ B C) ) D)
(A * (B + (C / D) ) ) (A (B (C D /) +) *) (* A (+ B (/ C D) ) )
You can convert directly between these bracketed forms simply by moving the
operator within the brackets e.g. (X + Y) or (X Y +) or (+ X Y). Repeat this for all the
operators in an expression, and finally remove any superfluous brackets.
You can use a similar trick to convert to and from parse trees - each bracketed triplet
of an operator and its two operands (or sub-expressions) corresponds to a node of the
tree. The corresponding parse trees are:
/ *
+ /  / 
/  * D A +
/  /  / 
* / A + B /
/  /  /  / 
A B C D B C C D
((A*B)+(C/D)) ((A*(B+C))/D) (A*(B+(C/D)))
Computer Languages
Because Infix is so common in mathematics, it is much easier to read, and so is used
in most computer languages (e.g. a simple Infix calculator). However, Prefix is often
used for operators that take a single operand (e.g. negation) and function calls.
Although Postfix and Prefix notations have similar complexity, Postfix is slightly
easier to evaluate in simple circumstances, such as in some calculators (e.g. a simple
Postfix calculator), as the operators really are evaluated strictly left-to-right
(see note above).
For lots more information about notations for expressions, see my CS2111 lectures.

More Related Content

What's hot

Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Deepak Singh
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Bsc math previous exam quetions
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetionsmshoaib15
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...vtunotesbysree
 
Unit 2 c programming_basics
Unit 2 c programming_basicsUnit 2 c programming_basics
Unit 2 c programming_basicskirthika jeyenth
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expressionshubham_jangid
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingSabik T S
 
Precedence and associativity (Computer programming and utilization)
Precedence and associativity (Computer programming and utilization)Precedence and associativity (Computer programming and utilization)
Precedence and associativity (Computer programming and utilization)Digvijaysinh Gohil
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressionsvishaljot_kaur
 

What's hot (20)

CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
C –imp points
C –imp pointsC –imp points
C –imp points
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Bsc math previous exam quetions
Bsc math previous exam quetionsBsc math previous exam quetions
Bsc math previous exam quetions
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
Unit 2 c programming_basics
Unit 2 c programming_basicsUnit 2 c programming_basics
Unit 2 c programming_basics
 
Operators and Expression
Operators and ExpressionOperators and Expression
Operators and Expression
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
C ppt
C pptC ppt
C ppt
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Precedence and associativity (Computer programming and utilization)
Precedence and associativity (Computer programming and utilization)Precedence and associativity (Computer programming and utilization)
Precedence and associativity (Computer programming and utilization)
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
 

Similar to Conversion of in fix pre fix,infix by sarmad baloch

Problem solving with algorithm and data structure
Problem solving with algorithm and data structureProblem solving with algorithm and data structure
Problem solving with algorithm and data structureRabia Tariq
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Saikrishna Tanguturu
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data StructureMeghaj Mallick
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Associativity of operators
Associativity of operatorsAssociativity of operators
Associativity of operatorsAjay Chimmani
 
Programming presentation
Programming presentationProgramming presentation
Programming presentationFiaz Khokhar
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1vikram mahendra
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of PrecedenceMuhammad Hammad Waseem
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdfMaryJacob24
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptxJAYAPRIYAR7
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++bajiajugal
 

Similar to Conversion of in fix pre fix,infix by sarmad baloch (20)

Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Problem solving with algorithm and data structure
Problem solving with algorithm and data structureProblem solving with algorithm and data structure
Problem solving with algorithm and data structure
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Associativity of operators
Associativity of operatorsAssociativity of operators
Associativity of operators
 
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
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
C language basics
C language basicsC language basics
C language basics
 
C program
C programC program
C program
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
C-PPT.pdf
C-PPT.pdfC-PPT.pdf
C-PPT.pdf
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
What is c
What is cWhat is c
What is c
 

More from Sarmad Baloch

Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...
Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...
Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...Sarmad Baloch
 
Positive matrix by sarmad baloch
Positive matrix by sarmad balochPositive matrix by sarmad baloch
Positive matrix by sarmad balochSarmad Baloch
 
Bnak reconciliation statement by sarmad baloch
Bnak reconciliation statement by sarmad balochBnak reconciliation statement by sarmad baloch
Bnak reconciliation statement by sarmad balochSarmad Baloch
 
Introduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochIntroduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochSarmad Baloch
 
Security_saftety_privacy of computer by sarmad baloch
Security_saftety_privacy of computer by sarmad balochSecurity_saftety_privacy of computer by sarmad baloch
Security_saftety_privacy of computer by sarmad balochSarmad Baloch
 
E commerce (introduction to computer) by sarmad baloch
E commerce (introduction to computer) by sarmad balochE commerce (introduction to computer) by sarmad baloch
E commerce (introduction to computer) by sarmad balochSarmad Baloch
 
Programming languages and programme development of computer by sarmad baloch
Programming languages and programme development of computer by sarmad balochProgramming languages and programme development of computer by sarmad baloch
Programming languages and programme development of computer by sarmad balochSarmad Baloch
 
Introduction to computer & its applications by sarmad baloch
Introduction to computer & its applications by sarmad balochIntroduction to computer & its applications by sarmad baloch
Introduction to computer & its applications by sarmad balochSarmad Baloch
 
Accounting principle & concept by sarmad baloch
Accounting principle & concept by sarmad balochAccounting principle & concept by sarmad baloch
Accounting principle & concept by sarmad balochSarmad Baloch
 
Implement of c & its coding programming by sarmad baloch
Implement of c & its coding  programming by sarmad balochImplement of c & its coding  programming by sarmad baloch
Implement of c & its coding programming by sarmad balochSarmad Baloch
 
The functions of modal auxiliary verbs by sarmad baloch
The functions of modal auxiliary verbs by sarmad balochThe functions of modal auxiliary verbs by sarmad baloch
The functions of modal auxiliary verbs by sarmad balochSarmad Baloch
 
Integrator & diferentiator amplifier presentation by sarmad baloch
Integrator & diferentiator amplifier presentation by sarmad balochIntegrator & diferentiator amplifier presentation by sarmad baloch
Integrator & diferentiator amplifier presentation by sarmad balochSarmad Baloch
 
Ms powerpoint 2007 presentation by sarmad baloch
Ms powerpoint 2007 presentation by sarmad balochMs powerpoint 2007 presentation by sarmad baloch
Ms powerpoint 2007 presentation by sarmad balochSarmad Baloch
 
Semiconductor materials and pn junction by sarmad baloch
Semiconductor materials and pn junction by sarmad balochSemiconductor materials and pn junction by sarmad baloch
Semiconductor materials and pn junction by sarmad balochSarmad Baloch
 
Introduction of BJT,types of Diodes by sarmad baloch
Introduction of BJT,types of Diodes by sarmad balochIntroduction of BJT,types of Diodes by sarmad baloch
Introduction of BJT,types of Diodes by sarmad balochSarmad Baloch
 
Pn junction diode by sarmad baloch
Pn junction diode by sarmad balochPn junction diode by sarmad baloch
Pn junction diode by sarmad balochSarmad Baloch
 
Difference b/w BRITISH vs AMERICAN Language by sarmad khosa
Difference b/w BRITISH vs AMERICAN Language by sarmad khosaDifference b/w BRITISH vs AMERICAN Language by sarmad khosa
Difference b/w BRITISH vs AMERICAN Language by sarmad khosaSarmad Baloch
 
ACTIVE & PASSIVE ELECTRONICS by sarmad khosa
ACTIVE & PASSIVE ELECTRONICS by sarmad khosaACTIVE & PASSIVE ELECTRONICS by sarmad khosa
ACTIVE & PASSIVE ELECTRONICS by sarmad khosaSarmad Baloch
 
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosa
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosaMICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosa
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosaSarmad Baloch
 
Types of DIODES Basic electronics by sarmad khosa
Types of DIODES Basic electronics by sarmad khosaTypes of DIODES Basic electronics by sarmad khosa
Types of DIODES Basic electronics by sarmad khosaSarmad Baloch
 

More from Sarmad Baloch (20)

Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...
Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...
Pakistan International cricket stadiums in ADVANCE DATABASE Managment System ...
 
Positive matrix by sarmad baloch
Positive matrix by sarmad balochPositive matrix by sarmad baloch
Positive matrix by sarmad baloch
 
Bnak reconciliation statement by sarmad baloch
Bnak reconciliation statement by sarmad balochBnak reconciliation statement by sarmad baloch
Bnak reconciliation statement by sarmad baloch
 
Introduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochIntroduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad baloch
 
Security_saftety_privacy of computer by sarmad baloch
Security_saftety_privacy of computer by sarmad balochSecurity_saftety_privacy of computer by sarmad baloch
Security_saftety_privacy of computer by sarmad baloch
 
E commerce (introduction to computer) by sarmad baloch
E commerce (introduction to computer) by sarmad balochE commerce (introduction to computer) by sarmad baloch
E commerce (introduction to computer) by sarmad baloch
 
Programming languages and programme development of computer by sarmad baloch
Programming languages and programme development of computer by sarmad balochProgramming languages and programme development of computer by sarmad baloch
Programming languages and programme development of computer by sarmad baloch
 
Introduction to computer & its applications by sarmad baloch
Introduction to computer & its applications by sarmad balochIntroduction to computer & its applications by sarmad baloch
Introduction to computer & its applications by sarmad baloch
 
Accounting principle & concept by sarmad baloch
Accounting principle & concept by sarmad balochAccounting principle & concept by sarmad baloch
Accounting principle & concept by sarmad baloch
 
Implement of c & its coding programming by sarmad baloch
Implement of c & its coding  programming by sarmad balochImplement of c & its coding  programming by sarmad baloch
Implement of c & its coding programming by sarmad baloch
 
The functions of modal auxiliary verbs by sarmad baloch
The functions of modal auxiliary verbs by sarmad balochThe functions of modal auxiliary verbs by sarmad baloch
The functions of modal auxiliary verbs by sarmad baloch
 
Integrator & diferentiator amplifier presentation by sarmad baloch
Integrator & diferentiator amplifier presentation by sarmad balochIntegrator & diferentiator amplifier presentation by sarmad baloch
Integrator & diferentiator amplifier presentation by sarmad baloch
 
Ms powerpoint 2007 presentation by sarmad baloch
Ms powerpoint 2007 presentation by sarmad balochMs powerpoint 2007 presentation by sarmad baloch
Ms powerpoint 2007 presentation by sarmad baloch
 
Semiconductor materials and pn junction by sarmad baloch
Semiconductor materials and pn junction by sarmad balochSemiconductor materials and pn junction by sarmad baloch
Semiconductor materials and pn junction by sarmad baloch
 
Introduction of BJT,types of Diodes by sarmad baloch
Introduction of BJT,types of Diodes by sarmad balochIntroduction of BJT,types of Diodes by sarmad baloch
Introduction of BJT,types of Diodes by sarmad baloch
 
Pn junction diode by sarmad baloch
Pn junction diode by sarmad balochPn junction diode by sarmad baloch
Pn junction diode by sarmad baloch
 
Difference b/w BRITISH vs AMERICAN Language by sarmad khosa
Difference b/w BRITISH vs AMERICAN Language by sarmad khosaDifference b/w BRITISH vs AMERICAN Language by sarmad khosa
Difference b/w BRITISH vs AMERICAN Language by sarmad khosa
 
ACTIVE & PASSIVE ELECTRONICS by sarmad khosa
ACTIVE & PASSIVE ELECTRONICS by sarmad khosaACTIVE & PASSIVE ELECTRONICS by sarmad khosa
ACTIVE & PASSIVE ELECTRONICS by sarmad khosa
 
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosa
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosaMICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosa
MICROSOFT WORD 2007 FULL PRESENTATION BY sarmad khosa
 
Types of DIODES Basic electronics by sarmad khosa
Types of DIODES Basic electronics by sarmad khosaTypes of DIODES Basic electronics by sarmad khosa
Types of DIODES Basic electronics by sarmad khosa
 

Recently uploaded

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Conversion of in fix pre fix,infix by sarmad baloch

  • 1. Infix, Postfix and Prefix Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands. Infix notation: X + Y Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer." Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction. (see CS2121 lecture). Postfix notation (also known as "Reverse Polish notation"): X Y + Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit: ( (A (B C +) *) D /) Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D". Prefix notation (also known as "Polish notation"): + X Y
  • 2. Operators are written before their operands. The expressions given above are equivalent to / * A + B C D As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear: (/ (* A (+ B C) ) D) Although Prefix "operators are evaluated left-to-right", they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication). Because Postfix operators use values to their left, any values involving computations will already have been calculated as we go left-to-right, and so the order of evaluation of the operators is not disrupted in the same way as in Prefix expressions. In all three versions, the operands occur in the same order, and just the operators have to be moved to keep the meaning correct. (This is particularly important for asymmetric operators like subtraction and division: A - Bdoes not mean the same as B - A; the former is equivalent to A B - or - A B, the latter to B A - or - B A). Examples: Infix Postfix Prefix Notes A * B + C / D A B * C D / + + * A B / C D multiply A and B, divide C by D, add the results A * (B + C) / D A B C + * D / / * A + B C D add B and C, multiply by A, divide by D A * (B + C / D) A B C D / + * * A + B / C D divide C by D, add B, multiply by A Converting between these notations The most straightforward method is to start by inserting all the implicit brackets that show the order of evaluation e.g.:
  • 3. Infix Postfix Prefix ( (A * B) + (C / D) ) ( (A B *) (C D /) +) (+ (* A B) (/ C D) ) ((A * (B + C) ) / D) ( (A (B C +) *) D /) (/ (* A (+ B C) ) D) (A * (B + (C / D) ) ) (A (B (C D /) +) *) (* A (+ B (/ C D) ) ) You can convert directly between these bracketed forms simply by moving the operator within the brackets e.g. (X + Y) or (X Y +) or (+ X Y). Repeat this for all the operators in an expression, and finally remove any superfluous brackets. You can use a similar trick to convert to and from parse trees - each bracketed triplet of an operator and its two operands (or sub-expressions) corresponds to a node of the tree. The corresponding parse trees are: / * + / / / * D A + / / / * / A + B / / / / / A B C D B C C D ((A*B)+(C/D)) ((A*(B+C))/D) (A*(B+(C/D))) Computer Languages Because Infix is so common in mathematics, it is much easier to read, and so is used in most computer languages (e.g. a simple Infix calculator). However, Prefix is often used for operators that take a single operand (e.g. negation) and function calls. Although Postfix and Prefix notations have similar complexity, Postfix is slightly easier to evaluate in simple circumstances, such as in some calculators (e.g. a simple Postfix calculator), as the operators really are evaluated strictly left-to-right (see note above). For lots more information about notations for expressions, see my CS2111 lectures.