Lecture 17 Stack Polish Notations II
Lecture 17 Stack Polish Notations II
Lecture 17 Stack Polish Notations II
(((A+B)*(C-E))/(F+G))
▪ stack: <empty>
▪ output: []
Infix to Postfix
( (A+ B ) * ( C- E ) ) /( F + G) )
▪ stack: (
▪ output: []
Infix to Postfix
(A+B)*(C-E))/(F+G))
▪ stack: ( (
▪ output: []
Infix to Postfix
A+B)*(C-E))/(F+G))
▪ stack: ( ( (
▪ output: []
Infix to Postfix
+B)*(C-E))/(F+G))
▪ stack: ( ( (
▪ output: [A]
Infix to Postfix
B)*(C-E))/(F+G))
stack: ( ( ( +
output: [A]
Infix to Postfix
)*(C-E))/(F+G))
stack: ( ( ( +
output: [A B]
Infix to Postfix
*(C-E))/(F+G))
▪ stack: ( (
▪ output: [A B + ]
Infix to Postfix
(C-E))/(F+G))
stack: ( ( *
output: [A B + ]
Infix to Postfix
C-E))/(F+G))
stack: ( ( * (
output: [A B + ]
Infix to Postfix
-E))/(F+G))
stack: ( ( * (
output: [A B + C ]
Infix to Postfix
E))/(F+G))
stack: ( ( * ( -
output: [A B + C ]
Infix to Postfix
))/(F+G))
stack: ( ( * ( -
output: [A B + C E ]
Infix to Postfix
)/(F+G))
stack: ( ( *
output: [A B + C E - ]
Infix to Postfix
/(F+G))
stack: (
output: [A B + C E - * ]
Infix to Postfix
(F+G))
▪ stack: ( /
▪ output: [A B + C E - * ]
Infix to Postfix
F+G))
stack: ( / (
output: [A B + C E - * ]
Infix to Postfix
+G))
stack: ( / (
output: [A B + C E - * F ]
Infix to Postfix
G))
stack: ( / ( +
output: [A B + C E - * F ]
Infix to Postfix
))
▪ stack: ( / ( +
▪ output: [A B + C E - * F G ]
Infix to Postfix
)
▪ stack: ( /
▪ output: [A B + C E - * F G + ]
Infix to Postfix
stack: <empty>
output: [A B + C E - * F G + / ]
❖ Infix to Postfix Example
❖ Infix String: ( a + b ) * ( c + d )
Rules
▪ The following is a trace of the postfix evaluation algorithm for the postfix
expression:
Evaluating postfix expressions (Using Open Stack)
Evaluating postfix expressions (Using Open Stack)
Infix to Prefix Conversion
Polish Notations
▪ The way to write arithmetic expression is known as polish notation
▪ There are basically three types of polish notation:
√ Infix: When the operator is written between two operands then it
is known as Infix notation.
❖ For example, A+B
√ Postfix: When the operator is written after their operands then it
is known as Postfix notation.
❖ For example, AB+
√ Prefix: When the operator is written before their operands then it
is known as Prefix notation.
❖ For example, +AB
Infix to prefix conversion
▪ Expression = (A+B) * C – D + F