cs302 – data structures...evaluating infix expressions •converting infix expressions to postfix...

64
CS302 - Data Structures using C++ Kostas Alexis Topic: Using the ADT Stack

Upload: others

Post on 27-Sep-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS302 – Data Structures...Evaluating Infix Expressions •Converting Infix Expressions to Postfix •Operand–append to the postfix expression •Operators * / + - etc •If operatorStackis

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thank you