Expressions and Expression Trees CS2: Data Structures and Algorithms - - PowerPoint PPT Presentation

expressions and expression trees
SMART_READER_LITE
LIVE PREVIEW

Expressions and Expression Trees CS2: Data Structures and Algorithms - - PowerPoint PPT Presentation

Expressions and Expression Trees CS2: Data Structures and Algorithms Colorado State University Russ Wakefield, Sudipto Ghosh and Wim Bohm Infix Expressions Infix notation places each operator between two operands for binary operators: A *


slide-1
SLIDE 1

Expressions and Expression Trees

CS2: Data Structures and Algorithms Colorado State University

Russ Wakefield, Sudipto Ghosh and Wim Bohm

slide-2
SLIDE 2

Infix Expressions

✦ Infix notation places each operator between

two operands for binary operators:

✦ This is the customary way we write math

formulas in most programming languages.

✦ However, we need to specify an order of

evaluation in order to get the correct answer.

CS165: Data Structures and Algorithms – Spring Semester 2020

2

A * x * x + B * x + C; // quadratic equation

slide-3
SLIDE 3

✦ The evaluation order you may have learned

in math class is named PEMDAS:

✦ Also need to account for unary, logical and

relational operators, pre/post increment, etc.

✦ Java has a similar order of evaluation. Parentheses → Exponentiation → Multiplication, Division → Addition, Subtraction

Evaluation Order

3

CS165: Data Structures and Algorithms – Spring Semester 2020

slide-4
SLIDE 4

Reminder: Java Precedence

parentheses ( ) unary ++ -- + - ~ ! multiplicative * / % additive + - shift << >> relational < > <= >= instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary conditional ? : assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

4

CS165: Data Structures and Algorithms – Spring Semester 2020

slide-5
SLIDE 5

Associativity

Operators with same precedence:

* /

and

+ -

are evaluated left to right: 2-3-4 = (2-3)-4

5

CS165: Data Structures and Algorithms – Spring Semester 2020

slide-6
SLIDE 6

Expression Trees

✦ Parsing decomposes source code and builds a

representation that represents its structure.

✦ Parsing generally results in a data structure such

as a tree:

CS165: Data Structures and Algorithms – Spring Semester 2020

6

A * x * x + B * x + C A x * x * B x * + C + // postfix version

+ C + * * * x B x x A

slide-7
SLIDE 7

Infix, Postfix, Prefix Conversion

CS165: Data Structures and Algorithms – Spring Semester 2020

7

Infix Postfix Prefix Notes A * B + C / D A B * C D / + + * A B / C D multiply A and B, divide C by D, add the results A * (B + C) / D A B C + * D / / * A + B C D add B and C, multiply by A, divide by D A * (B + C / D) A B C D / + * * A + B / C D divide C by D, add B, multiply by A

slide-8
SLIDE 8

Expression Trees

CS165: Data Structures and Algorithms – Spring Semester 2020

8

/ * +

/ \ / \ / \

* D A +

* /

/ \ / \ / \ / \ A + B / A B C D / \ / \ B C C D

Infix: A*B + C/D A*(B+C)/D A*(B+C/D) Postfix: A B * C D / + A B C + * D / A B C D / + *

Evaluate Post Order Left to Right Notice: the deeper in the tree, the higher the precedence

slide-9
SLIDE 9

Evaluating expression trees

✦ By postfix traversal:

– Internal node: operator

first evaluate children sub-trees

then evaluate the operator and return result

– Leaf: operand

either identifier: produce current value

  • r constant: produce value

return result

9

CS165: Data Structures and Algorithms – Spring Semester 2020

slide-10
SLIDE 10

Java support for trees?

✦ Question: Does the Java Collection

framework have support for binary trees?

✦ Answer: No, you have to build your own

trees using the same techniques as with linked lists.

CS165: Data Structures and Algorithms – Spring Semester 2020

10

slide-11
SLIDE 11

11

CS165: Data Structures and Algorithms – Spring Semester 2020

Post Order Evaluation of an Integer Expression Tree

private Integer evalBin(String op, Integer left, Integer right){ if(op.equals("+")) return left + right; if(op.equals("-")) return left - right; if(op.equals("*")) return left * right; if(op.equals("/")) return left / right; else return null; } public int postorderEval(TreeNode node){ String token = node.getItem(); if( isOperator(token)){ //internal node Integer left = postorderEval(node.getLeft()); Integer right = postorderEval(node.getRight()); return evalBin(token, left, right); } else // leafs are int literals here return Integer.parseInt(token); }

slide-12
SLIDE 12

Post order evaluation of 3*4+10-2

12

CS165: Data Structures and Algorithms – Spring Semester 2017

  • / \

+ 2

/ \

* 10

/ \ 3 4

  • / \

+ 2

/ \

* 10

/ 3 \

3

4

  • / \

+ 2

/ \

* 10

/ 3 \4

3 4

  • / \

+ 2

12 / \

* 10

/ 3 \4

3 4

  • / \

+ 2

12 / \ 10

* 10

/ 3 \4

3 4

  • 22 / \

+

2 12 / \ 10

* 10

/ 3 \4

3 4

  • 22 / \2

+

2 12 / \ 10

* 10

/ 3 \4

3 4 20

  • 22 / \2

+

2 12 / \ 10

* 10

/ 3 \4

3 4