In Fix to Post Fix Examples
Transcript of In Fix to Post Fix Examples
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 1/72
Data Structures and Algorithms
V22.0102
Otávio Braga
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 2/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 3/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 4/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack:
•
Output:
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 5/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack:
•
Output: 3
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 6/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: +
•
Output: 3
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 7/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: +
•
Output: 3 4
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 8/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: + *
•
Output: 3 4
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 9/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: + *
•
Output: 3 4 5
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 10/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: +
•
Output: 3 4 5 *
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 11/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: + /
•
Output: 3 4 5 *
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 12/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: + /
•
Output: 3 4 5 * 6
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 13/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack: +
•
Output: 3 4 5 * 6 /
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 14/72
Infix to Postfix Conversion
Example 1
• 3+4*5/6
• Stack:
•
Output: 3 4 5 * 6 / +
• Done!
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 15/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 16/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack:
•
Output:
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 17/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: (
•
Output:
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 18/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: (
•
Output: 300
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 19/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: ( +
•
Output: 300
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 20/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: ( +
•
Output: 300 23
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 21/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: (
•
Output: 300 23 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 22/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack:
•
Output: 300 23 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 23/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: *
•
Output: 300 23 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 24/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: * (
•
Output: 300 23 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 25/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: * (
•
Output: 300 23 + 43
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 26/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: * ( -
•
Output: 300 23 + 43
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 27/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: * ( -
•
Output: 300 23 + 43 21
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 28/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: * (
•
Output: 300 23 + 43 21 -
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 29/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: *
•
Output: 300 23 + 43 21 -
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 30/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack:
•
Output: 300 23 + 43 21 - *
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 31/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: /
•
Output: 300 23 + 43 21 - *
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 32/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: / (
• Output: 300 23 + 43 21 - *
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 33/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: / (
• Output: 300 23 + 43 21 - * 84
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 34/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: / ( +
• Output: 300 23 + 43 21 - * 84
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 35/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: / ( +
• Output: 300 23 + 43 21 - * 84 7
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 36/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: / (
• Output: 300 23 + 43 21 - * 84 7 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 37/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack: /
• Output: 300 23 + 43 21 - * 84 7 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 38/72
Infix to Postfix Conversion
Example 2
• (300+23)*(43-21)/(84+7)
• Stack:
• Output: 300 23 + 43 21 - * 84 7 + /
• Done!
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 39/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 40/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack:
• Output:
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 41/72
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 42/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: (
• Output: 4
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 43/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: ( +
• Output: 4
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 44/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: ( +
• Output: 4 8
f f
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 45/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: (
• Output: 4 8 +
f f
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 46/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack:
• Output: 4 8 +
fi fi i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 47/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: *
• Output: 4 8 +
I fi P fi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 48/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: * (
• Output: 4 8 +
I fi P fi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 49/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: * (
• Output: 4 8 + 6
I fi P fi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 50/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: * ( -
• Output: 4 8 + 6
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 51/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: * ( -
• Output: 4 8 + 6 5
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 52/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: * (
• Output: 4 8 + 6 5 -
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 53/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: *
• Output: 4 8 + 6 5 -
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 54/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack:
• Output: 4 8 + 6 5 - *
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 55/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: /
• Output: 4 8 + 6 5 - *
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 56/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: / (
• Output: 4 8 + 6 5 - *
I fi t P tfi C i
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 57/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: / ( (
• Output: 4 8 + 6 5 - *
Infix to Postfix Conversion
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 58/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 59/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 60/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 61/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 62/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 63/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 64/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 65/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 66/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 67/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 68/72
Infix to Postfix Conversion
Example 3
• (4+8)*(6-5)/((3-2)*(2+2))
• Stack: / ( * (
• Output: 4 8 + 6 5 - * 3 2 – 2 2 +
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 69/72
Infix to Postfix Conversion
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 70/72
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
7/31/2019 In Fix to Post Fix Examples
http://slidepdf.com/reader/full/in-fix-to-post-fix-examples 71/72
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