0% found this document useful (0 votes)
18 views18 pages

CS212 Sep2016 07 InPostPreExpressions

The document discusses infix, prefix, and postfix expressions and how to evaluate them using a stack. It provides examples of converting between infix and prefix/postfix notations. It then describes how to evaluate a postfix expression step-by-step using a stack, including pushing operands and calculating and pushing results when operators are encountered. The same process can be used for prefix expressions, but scanning from right to left. Evaluating expressions in this way allows using a stack to efficiently evaluate expressions in postfix or prefix form.

Uploaded by

Dahlia Gamal
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)
18 views18 pages

CS212 Sep2016 07 InPostPreExpressions

The document discusses infix, prefix, and postfix expressions and how to evaluate them using a stack. It provides examples of converting between infix and prefix/postfix notations. It then describes how to evaluate a postfix expression step-by-step using a stack, including pushing operands and calculating and pushing results when operators are encountered. The same process can be used for prefix expressions, but scanning from right to left. Evaluating expressions in this way allows using a stack to efficiently evaluate expressions in postfix or prefix form.

Uploaded by

Dahlia Gamal
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/ 18

Arab Academy for Science , Technology & Maritime Transport

College of Computing and Information Technology

Data Structures
and Algorithms (CS212)
Section 07 :
Infix , Prefix and Postfix
Expressions
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT
Infix , Prefix , Postfix Expressions
Infix : <operand> <operator> <operand>
Prefix: <operator> <operand> <operand>
Postfix: <operand> <operand> <operator>
Infix Prefix Postfix
2+3 +23 23+
p-q -pq pq-
a+b*c +a*bc abc*+
Infix is human readable.
Prefix & Postfix are good for machines.
Infix , Prefix , Postfix Expressions
Example Converting Infix to Prefix
a*b+c*d-e
[(a*b)+(c*d)]-e
[(*ab)+(*cd)]-e
[+*ab*cd]-e
-+*ab*cde
Infix , Prefix , Postfix Expressions

Example Converting Infix to Postfix


a*b+c*d-e
[(a*b)+(c*d)]-e
[(ab*)+(cd*)]-e
[ab*cd*+]-e
ab*cd*+e-
Evaluate postfix using stack
How we can evaluate this postfix using stack ?
23*54*+9- (every digit is a single number)
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

 First, create a stack

Initialize(s)

s
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

 Scan from left to right , if it’s an operand push it to


the stack
Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
}
2
s
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

 Scan from left to right , if it’s an operand push it to


the stack
Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
3 }
2
s
Evaluate postfix using stack
23*54*+9- (every digit is a single number)
 If it’s an operator , pop 2 elements then calculate
the result and push it to the stack
Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
2*3=6 Pop(s,x1,empty)
Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
6 Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
Pop(s,x1,empty)
5 Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
6 Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
4 Pop(s,x1,empty)
5 Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
6 Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
4*5=20 Pop(s,x1,empty)
20 Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
6 Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
20+6=26 Pop(s,x1,empty)
Pop(s,x2,empty)
26 R = Calculate(exp(i),x1,x2)
Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
Pop(s,x1,empty)
9 Pop(s,x2,empty)
26 R = Calculate(exp(i),x1,x2)
Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
26-9=17 Pop(s,x1,empty)
Pop(s,x2,empty)
17 R = Calculate(exp(i),x1,x2)
Push(s,R)
s
}
}
Evaluate postfix using stack
23*54*+9- (every digit is a single number)

 After scanning is finished , the top of the stack is


the final result Initialize(s)
For i=1 …. Length(exp) {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
Pop(s,x1,empty)
Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
Push(s,R)
17
}
s }
Pop(s,x,empty) Return x
Evaluate prefix using stack
 Same as postfix evaluation but scan from right to left
Initialize(s)
For i=Length(exp) …. 1 {
if(exp(i) is operand) {
Push(s,exp(i))
}
else if(exp(i) is operator){
Pop(s,x1,empty)
Pop(s,x2,empty)
R = Calculate(exp(i),x1,x2)
Push(s,R)
}
}
Pop(s,x,empty) Return x
END of Section
Thank You 
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT

You might also like