SlideShare a Scribd company logo
Problem Solving withalgorithm and Data Structure
Infix, Prefix and Postfix Expressions:
When you write an arithmetic expression such as B * C, the form of the expression provides you
with information so that you can interpret it correctly. In this case we know that the variable B
is being multiplied by the variable C since the multiplication operator * appears between them
in the expression. This type of notation is referred to as infix since the operator is in
between the two operands that it is working on.
Consider another infix example, A + B * C. The operators + and * still appear between the
operands, but there is a problem. Which operands do they work on? Does the + work on A and
B or does the * take B and C? The expression seems ambiguous.
In fact, you have been reading and writing these types of expressions for a long time and they
do not cause you any problem. The reason for this is that you know something about the
operators + and *. Each operator has a precedence level. Operators of higher precedence are
used before operators of lower precedence. The only thing that can change that order is the
presence of parentheses. The precedence order for arithmetic operators places multiplication
and division above addition and subtraction. If two operators of equal precedence appear, then
a left-to-right ordering or associatively is used.
Let’s interpret the troublesome expression A + B * C using operator precedence. B and C are
multiplied first, and A is then added to that result. (A + B) * C would force the addition of A and
B to be done first before the multiplication. In expression A + B + C, by precedence (via
associatively), the leftmost + would be done first.
Although all this may be obvious to you, remember that computers need to know exactly what
operators to perform and in what order. One way to write an expression that guarantees there
will be no confusion with respect to the order of operations is to create what is called a fully
parenthesized expression. This type of expression uses one pair of parentheses for each
operator. The parentheses dictate the order of operations; there is no ambiguity. There is also
no need to remember any precedence rules.
The expression A + B * C + D can be rewritten as ((A + (B * C)) + D) to show that the
multiplication happens first, followed by the leftmost addition. A + B + C + D can be written as
(((A + B) + C) + D) since the addition operations associate from left to right.
There are two other very important expression formats that may not seem obvious to you at
first. Consider the infix expression A + B. What would happen if we moved the operator before
the two operands? The resulting expression would be + A B. Likewise, we could move the
operator to the end. We would get A B +. These look a bit strange.
These changes to the position of the operator with respect to the operands create two new
expression formats, prefix and postfix. Prefix expression notation requires that all operators
precede the two operands that they work on. Postfix, on the other hand, requires that its
operators come after the corresponding operands. A few more examples should help to make
this a bit clearer (see Table 2).
A + B * C would be written as + A * B C in prefix. The multiplication operator comes
immediately before the operands B and C, denoting that * has precedence over +. The addition
operator then appears before the A and the result of the multiplication.
In postfix, the expression would be A B C * +. Again, the order of operations is preserved since
the * appears immediately after the B and the C, denoting that * has precedence, with +
coming after. Although the operators moved and now appear either before or after their
respective operands, the order of the operands stayed exactly the same relative to one
another.
Table 2: Examples of Infix, Prefix, and Postfix
Infix Expression Prefix Expression Postfix Expression
A + B + A B A B +
A + B * C + A * B C A B C * +
Now consider the infix expression (A + B) * C. Recall that in this case, infix requires the parentheses
to force the performance of the addition before the multiplication. However, when A + B was written
in prefix, the addition operator was simply moved before the operands, + A B. The result of this
operation becomes the first operand for the multiplication. The multiplication operator is moved in
front of the entire expression, giving us * + A B C. Likewise, in postfix A B + forces the addition to
happen first. The multiplication can be done to that result and the remaining operand C. The proper
postfix expression is then A B + C *.
Consider these three expressions again (see Table 3). Something very important has happened.
Where did the parentheses go? Why don’t we need them in prefix and postfix? The answer is that
the operators are no longer ambiguous with respect to the operands that they work on. Only infix
notation requires the additional symbols. The order of operations within prefix and postfix
expressions is completely determined by the position of the operator and nothing else. In many
ways, this makes infix the least desirable notation to use.
Table 3: An Expression with Parentheses
Infix Expression Prefix Expression Postfix Expression
Table 3: An Expression with Parentheses
Infix Expression Prefix Expression Postfix Expression
(A + B) * C * + A B C A B + C *
Table 4 shows some additional examples of infix expressions and the equivalent prefix and postfix
expressions. Be sure that you understand how they are equivalent in terms of the order of the
operations being performed.
Table 4: Additional Examples of Infix, Prefix, and Postfix
Infix Expression Prefix Expression Postfix Expression
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 * +
A + B + C + D + + + A B C D A B + C + D +
Conversion of InfixExpressionsto Prefixand Postfix
So far, we have used ad hoc methods to convert between infix expressions and the equivalent
prefix and postfix expression notations. As you might expect, there are algorithmic ways to
perform the conversion that allow any expression of any complexity to be correctly
transformed.
The first technique that we will consider uses the notion of a fully parenthesized expression
that was discussed earlier. Recall that A + B * C can be written as (A + (B * C)) to show explicitly
that the multiplication has precedence over the addition. On closer observation, however, you
can see that each parenthesis pair also denotes the beginning and the end of an operand pair
with the corresponding operator in the middle.
Look at the right parenthesis in the sub expression (B * C) above. If we were to move the
multiplication symbol to that position and remove the matching left parenthesis, giving us B C
*, we would in effect have converted the sub expression to postfix notation. If the addition
operator were also moved to its corresponding right parenthesis position and the matching left
parenthesis were removed, the complete postfix expression would result (see Figure 6).
Figure 6: Moving Operators to the Right for Postfix Notation
If we do the same thing but instead of moving the symbol to the position of the right
parenthesis, we move it to the left, we get prefix notation (see Figure 7). The position of the
parenthesis pair is actually a clue to the final position of the enclosed operator.
Figure 7: Moving Operators to the Left for Prefix Notation
So in order to convert an expression, no matter how complex, to either prefix or postfix
notation, fully parenthesize the expression using the order of operations. Then move the
enclosed operator to the position of either the left or the right parenthesis depending on
whether you want prefix or postfix notation.
Here is a more complex expression: (A + B) * C - (D - E) * (F + G). Figure 8 shows the conversion
to postfix and prefix notations.
Figure 8: Converting a Complex Expression to Prefix and Postfix Notations

More Related Content

PDF
Assignment8
PDF
Assignment12
PDF
Assignment6
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
PDF
Assignment10
PPTX
C –imp points
DOCX
Mycasestudy
PDF
Assignment7
Assignment8
Assignment12
Assignment6
[ITP - Lecture 04] Variables and Constants in C/C++
Assignment10
C –imp points
Mycasestudy
Assignment7

What's hot (20)

PDF
[ITP - Lecture 05] Datatypes
PPT
Cd2 [autosaved]
PDF
Data types in python part 2
DOC
Compiler Design QA
PPTX
Unit 2 application of stack
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPT
Getting Started with C++
PPTX
Strings
PDF
07 top-down-parsing
PPTX
Introduction to c programming
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPTX
Top Down Parsing, Predictive Parsing
PDF
C programming part4
PPT
Csharp4 operators and_casts
PDF
Type Conversion in C++ and C# Arithmetic Expressions
PPT
Lập trình C
PDF
[ITP - Lecture 15] Arrays & its Types
PPTX
Top down parsing
PPTX
Variable declaration
[ITP - Lecture 05] Datatypes
Cd2 [autosaved]
Data types in python part 2
Compiler Design QA
Unit 2 application of stack
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Getting Started with C++
Strings
07 top-down-parsing
Introduction to c programming
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Top Down Parsing, Predictive Parsing
C programming part4
Csharp4 operators and_casts
Type Conversion in C++ and C# Arithmetic Expressions
Lập trình C
[ITP - Lecture 15] Arrays & its Types
Top down parsing
Variable declaration
Ad

Viewers also liked (16)

PPT
Data structures & problem solving unit 1 ppt
PPT
All-Reduce and Prefix-Sum Operations
PPTX
Amistad & amor
PPT
тема 2.7.внимание
PPT
тема 1.2. методы психологии
PDF
Trabajo de Investigacion
PDF
fisheYe Snapshot
PPT
тема 2.9. эмоции
PDF
Professional Liability Insurance application for Adult day care
PPT
тема 4.5. характер
PDF
DEEPIKA VERMA (RESUME) (1)
PDF
Problem Solving with Algorithms and Data Structures
PPTX
Top 5 algorithms used in Data Science
PDF
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later
PDF
Problem Solving with Algorithms and Data Structure - Lists
PPT
Database structure Structures Link list and trees and Recurison complete
Data structures & problem solving unit 1 ppt
All-Reduce and Prefix-Sum Operations
Amistad & amor
тема 2.7.внимание
тема 1.2. методы психологии
Trabajo de Investigacion
fisheYe Snapshot
тема 2.9. эмоции
Professional Liability Insurance application for Adult day care
тема 4.5. характер
DEEPIKA VERMA (RESUME) (1)
Problem Solving with Algorithms and Data Structures
Top 5 algorithms used in Data Science
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later
Problem Solving with Algorithms and Data Structure - Lists
Database structure Structures Link list and trees and Recurison complete
Ad

Similar to Problem solving with algorithm and data structure (20)

DOCX
Conversion of in fix pre fix,infix by sarmad baloch
PPTX
Polish Notation In Data Structure
PDF
Applications of Stack (Data Structure).pdf
PPTX
Prefix and PostFIx presentation for DSA .pptx
PPT
computer notes - Data Structures - 6
PPTX
Data structures (Infix, Prefix and Postfix notations).pptx
PPT
Lecture6
PDF
Associativity of operators
PDF
computer notes - Conversion from infix to postfix
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PPTX
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
PPTX
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
PPTX
Data structures
DOC
C programming operators
PDF
C++ revision tour
PDF
Oop with c++ notes unit 01 introduction
PDF
Data Structures And Algorithms(stacks queues)
PPTX
Lec 06 Applications of Stacks.pptx Applications of Stack
PPTX
Lec5-Stack-bukc-28022024-112316am (1) .pptx
DOCX
Functions in c++
Conversion of in fix pre fix,infix by sarmad baloch
Polish Notation In Data Structure
Applications of Stack (Data Structure).pdf
Prefix and PostFIx presentation for DSA .pptx
computer notes - Data Structures - 6
Data structures (Infix, Prefix and Postfix notations).pptx
Lecture6
Associativity of operators
computer notes - Conversion from infix to postfix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
STACK APPLICATIONS: INFOX TO POSTFIX CONVERSION AND EVALUATION OF POSTFIX EXP...
Data structures
C programming operators
C++ revision tour
Oop with c++ notes unit 01 introduction
Data Structures And Algorithms(stacks queues)
Lec 06 Applications of Stacks.pptx Applications of Stack
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Functions in c++

Recently uploaded (20)

PPT
Quality review (1)_presentation of this 21
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PDF
Introduction to Business Data Analytics.
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Mega Projects Data Mega Projects Data
PDF
Foundation of Data Science unit number two notes
Quality review (1)_presentation of this 21
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Supervised vs unsupervised machine learning algorithms
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Introduction to Business Data Analytics.
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Business Acumen Training GuidePresentation.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
IB Computer Science - Internal Assessment.pptx
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Mega Projects Data Mega Projects Data
Foundation of Data Science unit number two notes

Problem solving with algorithm and data structure

  • 1. Problem Solving withalgorithm and Data Structure Infix, Prefix and Postfix Expressions: When you write an arithmetic expression such as B * C, the form of the expression provides you with information so that you can interpret it correctly. In this case we know that the variable B is being multiplied by the variable C since the multiplication operator * appears between them in the expression. This type of notation is referred to as infix since the operator is in between the two operands that it is working on. Consider another infix example, A + B * C. The operators + and * still appear between the operands, but there is a problem. Which operands do they work on? Does the + work on A and B or does the * take B and C? The expression seems ambiguous. In fact, you have been reading and writing these types of expressions for a long time and they do not cause you any problem. The reason for this is that you know something about the operators + and *. Each operator has a precedence level. Operators of higher precedence are used before operators of lower precedence. The only thing that can change that order is the presence of parentheses. The precedence order for arithmetic operators places multiplication and division above addition and subtraction. If two operators of equal precedence appear, then a left-to-right ordering or associatively is used. Let’s interpret the troublesome expression A + B * C using operator precedence. B and C are multiplied first, and A is then added to that result. (A + B) * C would force the addition of A and B to be done first before the multiplication. In expression A + B + C, by precedence (via associatively), the leftmost + would be done first. Although all this may be obvious to you, remember that computers need to know exactly what operators to perform and in what order. One way to write an expression that guarantees there will be no confusion with respect to the order of operations is to create what is called a fully parenthesized expression. This type of expression uses one pair of parentheses for each operator. The parentheses dictate the order of operations; there is no ambiguity. There is also no need to remember any precedence rules. The expression A + B * C + D can be rewritten as ((A + (B * C)) + D) to show that the multiplication happens first, followed by the leftmost addition. A + B + C + D can be written as (((A + B) + C) + D) since the addition operations associate from left to right. There are two other very important expression formats that may not seem obvious to you at first. Consider the infix expression A + B. What would happen if we moved the operator before the two operands? The resulting expression would be + A B. Likewise, we could move the operator to the end. We would get A B +. These look a bit strange. These changes to the position of the operator with respect to the operands create two new expression formats, prefix and postfix. Prefix expression notation requires that all operators precede the two operands that they work on. Postfix, on the other hand, requires that its
  • 2. operators come after the corresponding operands. A few more examples should help to make this a bit clearer (see Table 2). A + B * C would be written as + A * B C in prefix. The multiplication operator comes immediately before the operands B and C, denoting that * has precedence over +. The addition operator then appears before the A and the result of the multiplication. In postfix, the expression would be A B C * +. Again, the order of operations is preserved since the * appears immediately after the B and the C, denoting that * has precedence, with + coming after. Although the operators moved and now appear either before or after their respective operands, the order of the operands stayed exactly the same relative to one another. Table 2: Examples of Infix, Prefix, and Postfix Infix Expression Prefix Expression Postfix Expression A + B + A B A B + A + B * C + A * B C A B C * + Now consider the infix expression (A + B) * C. Recall that in this case, infix requires the parentheses to force the performance of the addition before the multiplication. However, when A + B was written in prefix, the addition operator was simply moved before the operands, + A B. The result of this operation becomes the first operand for the multiplication. The multiplication operator is moved in front of the entire expression, giving us * + A B C. Likewise, in postfix A B + forces the addition to happen first. The multiplication can be done to that result and the remaining operand C. The proper postfix expression is then A B + C *. Consider these three expressions again (see Table 3). Something very important has happened. Where did the parentheses go? Why don’t we need them in prefix and postfix? The answer is that the operators are no longer ambiguous with respect to the operands that they work on. Only infix notation requires the additional symbols. The order of operations within prefix and postfix expressions is completely determined by the position of the operator and nothing else. In many ways, this makes infix the least desirable notation to use. Table 3: An Expression with Parentheses Infix Expression Prefix Expression Postfix Expression
  • 3. Table 3: An Expression with Parentheses Infix Expression Prefix Expression Postfix Expression (A + B) * C * + A B C A B + C * Table 4 shows some additional examples of infix expressions and the equivalent prefix and postfix expressions. Be sure that you understand how they are equivalent in terms of the order of the operations being performed. Table 4: Additional Examples of Infix, Prefix, and Postfix Infix Expression Prefix Expression Postfix Expression 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 * + A + B + C + D + + + A B C D A B + C + D + Conversion of InfixExpressionsto Prefixand Postfix So far, we have used ad hoc methods to convert between infix expressions and the equivalent prefix and postfix expression notations. As you might expect, there are algorithmic ways to perform the conversion that allow any expression of any complexity to be correctly transformed. The first technique that we will consider uses the notion of a fully parenthesized expression that was discussed earlier. Recall that A + B * C can be written as (A + (B * C)) to show explicitly that the multiplication has precedence over the addition. On closer observation, however, you can see that each parenthesis pair also denotes the beginning and the end of an operand pair with the corresponding operator in the middle. Look at the right parenthesis in the sub expression (B * C) above. If we were to move the multiplication symbol to that position and remove the matching left parenthesis, giving us B C
  • 4. *, we would in effect have converted the sub expression to postfix notation. If the addition operator were also moved to its corresponding right parenthesis position and the matching left parenthesis were removed, the complete postfix expression would result (see Figure 6). Figure 6: Moving Operators to the Right for Postfix Notation If we do the same thing but instead of moving the symbol to the position of the right parenthesis, we move it to the left, we get prefix notation (see Figure 7). The position of the parenthesis pair is actually a clue to the final position of the enclosed operator. Figure 7: Moving Operators to the Left for Prefix Notation So in order to convert an expression, no matter how complex, to either prefix or postfix notation, fully parenthesize the expression using the order of operations. Then move the enclosed operator to the position of either the left or the right parenthesis depending on whether you want prefix or postfix notation. Here is a more complex expression: (A + B) * C - (D - E) * (F + G). Figure 8 shows the conversion to postfix and prefix notations. Figure 8: Converting a Complex Expression to Prefix and Postfix Notations