1 EXPRESSION TREES In this lecture we will discuss Expression trees as a method for storing & evaluating mathematical expressions.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Prefix, Postfix, Infix Notation
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Binary Tree Applications Chapter Trees Parse Trees What is parsing? Originally from language study The breaking up of sentences into component.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Computer Science 112 Fundamentals of Programming II Expression Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Lecture 8 Tree.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Review Use of Stack Introduction Stack in our life Stack Operations
Revised based on textbook author’s notes.
Paul Tymann and Andrew Watkins
ASTs, Grammars, Parsing, Tree traversals
Podcast Ch17a Title: Expression Trees
Stacks – Calculator Application
PART II STACK APPLICATIONS
CSC 172– Data Structures and Algorithms
Binary Tree Application Expression Tree
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Paul Tymann, Andrew Watkins,
ASTs, Grammars, Parsing, Tree traversals
Section 9.3 by Andrew Watkins
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Paul Tymann, Andrew Watkins,
CSC 143 Java Applications of Trees.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 20: Binary Trees.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

1 EXPRESSION TREES In this lecture we will discuss Expression trees as a method for storing & evaluating mathematical expressions

2 zResources: zBarrons Chapter 10 p.341 plus M/C #s 9, 10, 11 zAP Java Text Chapter 19 p zHandouts: z1.ExpressionTree.java

3 zExpression tree: yAlgebraic Expression with parentheses and a defined precedence of operations yA nested structure that can be represented by a Binary (expression) Tree

4 zExample: Operator + - * / / \ 1 st 2 nd operand operand za + b can be represented as:+ / \ a b

5 zEach node of the Expression tree contains a “TOKEN” which is either an operator (+ - * /) or an operand ( some #) zThe operand can be a variable or a number

6 zExample: (a + 1) (bc + 5 – c) can be represented as:

7 zExample: * / \ + - / \ / \ a 1 + c / \ * 5 / \ b c

8 zOperators are represented by nodes with children zOperands are represented by leaves

9 zEvaluating Expressions Represented by Trees: xThe evaluation of an expression tree includes calculating the value of an expression when given values for the operands’ variables. xRecursion is the easiest way to implement an evaluation function for expression trees.

10 zHere is the process:  If a node is an operand, we fetch its value  If a node is an operator, we apply that operator to the results of an evaluation of its left and right subtrees

11 * ---- operator / \ b c operands xCODE EXAMPLE “ExpressionTree.java”: Walk thru the code’s evaluate function *** Assume operands & operators are represented by character strings in tree nodes ***

12 String value = (String) node.getValue (); double leftVal, rightVal; if (value.equals ("+")) { leftVal = evaluate (node.getLeft ()); rightVal = evaluate(node.getRight ()); return leftValue + rightValue; } else return Double.parseDouble (value);

13 // IN SPVM TreeNode exprTree; // Convert the expression into a tree. exprTree = (TreeNode) TreeUtil.createExpressionTree (expr); result = evaluate (exprTree); System.out.println ("Result = " + result);

14 (a + 1) (bc + 5 – c) * / \ + - / \ / \ a 1 + c / \ * 5 / \ b c

15 PREFIX, POSTFIX & INFIX Notations zINFIX Notation: yAn inorder traversal of an expression tree printed out fully parenthesized yConventional algebraic notation where an operator is placed BETWEEN the operands x + y yRequires recursive evaluation of subtrees

16 zINFIX Notation: Infix Notation: ( a + 1 ) *((( b * c ) + 5 ) - c)

17 zPrefix and Postfix: zConvenient for evaluating expressions:  they do not use parenthesis  do not need to take into account order of precedence  order of operations can be reconstructed from the expression

18 zPrefix and Postfix: yThese notations for an algebraic expression can be generated by traversing the expression in preorder (for prefix) and postorder (for postfix)

19 zPREFIX Notation: (N L R) yPlace the operator BEFORE the operands +xy zHowever, there is no need for parenthesis as there is only 1 way to correctly place them zPrefix notation is also called Polish Notation Prefix Notation: * + a * b c 5 c

20 zPOSTFIX Notation: (L R N) zPlace the operator AFTER the operands xy+ zAgain, there is no need for parenthesis as there is only 1 way to correctly place them zPostfix notation is also called Reverse Polish Notation Postfix Notation: a 1 + b c * 5 + c - *

21 Evaluation of an Expression using Prefix and Postfix zPrefix & Postfix allow us to evaluate an algebraic expression in a single, sequential swipe

22 zPOSTFIX Notation can be evaluated: yProceed from LEFT to RIGHT using a temporary STACK for holding unused operands and intermediate results

23 zPOSTFIX Notation can be evaluated... yAlgorithm:  going from LEFT to RIGHT consider the next token  if it is an operand (#) push its value on the stack  if it is an operator:  pop the second operand  pop the first operand  perform the operation  push the result on the stack zwe will be left with one value on the stack ---- the result of the evaluation

24 zPOSTFIX Notation can be evaluated... yEXAMPLE: a 1 + b c * 5 + c - * Lets Draw the Stack’s evolution showing the state of the stack AFTER each token is encountered

25 zPREFIX Notation can be evaluated... yProceed from RIGHT to LEFT using a temporary STACK for holding unused operands and intermediate results

26 zPREFIX Notation can be evaluated... xAlgorithm:  going from RIGHT to LEFT consider the next token  if it is an operand (#) push its value on the stack  if it is an operator:  pop the first operand  pop the Second operand  perform the operation  push the result on the stack zwe will be left with one value on the stack ---- the result of the evaluation

27 zNOTE:the operands appear in the same order in infix, prefix and postfix notations. Only the position of the operators is different. Infix Notation: ( a + 1 ) * ((( b * c ) + 5 ) - c) Prefix Notation: * + a * b c 5 c Postfix Notation: a 1 + b c * 5 + c - *

28 Build an expression tree from a postfix or prefix notation zInstead of numbers, we push references to nodes on the stack, instead of performing an operation, we link the operator node to the nodes of the operands.

29 yAlgorthim:  going from LEFT to RIGHT consider the next token  Create a new node for the token  If it is an operand:  Set the LEFT and RIGHT children to null  Push the reference to the new node on the stack yIf it is an operator:  Pop a reference from a stack  Set the new node’s RIGHT child to it  Pop another reference from the stack  Set the new node’s LEFT child to it  Push the reference to the new node on the stack