cs302 – data structures...evaluating infix expressions •converting infix expressions to postfix...
TRANSCRIPT
![Page 1: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/1.jpg)
CS302 - Data Structuresusing C++
Kostas Alexis
Topic: Using the ADT Stack
![Page 2: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/2.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
![Page 3: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/3.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
20 – 2 * 2 ^ 3
![Page 4: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/4.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
20 – 2 * 8
![Page 5: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/5.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
20 – 16
![Page 6: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/6.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
4
![Page 7: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/7.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
![Page 8: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/8.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
5 + 6
![Page 9: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/9.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
+ 6
5
![Page 10: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/10.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
6
5 +
operatorStack
![Page 11: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/11.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
5 6 +
operatorStack
![Page 12: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/12.jpg)
Algebraic Expression
• Operator Precedence
• Parenthesis ( )
• Exponentiation ^ (not a C++ expression)
• Multiplication and Division * /
• Addition and Subtraction + -
• Binary Operators
• Require two operands
• 4 + 5
• Unary Operators
• Single operand
• - 6
• Infix
• Common notation
• 5 + 6
• 5 + 6 * 7
• (5 + 6) * 7
• Prefix
• Functional languages
• + 5 6
• + * 7 6 5
• * + 5 6 7
• Postfix
• Reverse Polish Notation
• 5 6 +
• 5 6 7 * +
• 7 5 6 + *
5 6 +
operatorStack
![Page 13: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/13.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
• Use stacks to do so
![Page 14: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/14.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
![Page 15: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/15.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix 5 * ( 4 + 2 ) ^ 3
infix postfix
![Page 16: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/16.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
5 * ( 4 + 2 ) ^ 3
infix postfix
![Page 17: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/17.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postfix expression
• If their precedence >= that of the operator in the infix expression until operatorStack is empty or a ( is reached
• Push operator onto the operatorStack
* ( 4 + 2 ) ^ 3 5
infix postfix
![Page 18: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/18.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStack is empty or a ( is reached
• Push operator onto the operatorStack
* ( 4 + 2 ) ^ 3 5
infix postfix
operatorStack
![Page 19: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/19.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStack is empty or a ( is reached
• Push operator onto the operatorStack
* ( 4 + 2 ) ^ 3 5
infix postfix
*
operatorStack
![Page 20: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/20.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStack is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
( 4 + 2 ) ^ 3 5
infix postfix
*
operatorStack
![Page 21: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/21.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
4 + 2 ) ^ 3 5
infix postfix
(
*
operatorStack
![Page 22: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/22.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
4 + 2 ) ^ 3 5
infix postfix
(
*
operatorStack
![Page 23: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/23.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
+ 2 ) ^ 3 5 4
infix postfix
(
*
operatorStack
![Page 24: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/24.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
+ 2 ) ^ 3 5 4
infix postfix
(
*
operatorStack
![Page 25: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/25.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
2 ) ^ 3 5 4
infix postfix
+
(
*
operatorStack
![Page 26: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/26.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
2 ) ^ 3 5 4
infix postfix
+
(
*
operatorStack
![Page 27: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/27.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
) ^ 3 5 4 2
infix postfix
+
(
*
operatorStack
![Page 28: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/28.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
) ^ 3 5 4 2
infix postfix
+
(
*
operatorStack
![Page 29: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/29.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
) ^ 3 5 4 2 +
infix postfix
(
*
operatorStack
![Page 30: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/30.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
^ 3 5 4 2 +
infix postfix
*
operatorStack
![Page 31: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/31.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
^ 3 5 4 2 +
infix postfix
*
operatorStack
![Page 32: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/32.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
3 5 4 2 +
infix postfix
^
*
operatorStack
![Page 33: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/33.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
3 5 4 2 +
infix postfix
^
*
operatorStack
![Page 34: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/34.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
5 4 2 + 3
infix postfix
^
*
operatorStack
![Page 35: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/35.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
5 4 2 + 3 ^
infix postfix
*
operatorStack
![Page 36: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/36.jpg)
Evaluating Infix Expressions
• Converting Infix Expressions to Postfix
• Operand – append to the postfix expression
• Operators * / + - etc
• If operatorStack is not empty
• Pop operators and append to the postdix expression
• If their precedence >= that of the operator in the infix expression until operatorStac is empty or a ( is reached
• Push operator onto the operatorStack
• ( - push onto operatorStack
• ) - pop operators from operatorStack and append to postfix expression until ( is popped
5 4 2 + 3 ^ *
infix postfix
operatorStack
![Page 37: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/37.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
5 4 2 + 3 ^ *
postfix
operandStack
![Page 38: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/38.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
5 4 2 + 3 ^ *
postfix
operandStack
![Page 39: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/39.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
4 2 + 3 ^ *
postfix
5
operandStack
![Page 40: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/40.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
4 2 + 3 ^ *
postfix
5
operandStack
![Page 41: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/41.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
2 + 3 ^ *
postfix
4
5
operandStack
![Page 42: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/42.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
2 + 3 ^ *
postfix
4
5
operandStack
![Page 43: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/43.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
+ 3 ^ *
postfix
2
4
5
operandStack
![Page 44: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/44.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
+ 3 ^ *
postfix
2
4
5
operandStack
![Page 45: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/45.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
3 ^ *
postfix
5
operandStack
4 + 2
![Page 46: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/46.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
3 ^ *
postfix
6
5
operandStack
6
![Page 47: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/47.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
3 ^ *
postfix
6
5
operandStack
![Page 48: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/48.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
^ *
postfix
3
6
5
operandStack
![Page 49: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/49.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
^ *
postfix
3
6
5
operandStack
![Page 50: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/50.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
^ *
postfix
5
operandStack
6^3
![Page 51: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/51.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
^ *
postfix
5
operandStack
216
![Page 52: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/52.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
*
postfix
216
5
operandStack
![Page 53: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/53.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
*
postfix
216
5
operandStack
![Page 54: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/54.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
*
postfix
operandStack
5*216
![Page 55: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/55.jpg)
Evaluating Postfix Expressions
• Scan characters in the postfix expression
• When an operand is entered
• push it onto the operandStack
• When an operator is entered
• Apply it to the top two operands of the operandStack
• pop the operands from the operandStack
• push the result of the operation onto the operandStack
postfix
operandStack
1080
![Page 56: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/56.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
Algorithm convertToPostfix(infix)
// Converts an infix expression to an equivalent postfix expression
infixExpression = infix expression to process
operatorStack = a new empty stack
postfixExpression = a new empty string
![Page 57: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/57.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
Algorithm convertToPostfix(infix)
// Converts an infix expression to an equivalent postfix expression
infixExpression = infix expression to process
operatorStack = a new empty stack
postfixExpression = a new empty string
while (infixQueue has characters left to parse)
{
nextCharacter = next non-blank infixExpression character
process(nextCharacter)
}
while (!operatorStack.isEmpty())
{
topOperator = operatorStack.pop()
append topOperator to postfixExpression
}
return postfixExpression
![Page 58: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/58.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
// Process nextCharacter algorithm
switch (nextCharacter)
{
case variable:
append nextCharacter to postfixExpression
case ‘^’:
operatorStack.push(nextCharacter)
case ‘(’:
operatorStack.push(nextCharacter)
![Page 59: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/59.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
// Process nextCharacter algorithm
switch (nextCharacter)
{
case variable:
append nextCharacter to postfixExpression
case ‘^’:
operatorStack.push(nextCharacter)
case ‘(’:
operatorStack.push(nextCharacter)
case ‘)’:
topOperator = operatorStack.pop()
while(topOperator != ‘(’)
append topOperator to postfixExpression
topOperator = operatorStack.pop()
default:
break;
}
![Page 60: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/60.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
// Process nextCharacter algorithm
switch (nextCharacter)
{
case variable:
append nextCharacter to postfixExpression
case ‘^’:
operatorStack.push(nextCharacter)
case ‘(’:
operatorStack.push(nextCharacter)
case ‘)’:
topOperator = operatorStack.pop()
while(topOperator != ‘(’)
append topOperator to postfixExpression
topOperator = operatorStack.pop()
case ‘+’: case ‘-’: case ‘*’: case ‘/’:
while(!operatorStack.isEmpty() and precedence of
nextCharacter <= precedence of operatorStack.peek())
topOperator = operatorStack.pop()
append topOperator to postfixExpression
topOperator = operatorStack.pop()
peratorStack.push(nextCharacter)
default:
break;
}
![Page 61: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/61.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
Algorithm convertToPostfix(infix)
// Converts an infix expression to an equivalent postfix expression
infixExpression = infix expression to process
operatorStack = a new empty stack
postfixExpression = a new empty string
while (infixQueue has characters left to parse)
{
nextCharacter = next non-blank infixExpression character
process(nextCharacter)
}
while (!operatorStack.isEmpty())
{
topOperator = operatorStack.pop()
append topOperator to postfixExpression
}
return postfixExpression
![Page 62: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/62.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
Algorithm evaluatePostfix(postfix)
// evaluate a postfix expression
valueStack = a new empty stack
while(postfixExpression has characters left to process)
{
nextCharacter = next non blank character of postfixExpression
switch (nextCharacter)
{
case variable:
valueStack.push(value of var nextCharacter)
break
case ‘+’: case ‘-’: case ‘*’; case ‘/’: case ‘*’:
operandTwo = valueStack.peek()
valueStack.pop()
operandOne = valueStack.peek()
valueStack.pop()
result = apply operation in nextCharacter to its operands operandOne and operandTwo
break
default: break
} // end switch
} // end while
![Page 63: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/63.jpg)
Evaluating Infix Expressions
• To evaluate an infix expression
• Convert the infix expression to postfix form
• Evaluate the postfix expression
Algorithm evaluatePostfix(postfix)
// evaluate a postfix expression
valueStack = a new empty stack
while(postfixExpression has characters left to process)
{
nextCharacter = next non blank character of postfixExpression
switch (nextCharacter)
{
case variable:
valueStack.push(value of var nextCharacter)
break
case ‘+’: case ‘-’: case ‘*’; case ‘/’: case ‘*’:
operandTwo = valueStack.peek()
valueStack.pop()
operandOne = valueStack.peek()
valueStack.pop()
result = apply operation in nextCharacter to its operands operandOne and operandTwo
break
default: break
} // end switch
} // end while
return valueStack.peek()
![Page 64: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis](https://reader033.vdocument.in/reader033/viewer/2022051310/601361b01ee8ff09f676dc15/html5/thumbnails/64.jpg)
Thank you