Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Transforming Infix to Postfix
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
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.
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.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures Week 7 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
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.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Discrete Structures Trees (Ch. 11)
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Data Structure By Amee Trivedi.
Data Structures Binary Trees 1.
Binary Search Tree (BST)
CSC 172– Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Principles of Computing – UFCFA3-30-1
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Data Structures and Algorithm Analysis Trees
CE 221 Data Structures and Algorithms
CSC 143 Java Applications of Trees.
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
Data Structures Using C++ 2E
Presentation transcript:

Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation

Data Structures : Project 5 Expression Trees In this project, we will  Use the tree data structure and apply them to expression evaluation again  How to generate code for an expression given its expression tree.  Understand how to traverse trees

Data Structures : Project 5 Binary Trees A special class of the general trees. Restrict each node to have at most two children.  These two children are called the left and the right child of the node.  Easy to implement and program.  Still, several applications.

Data Structures : Project 5 An Example Figure shows a binary tree rooted at A. All notions such as  height  depth  parent/child  ancestor/descendant are applicable.

Data Structures : Project 5 Application to Expression Evaluation We know what expression evaluation is. We deal with binary operators. An expression tree for a expression with only unary or binary operators is a binary tree where the leaf nodes are the operands and the internal nodes are the operators.

Data Structures : Project 5 Why Expression Trees? Useful in several settings such as  compliers to generate code  can verify if the expression is well formed.

Data Structures : Project 5 Example Expression Tree See the example to the right. The operands are 22, 5, 10, 6, and 3. These are also leaf nodes.

Data Structures : Project 5 Questions wrt an Expression Tree How to evaluate an expression tree?  Meaning, how to apply the operators to the right operands. How to build an expression tree?  Given an expression, how to build an equivalent expression tree?

Data Structures : Project 5 A Few Observations Notice that an inorder traversal of the expression tree gives an expression in the infix notation.  The above tree is equivalent to the expression ((22 + 5) × (−10)) + (6/3)‏ What does a postorder and preorder traversal of the tree give?  Answer: ??

Data Structures : Project 5 How to Evaluate using an Expression Tree Notice that to evaluate a node, its left subtree and its right subtree need to be operands. For this, may have to evaluate these subtrees first, if they are not operands. Essentially, have to evaluate the root. So, Evaluate(root) should be equivalent to: –Evaluate the left subtree –Evaluate the right subtree –Apply the operator at the root to the operands.

Data Structures : Project 5 How to Evaluate using an Expression Tree This suggests a recursive procedure that has the above three steps. Recursion stops at a node if it is already an operand.

Data Structures : Project 5 How to Evaluate using an Expression Tree Example

Data Structures : Project 5 Example Contd...

Data Structures : Project 5 Pending Question How to build an expression tree? Start with an expression in the infix notation. Recall how we converted an infix expression to a postfix expression. The idea is that operators have to wait to be sent to the output.  A similar approach works now.

Data Structures : Project 5 Building an Expression Tree Let us start with a postfix expression. The question is how to link up operands as (sub)trees. As in the case of evaluating a postfix expression, have to remember operators seen so far.  need to see the correct operands. A stack helps again. But instead of evaluating subexpression, we have to grow them as trees.  Details follow.

Data Structures : Project 5 Building an Expression Tree When we see an operand :  That could be a leaf node...Or a tree with no children.  What is its parent?  Some operator.  In our case, operands can be trees also. The result of the expression tree could be an operand. The above observations suggest that operands should wait on the stack.  Wait as trees.

Data Structures : Project 5 Building an Expression Tree What about operators? Recall that in the postfix notation, the operands for an operator are available in the immediate preceding positions. Similar rules apply here too. So, pop two operands (trees) from the stack. Need not evaluate, but create a bigger (sub)tree.

Data Structures : Project 5 Building an Expression Tree Procedure ExpressionTree(E)‏ //E is an expression in postfix notation. begin for i=1 to |E| do if E[i] is an operand then create a tree with the operand as the only node; add it to the stack else if E[i] is an operator then pop two trees from the stack create a new tree with E[i] as the root and the two trees popped as its children; push the tree to the stack end-for end

Data Structures : Project 5 Example Consider the expression The postfix of the expression is a b + f − c d × e + / Let us follow the above algorithm.

Data Structures : Project 5 Example Stack b a + f − c d × e + /

Data Structures : Project 5 Example Stack b a + f − c d × e + /

Data Structures : Project 5 Example Stack b a + − c d × e + / f

Data Structures : Project 5 Example Stack b a + c d × e + / f -

Data Structures : Project 5 Example Stack b a + × e + / f - c d

Data Structures : Project 5 Example Stack b a + e + / f - c d +

Data Structures : Project 5 Example Stack b a + + / f - c d + e

Data Structures : Project 5 Example Stack b a + / f - c d + e +

Data Structures : Project 5 Example Stack b a + f / c d + e + -

Data Structures : Project 5 Generating Code Given an expression tree, how to generate code? What is the code for a tree with just three nodes, a parent and its two children?  OP value1, value2 So, extend this idea.  For nodes that are one level above the leaf nodes, it is easy.  For other internal nodes, wait till their children are leaves.  Few complications Have to wait till both values are known. Where to keep the temporary results?

Data Structures : Project 5 Generating Code Temporary values are typically in registers. But registers are limited in number. So, sometimes, temporary values are copied to memory from registers. Assume enough registers and generate code.

Data Structures : Project 5 Our Next Operation To print the nodes in a (binary) tree This is also called as a traversal. Need a systematic approach  ensure that every node is indeed printed  and printed only once.

Data Structures : Project 5 Tree Traversal Several methods possible. Attempt a categorization. Consider a tree with a root D and L, R being its left and right sub-trees respectively. Should we intersperse elements of L and R during the traversal?  OK – one kind of traversal.  No. -- One kind of traversal.  Let us study the latter first.

Data Structures : Project 5 Tree Traversal When items in L and R should not be interspersed, there are six ways to traverse the tree.  D L R  D R L  R D L  R L D  L D R  L R D

Data Structures : Project 5 Tree Traversal Of these, let us make a convention that R cannot precede L in any traversal. We are left with three:  L R D  L D R  D L R We will study each of the three. Each has its own name.

Data Structures : Project 5 The Inorder Traversal (LDR) The traversal that first completes L, then prints D, and then traverses R. To traverse L, use the same order.  First the left subtree of L, then the root of L, and then the right subtree of R.

Data Structures : Project 5 The Inorder Traversal -- Example Start from the root node A. We first should process the left subtree of A. Continuing further, we first should process the node E. Then come D and B. The L part of the traversal is thus E D B.

Data Structures : Project 5 The Inorder Traversal -- Example Then comes the root node A. We first next process the right subtree of A. Continuing further, we first should process the node C. Then come G and F. The R part of the traversal is thus C G F. Inorder: E D B A C G F

Data Structures : Project 5 The Inorder Traversal -- Example Procedure Inorder(T)‏ begin if T == NULL return; Inorder(T->left); print(T->data); Inorder(T->right); end Inorder: E D B A C G F

Data Structures : Project 5 The Preorder Traversal (DLR) The traversal that first completes D, then prints L, and then traverses R. To traverse L (or R), use the same order.  First the root of L, then left subtree of L, and then the right subtree of L.

Data Structures : Project 5 The Preorder Traversal -- Example Start from the root node A. We first should process the root node A. Continuing further, we should process the left subtree of A. This suggests that we should print B, D, and E in that order. The L part of the traversal is thus B D E.

Data Structures : Project 5 The Preorder Traversal -- Example We first next process the right subtree of A. Continuing further, we first should process the node C. Then come F and G in that order. The R part of the traversal is thus C F G. Preorder: A B D E C F G

Data Structures : Project 5 The Preorder Traversal – Example Procedure Preorder(T)‏ begin if T == NULL return; print(T->data); Preorder(T->left); Preorder(T->right); end Preorder: A B D E C F G

Data Structures : Project 5 The Postorder Traversal (LDR) The traversal that first completes L, then traverses R, and then prints D. To traverse L, use the same order.  First the left subtree of L, then the right subtree of R, and then the root of L.

Data Structures : Project 5 The Postorder Traversal -- Example Start from the root node A. We first should process the left subtree of A. Continuing further, we first should process the node E. Then come D and B. The L part of the traversal is thus E D B.

Data Structures : Project 5 The Postorder Traversal -- Example We next process the right subtree of A. Continuing further, we first should process the node C. Then come G and F. The R part of the traversal is thus G F C. Then comes the root node A. postorder: E D B G F C A

Data Structures : Project 5 The Postorder Traversal -- Example Procedure postorder(T)‏ begin if T == NULL return; Postorder(T->left); Postorder(T->right); print(T->data); end Inorder: E D B G F C A

Data Structures : Project 5 A Few Observations Notice that an inorder traversal of the expression tree gives an expression in the infix notation. What does a postorder and preorder traversal of the tree give?  Answer: ??

Data Structures : Project 5 Further Insights It is common knowledge that any program that uses recursion can also be written without using recursion. How to implement tree traversal without using recursion?  Consider inorder traversal  write a program for inorder traversal without using recursion.

Data Structures : Project 5 Further Insights Describing data structures uniquely.  How can you describe a binary tree uniquely?  One possibility is to give a complete level order description in an array. Too space consuming.  Would a single traversal suffice? Too ambiguous

Data Structures : Project 5 Further Insights Turns out that two traversals suffice so long as one of them is inorder. Claim: Given the inorder and preorder travesal of a binary tree, one can reconstruct the tree uniquely.  Same holds for also inorder and postorder traversal. Design an algorithm for the above claim.

Data Structures : Project 5 Innovation