Chapter 5-1
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
İf we look at the arithmetic expressions, we encounter operand, operator
and parenthesis.
2+3 , (p*q), 2+5*6
For evaluting this expressions, we should parse and them calculate.
<operand><operator><operand> (infix)
Operand: Any object that operation is performed.
Operator: In mathematics and sometimes in computer programming, an
operator is a character that represents an action, as for example x is an
arithmetic operator that represents multiplication.
2
Evaluation of Expressions
 For example 2+3*5-1*4
 To evaluate this expressions you should know precedence of
operator.
 Order of operations
1. Parentheses {()}
2. Exponents 2^3^2 (right to left)
3. Multiplication and division (left to right)
4. Addition and subtraction (left to right)
2*6/3+5-2+8/4*2
3
Evaluation of Expressions
 Prefix (Polish Notation)
 <operator><operand><operand>
 İnfix prefix
 4+5 +45
 p/q /pq
 a*b+c +*abc
4
Evaluation of Expressions
 Postfix (Reverse Polish Notation)
 <operand><operand><operator>
 İnfix prefix postfix
 4+5 +45 45+
 p/q /pq pq/
 a*b+c +*abc ab*c+
5
Evaluation of Expressions
 a*b+c*d-2
 First step we should add parentheses
 {(a*b)+(c*d)}-2
 Next step is that operator will place to end of expression
 {(ab*)+(cd*)}-2
 {ab*cd*+}-2
 ab*cd*+2-
6
Evaluation of prefix and postfix
 2*3+4*5-6=>23*45*+6- (postfix)
 evaluatePostfix(exp){
create a Stack S
for i<-0 to length(exp)-1
{
if (exp[i] is operand)
push(exp[i])
else if (exp[i] is operator){
op2<-pop()
op1<-pop()
temp<perform(op1,op2,exp[i])
push(temp)
}
} return S[top]
}
7
Evaluation of Postfix with Stack
 2*3+4*5-6=>-+*23*456 (prefix)
 This algorithm is same with postfix algorithm. But this
algorithm starts from end of expression.
8
Evaluation of Prefix with Stack
 A+B*C-D*E
 InfixToPostfix(exp){
create a Stack S
String temp<-empty String
for i<-0 to length(exp)-1{
if exp[i] is operand
temp <-temp+exp[i]
else if exp[i] is operator
while(!S.empty()&&HasHigherPrecedence(s.top(),exp[i])){
temp <-temp + s.pop();
}
s.push(exp[i])
}
while(!s.empty()) temp<-temp+s.pop();
Return 1;
}
9
Infix to postfix
 ((A+B)*C-D)*E
 A+(B*C)
10
Infix With parentheses to Postfix

Data structure week y 5 1

  • 1.
    Chapter 5-1 Data Structures Assoc.Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.
    İf we lookat the arithmetic expressions, we encounter operand, operator and parenthesis. 2+3 , (p*q), 2+5*6 For evaluting this expressions, we should parse and them calculate. <operand><operator><operand> (infix) Operand: Any object that operation is performed. Operator: In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example x is an arithmetic operator that represents multiplication. 2 Evaluation of Expressions
  • 3.
     For example2+3*5-1*4  To evaluate this expressions you should know precedence of operator.  Order of operations 1. Parentheses {()} 2. Exponents 2^3^2 (right to left) 3. Multiplication and division (left to right) 4. Addition and subtraction (left to right) 2*6/3+5-2+8/4*2 3 Evaluation of Expressions
  • 4.
     Prefix (PolishNotation)  <operator><operand><operand>  İnfix prefix  4+5 +45  p/q /pq  a*b+c +*abc 4 Evaluation of Expressions
  • 5.
     Postfix (ReversePolish Notation)  <operand><operand><operator>  İnfix prefix postfix  4+5 +45 45+  p/q /pq pq/  a*b+c +*abc ab*c+ 5 Evaluation of Expressions
  • 6.
     a*b+c*d-2  Firststep we should add parentheses  {(a*b)+(c*d)}-2  Next step is that operator will place to end of expression  {(ab*)+(cd*)}-2  {ab*cd*+}-2  ab*cd*+2- 6 Evaluation of prefix and postfix
  • 7.
     2*3+4*5-6=>23*45*+6- (postfix) evaluatePostfix(exp){ create a Stack S for i<-0 to length(exp)-1 { if (exp[i] is operand) push(exp[i]) else if (exp[i] is operator){ op2<-pop() op1<-pop() temp<perform(op1,op2,exp[i]) push(temp) } } return S[top] } 7 Evaluation of Postfix with Stack
  • 8.
     2*3+4*5-6=>-+*23*456 (prefix) This algorithm is same with postfix algorithm. But this algorithm starts from end of expression. 8 Evaluation of Prefix with Stack
  • 9.
     A+B*C-D*E  InfixToPostfix(exp){ createa Stack S String temp<-empty String for i<-0 to length(exp)-1{ if exp[i] is operand temp <-temp+exp[i] else if exp[i] is operator while(!S.empty()&&HasHigherPrecedence(s.top(),exp[i])){ temp <-temp + s.pop(); } s.push(exp[i]) } while(!s.empty()) temp<-temp+s.pop(); Return 1; } 9 Infix to postfix
  • 10.
     ((A+B)*C-D)*E  A+(B*C) 10 InfixWith parentheses to Postfix