Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Arithmetic Expressions
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
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.
Transforming Infix to Postfix
Binary Tree Applications Chapter Trees Parse Trees What is parsing? Originally from language study The breaking up of sentences into component.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
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.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
1 EXPRESSION TREES In this lecture we will discuss Expression trees as a method for storing & evaluating mathematical expressions.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
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.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Review Use of Stack Introduction Stack in our life Stack Operations
Infix to postfix conversion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Podcast Ch17a Title: Expression Trees
Stacks Chapter 4.
Stack application: postponing data usage
PART II STACK APPLICATIONS
Binary Tree Application Expression Tree
CS212: Data Structures and Algorithms
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Paul Tymann, Andrew Watkins,
Stacks and Queues 1.
Section 9.3 by Andrew Watkins
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Paul Tymann, Andrew Watkins,
Stacks.
CSC 143 Java Applications of Trees.
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
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:

Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix, and Postfix forms Infix to Postfix conversion Constructing an expression tree from a postfix expression

What is an Expression tree? An expression tree for an arithmetic, relational, or logical expression is a binary tree in which: The parentheses in the expression do not appear. The leaves are the variables or constants in the expression. The non-leaf nodes are the operators in the expression: A node for a binary operator has two non-empty subtrees. A node for a unary operator has one non-empty subtree. The operators, constants, and variables are arranged in such a way that an inorder traversal of the tree produces the original expression without parentheses.

Expression Tree Examples Inorder Traversal ResultExpression TreeExpression a + 3(a+3) 3+4* (4*5-(9+6)) log xlog(x) n ! + 3a + -3 * log x ! n

Expression tree implementation The class hierarchy of Expression trees is: Comparable Container Tree AbstractObject AbstractContainer AbstractTree BinaryTree ExpressionTree

Expression tree implementation (contd.) 1 public class ExpressionTree extends BinaryTree{ 2 public ExpressionTree(Object obj, ExpressionTree left, ExpressionTree right){ 3 super(obj, left, right) ; 4 } 5 public ExpressionTree(){ super(null, null, null) ; } 6 7 public ExpressionTree(Object obj){ 8 this(obj, new ExpressionTree(), new ExpressionTree()) ; 9 } public void attachTreeToLeft(ExpressionTree tree){ 12 left = tree ; 13 } public void attachTreeToRight(ExpressionTree tree){ 16 right = tree ; } 17 // }

Why Expression trees? Expression trees are used to remove ambiguity in expressions. Consider the algebraic expression * Without the use of precedence rules or parentheses, different orders of evaluation are possible: ((2-3)*(4+5)) = -9 ((2-(3*4))+5) = -5 (2-((3*4)+5)) = -15 (((2-3)*4)+5) = 1 (2-(3*(4+5))) = -25 The expression is ambiguous because it uses infix notation: each operator is placed between its operands.

Why Expression trees? (contd.) Storing a fully parenthesized expression, such as ((x+2)-(y*(4-z))), is wasteful, since the parentheses in the expression need to be stored to properly evaluate the expression. A compiler will read an expression in a language like Java, and transform it into an expression tree. Expression trees impose a hierarchy on the operations in the expression. Terms deeper in the tree get evaluated first. This allows the establishment of the correct precedence of operations without using parentheses. Expression trees can be very useful for: Evaluation of the expression. Generating correct compiler code to actually compute the expression's value at execution time. Performing symbolic mathematical operations (such as differentiation) on the expression.

Evaluating an Expression tree Assuming that t is a valid expression tree, a pseudo code algorithm for evaluating the expression tree is: 1 evaluate(ExpressionTree t){ 2 if(t is a leaf) 3 return value of t's operand ; 4 else{ 5 operator = t.element ; 6 operand1 = evaluate(t.left) ; 7 operand2 = evaluate(t.right) ; 8 return(applyOperator(operand1, operator, operand2) ; 9 } 10} + * Order of evaluation: (2 + ((5 – 1) * 3))

Prefix, Infix, and Postfix Forms A preorder traversal of an expression tree yields the prefix (or polish) form of the expression. In this form, every operator appears before its operand(s). An inorder traversal of an expression tree yields the infix form of the expression. In this form, every operator appears between its operand(s). A postorder traversal of an expression tree yields the postfix (or reverse polish) form of the expression. In this form, every operator appears after its operand(s). Prefix form: + a * - b c d Infix form: a + b - c * d Postfix form: a b c - d * + + *a d- cb

Prefix, Infix, and Postfix Forms (contd.) Postfix formsInfix formsPostfix formsExpression a b +a + b+ a b(a + b) a b c * -a - b * c- a * b ca - (b * c) x loglog x log (x) n ! ! nn !

Prefix, Infix, and Postfix Forms (contd.) A prefix or postfix form corresponds to exactly one expression tree. An infix form may correspond to more than one expression tree. It is therefore not suitable for expression evaluation. Infix formExpression TreeExpression * (2 - 3) * (4 + 5) * (3 * 4 + 5) * * 34 *

Infix to Postfix conversion (manual) An Infix to Postfix manual conversion algorithm is: 1. Completely parenthesize the infix expression according to order of priority you want. 2. Move each operator to its corresponding right parenthesis. 3. Remove all parentheses. Examples: * 5(3 + (4 * 5) )3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence

Infix to Prefix conversion (manual) An Infix to Postfix manual conversion algorithm is: 1 Completely parenthesize the infix expression according to order of priority you want. 2 Move each operator to its corresponding left parenthesis. 3Remove all parentheses. Examples: * 5(3 + (4 * 5) )3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence

Infix to Postfix conversion (pseudo code) 1 Initialize stack to empty ; Initialize postFixQueue to empty ; 2while(infix expression has more tokens) 3{ // must be a valid infix expression 4 get current token ; 5 if(current token is an operand) 6 enqueue it in postFixQueue ; 7 else if(current token is a left parenthesis) 8 push it onto stack ; 9 else if(current token is a right parenthesis) 10 { 11 pop operators from the stack and enqueue them in postFixQueue until the top 12 of stack is a left parenthesis ; 13 pop and discard the left parenthesis ; 14 } 15 else if(current token is an operator) 16 { 17 while(stack is not empty AND top of stack is not a left parenthesis AND top 18 of stack is not a left-associative operator that has lower precedence than 19 the current token AND top of stack is not a right-associative operator that 20 has the same precedence as the current token) 21 pop operators from the stack and enqueue them in postFixQueue ; 22 push the current token onto the stack ; 23 } 24 } 25 while(stack is not empty) 26 { pop token from stack ; 27 enqueue token in postFixQueue ; 28 }

Constructing an expression tree from a postfix expression The pseudo code algorithm to convert a valid postfix expression, containing binary operators, to an expression tree: 1while(not the end of the expression) 2{ 3 if(the next symbol in the expression is an operand) 4 { 5 create a node for the operand ; 6 push the reference to the created node onto the stack ; 7 } 8 if(the next symbol in the expression is a binary operator) 9 { 10 create a node for the operator ; 11 pop from the stack a reference to an operand ; 12 make the operand the right subtree of the operator node ; 13 pop from the stack a reference to an operand ; 14 make the operand the left subtree of the operator node ; 15 push the reference to the operator node onto the stack ; 16 } 17 }