Top Banner
Computer Science 112 Fundamentals of Programming II Expression Trees
39

Computer Science 112 Fundamentals of Programming II Expression Trees.

Dec 27, 2015

Download

Documents

Philip Garrison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Science 112 Fundamentals of Programming II Expression Trees.

Computer Science 112

Fundamentals of Programming IIExpression Trees

Page 2: Computer Science 112 Fundamentals of Programming II Expression Trees.

A Parse (Expression) Tree

Evaluator

Source language program

Syntax error messages

Ok or not OKParser

The value of the expression

Semantic error messages

A parse tree

Page 3: Computer Science 112 Fundamentals of Programming II Expression Trees.

Trees for Binary Expressions

• An expression tree for a number is a node containing the number

• Otherwise, the tree is a node containing an operator and links to left and right subtrees

• The subtrees contain the operands of the expression

Page 4: Computer Science 112 Fundamentals of Programming II Expression Trees.

Example Expression Trees

23 23

Page 5: Computer Science 112 Fundamentals of Programming II Expression Trees.

Example Expression Trees

23 23

3 + 5+

3 5

Root nodeRight subtreeLeft subtree

Page 6: Computer Science 112 Fundamentals of Programming II Expression Trees.

Example Expression Trees

23 23

3 + 5+

3 5

3 + 5 * 4 +

3 *

5 4

Root nodeRight subtreeLeft subtree

Page 7: Computer Science 112 Fundamentals of Programming II Expression Trees.

Example Expression Trees

23 23

3 + 5+

3 5

3 + 5 * 4 +

3 *

5 4

Root nodeRight subtreeLeft subtree

(3 + 5) * 4

+

3 5

*

4

Page 8: Computer Science 112 Fundamentals of Programming II Expression Trees.

Operator Precedence

3 + 5 * 4 +

3 *

5 4

(3 + 5) * 4

+

3 5

*

4

5 * 4 + 3 +

*

5 4

3

Operators with higher precedence appear lowerin the tree, unless overriddenby parentheses.

Page 9: Computer Science 112 Fundamentals of Programming II Expression Trees.

Operator Precedence

5 * 4 - 3 -

*

5 4

3

When operators have equalprecedence, the ones to theleft appear lower in the tree.

5 + 4 - 3 -

+

5 4

3

5 + 4 - 3 + 6 -

+

5 4

3

+

6

Page 10: Computer Science 112 Fundamentals of Programming II Expression Trees.

Expression Trees

5 * 4 - 3 -

*

5 4

3

5 + 4 - 3 -

+

5 4

3

5 + 4 - 3 + 6 -

+

5 4

3

+

6

33

Page 11: Computer Science 112 Fundamentals of Programming II Expression Trees.

Properties of Expression Trees

• Numbers are in leaf nodes

• Operators are in interior nodes

• All interior nodes have exactly two children

• Both types of nodes implement the same interface

Page 12: Computer Science 112 Fundamentals of Programming II Expression Trees.

Things We Might Want from an Expression Tree

• Its value

• Its prefix form

• Its infix form

• Its postfix form

Page 13: Computer Science 112 Fundamentals of Programming II Expression Trees.

Resources# The interface to all expression tree classes

value() # Returns the int value of the expression prefix() # Returns a string in prefixinfix() # Returns a string in infix (fully parenthesized)postfix() # Returns a string in postfix

# Implementing classes

LeafNode(data)

InteriorNode(opToken, leftOperandNode, rightOperandNode)

Page 14: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluating an Expression Tree

• A leaf node returns the number contained in it

• An interior node evaluates its left and right subtrees, applies its operator to these values, and returns the result

Page 15: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Contains an operator, soevaluate left and right subtrees

Page 16: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4Contains anumber, soreturn 3

Page 17: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Contains an operator, soevaluate left and right subtrees

Page 18: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Contains anumber, soreturn 5

Page 19: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Contains anumber, soreturn 4

Page 20: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Return 5 * 4 = 20

Page 21: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluate This One

3 + 5 * 4 +

3 *

5 4

Return 3 + 20 = 23

Page 22: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluating a Leaf Nodeclass LeafNode(object):

def __init__(self, data): self.data = data

def value(self): return self.data

# Other methods go here

A LeafNode forms the base case for the recursive processing of expression trees

Page 23: Computer Science 112 Fundamentals of Programming II Expression Trees.

Evaluating an Interior Nodeclass InteriorNode(object):

def __init__(self, opToken, leftOperandNode, rightOperandNode): self.operator = opToken self.left = leftOperandNode self.right = rightOperandNode

def value(self): return self.computeValue(op, self.left.value() self.right.value()) # Other methods go here

The method computeValue is the same one we used in the stack-based evaluator

Page 24: Computer Science 112 Fundamentals of Programming II Expression Trees.

Tree Traversals

• An expression tree supports three types of traversals– preorder (visit the node, then go left, then go right)– inorder (go left, then visit the node, then go right)– postorder (go left, then go right, then the visit node

Page 25: Computer Science 112 Fundamentals of Programming II Expression Trees.

Tree Traversals

• An expression tree supports three types of traversals– preorder (visit the node, then go left, then go right)– inorder (go left, then visit the node, then go right)– postorder (go left, then go right, then visit the node

• These traversals can generate the prefix, infix, and postfix notations of an expression

Page 26: Computer Science 112 Fundamentals of Programming II Expression Trees.

Code for postfixclass LeafNode(object):

def __init__(self, data): self.data = data

def postfix(self): return str(self.data)

# Other methods go here

class InteriorNode(object):

def __init__(self, opToken, leftOperandNode, rightOperandNode): self.operator = opToken self.left = leftOperandNode self.right = rightOperandNode

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 27: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Node an operator, so go left

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 28: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Node an operand, return "3"

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 29: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Now go right

3

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 30: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Node an operator, so go left

3

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 31: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Node an operand, return "5"

3 5

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 32: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Now go right

3 5

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 33: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Node an operand, return "4"

3 5 4

def postfix(self): return str(self.data)

def postfix(self): return self. left.postfix() + " " + self. right.postfix() \ + " " + str(self.operator)

Page 34: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Return "*"

3 5 4 *

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 35: Computer Science 112 Fundamentals of Programming II Expression Trees.

Generate Postfix

3 + 5 * 4 +

3 *

5 4

Return "+"

3 5 4 * +

def postfix(self): return str(self.data)

def postfix(self): return self.left.postfix() + " " + self.right.postfix() \ + " " + str(self.operator)

Page 36: Computer Science 112 Fundamentals of Programming II Expression Trees.

Building an Expression Tree

• Each parsing method builds a tree that represents the portion of the expression for which it is responsible

• Each parsing method returns its tree to the caller

Page 37: Computer Science 112 Fundamentals of Programming II Expression Trees.

Build a Tree for Primaries# primary = number | "(" expression ")"def primary(self): token = scanner.get() if token.getType() == Token.INT: tree = LeafNode(token.getValue()) scanner.next() elif token.getType() == Token.L_PAR: scanner.next() tree = self.expression() self._accept(scanner.get(), Token.R_PAR, "')' expected") scanner.next() else: tree = LeafNode(token.getValue()) fatalError (token, "unexpected token") return tree

Page 38: Computer Science 112 Fundamentals of Programming II Expression Trees.

Build a Tree for Expressions

# expression = term { addingOperator term }def expression(self): tree = self.term() token = scanner.get() while token.getType() in (Token.PLUS, Token.MINUS): scanner.next() tree = InteriorNode(token, tree, self.term()) token = scanner.get() return tree

Page 39: Computer Science 112 Fundamentals of Programming II Expression Trees.

For Friday

Heaps