0% found this document useful (0 votes)
108 views72 pages

Data Structures and Algorithms V22.0102: Otávio Braga

The document describes the process for converting an infix notation mathematical expression to postfix notation. It involves using a stack and pushing and popping operators based on their precedence. Three examples are provided and shown step-by-step to demonstrate the algorithm.

Uploaded by

sesh2sesh4081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views72 pages

Data Structures and Algorithms V22.0102: Otávio Braga

The document describes the process for converting an infix notation mathematical expression to postfix notation. It involves using a stack and pushing and popping operators based on their precedence. Three examples are provided and shown step-by-step to demonstrate the algorithm.

Uploaded by

sesh2sesh4081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Data Structures and Algorithms

V22.0102
Otvio Braga
Infix to Postfix Conversion
We use a stack
When an operand is read, output it
When an operator is read
Pop until the top of the stack has an element of lower
precedence
Then push it
When ) is found, pop until we find the matching (
( has the lowest precedence when in the stack
but has the highest precedence when in the input
When we reach the end of input, pop until the stack is
empty
Infix to Postfix Conversion
Example 1
3+4*5/6
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack:
Output:
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack:
Output: 3
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: +
Output: 3
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: +
Output: 3 4
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: + *
Output: 3 4
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: + *
Output: 3 4 5
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: +
Output: 3 4 5 *
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: + /
Output: 3 4 5 *
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: + /
Output: 3 4 5 * 6
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack: +
Output: 3 4 5 * 6 /
Infix to Postfix Conversion
Example 1
3+4*5/6
Stack:
Output: 3 4 5 * 6 / +

Done!
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack:
Output:
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: (
Output:
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: (
Output: 300
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: ( +
Output: 300
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: ( +
Output: 300 23
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: (
Output: 300 23 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack:
Output: 300 23 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: *
Output: 300 23 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: * (
Output: 300 23 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: * (
Output: 300 23 + 43
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: * ( -
Output: 300 23 + 43
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: * ( -
Output: 300 23 + 43 21
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: * (
Output: 300 23 + 43 21 -
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: *
Output: 300 23 + 43 21 -
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack:
Output: 300 23 + 43 21 - *
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: /
Output: 300 23 + 43 21 - *
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: / (
Output: 300 23 + 43 21 - *
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: / (
Output: 300 23 + 43 21 - * 84
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: / ( +
Output: 300 23 + 43 21 - * 84
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: / ( +
Output: 300 23 + 43 21 - * 84 7
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: / (
Output: 300 23 + 43 21 - * 84 7 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack: /
Output: 300 23 + 43 21 - * 84 7 +
Infix to Postfix Conversion
Example 2
(300+23)*(43-21)/(84+7)
Stack:
Output: 300 23 + 43 21 - * 84 7 + /

Done!
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack:
Output:
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: (
Output:
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: (
Output: 4
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: ( +
Output: 4
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: ( +
Output: 4 8
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: (
Output: 4 8 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack:
Output: 4 8 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: *
Output: 4 8 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: * (
Output: 4 8 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: * (
Output: 4 8 + 6
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: * ( -
Output: 4 8 + 6
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: * ( -
Output: 4 8 + 6 5
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: * (
Output: 4 8 + 6 5 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: *
Output: 4 8 + 6 5 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack:
Output: 4 8 + 6 5 - *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: /
Output: 4 8 + 6 5 - *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / (
Output: 4 8 + 6 5 - *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( (
Output: 4 8 + 6 5 - *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( (
Output: 4 8 + 6 5 - * 3
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( ( -
Output: 4 8 + 6 5 - * 3
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( ( -
Output: 4 8 + 6 5 - * 3 2
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( (
Output: 4 8 + 6 5 - * 3 2 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / (
Output: 4 8 + 6 5 - * 3 2 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( *
Output: 4 8 + 6 5 - * 3 2 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( * (
Output: 4 8 + 6 5 - * 3 2 -
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( * (
Output: 4 8 + 6 5 - * 3 2 - 2
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( * ( +
Output: 4 8 + 6 5 - * 3 2 - 2
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( * ( +
Output: 4 8 + 6 5 - * 3 2 2 2
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( * (
Output: 4 8 + 6 5 - * 3 2 2 2 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / ( *
Output: 4 8 + 6 5 - * 3 2 2 2 +
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: / (
Output: 4 8 + 6 5 - * 3 2 2 2 + *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack: /
Output: 4 8 + 6 5 - * 3 2 2 2 + *
Infix to Postfix Conversion
Example 3
(4+8)*(6-5)/((3-2)*(2+2))
Stack:
Output: 4 8 + 6 5 - * 3 2 2 2 + * /

Done!

You might also like