Podcast Ch17a Title: Expression Trees

Slides:



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

Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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
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.
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 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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 17 Binary.
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.
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.
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.
1 EXPRESSION TREES In this lecture we will discuss Expression trees as a method for storing & evaluating mathematical expressions.
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.
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.
Tree Data Structures.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
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.
Podcast Ch17b Title: Iterative Tree Traversal
Revised based on textbook author’s notes.
Stacks Chapter 7 introduces the stack data type.
Podcast Ch17d Title: Drawing a Binary Tree
PART II STACK APPLICATIONS
Binary Tree Application Expression Tree
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Binary Tree Traversal Methods
Paul Tymann, Andrew Watkins,
Podcast Ch18b Title: STree Class
Binary Tree Traversal Methods
COMPUTER 2430 Object Oriented Programming and Data Structures I
Section 9.3 by Andrew Watkins
Podcast Ch22c Title: Deleting from a Heap
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Paul Tymann, Andrew Watkins,
Podcast Ch18c Title: BST delete operation
CSC 143 Java Applications of Trees.
BINARY TREE CSC248 – Data Structure.
Podcast Ch22b Title: Inserting into a Heap
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Podcast Ch18a Title: Overview of Binary Search Trees
Chapter 20: Binary Trees.
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Binary Tree Traversal.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Podcast Ch17a Title: Expression Trees Description: Building an expression tree; buildExpTree method; Program 17.1 Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Expression Trees A binary expression tree represents an arithmetic expression. In an expression tree each operator is an interior node whose children are operands or subexpressions. Operands are in leaf nodes.

Student Question Draw the expression tree for the following a b + c - d * e / f % a b c d e f + - * / % a b c + - d e f * / %

Practice Problem Draw the expression trees for the following a b + c - d * e f / % a b c + - d e * / f %

Expression Trees (continued) The preorder and postorder traversals of a binary expression tree produce the prefix and postfix (RPN) notation for the expression. An inorder traversal generates the infix form of the expression, assuming that parentheses are not needed to determine the order of evaluation; the traversal can be modified to insert full parenthesization

Expression Trees (continued) Preorder(Prefix): - + a / * b c d e Inorder(Infix): a + b * c / d - e Postorder(Postfix): a b c * d / + e -

Student Question The infix expression generated by an inorder traversal will often be incorrect due to parentheses in the original expression We can fix this by having the traversal insert one set of parentheses for each operand How would you modify the traversal to do this?

Practice Coding Write a method, call it fullParenInfix that scans an expression tree and returns as a string the fully parenthesized infix expression The previous discussion by John and Tobie should get you started

Building a Binary Expression Tree Build an expression tree from a postfix expression using an iterative algorithm. An operand is a single character such as 'a' or 'b'. If the token is an operand, create a leaf node whose value is the operand and whose left and right subtrees are null. Push the node onto a stack of TNode references.

Building a Binary Expression Tree (continued) If the token is an operator, create a new node with the operator as its value. Pop the two child nodes from the stack and attach them to the new node. The first child popped from the stack becomes the right subtree of the new node and the second child popped from the stack becomes the left subtree.

Building a Binary Expression Tree (continued) a b c * +

Student Question Show the stack and the partial expression trees, step by step, for the expression a b c - d e / + *

Practice Problem Show the stack and the partial expression trees, step by step, for the expression a b - c d / e + *

buildExpTree() public static TNode<Character> buildExpTree(String postfixExp){ // newNode is a reference to the root of subtrees // we build, and newLeft/newRight are its children TNode<Character> newNode, newLeft, newRight; char token; // subtrees go into and off the stack ALStack<TNode<Character>> s = new ALStack<TNode<Character>>(); int i = 0, n = postfixExp.length(); // loop until i reaches the end of the string while(i != n) { // skip blanks and tabs in the expression while (postfixExp.charAt(i) == ' ' || postfixExp.charAt(i) == '\t') i++;

buildExpTree() (cont) // if the expression has trailing whitespace, // we could be at the end of the string if (i == n) break; // extract the current token and increment i token = postfixExp.charAt(i); i++; // see if the token is an operator or operand if (token == '+' || token == '-' || token == '*' || token == '/') { // current token is an operator; pop two // subtrees off the stack newRight = s.pop(); newLeft = s.pop();

buildExpTree() (continued) // create a new subtree with token as root and // subtrees newLeft and newRight and push it // onto the stack newNode = new TNode<Character>(token, newLeft,newRight); s.push(newNode); } else // must be an operand { // create a leaf node and push it onto the stack newNode = new TNode<Character>(token);

buildExpTree() (concluded) // if the expression was not empty, the root of // the expression tree is on the top of the stack if (!s.isEmpty()) return s.pop(); else return null; }

Run of Program 17.1

Program 17.1 (continued) public void actionPerformed(ActionEvent ae) { // obtain the expression the user typed String expression = expInput.getText(); // build the expression tree TNode<Character> root = BinaryTree.buildExpTree(expression); // output the expression and its tree textArea.append("Expression tree for " + expression + "\n\n"); textArea.append(BinaryTree.displayTree(root, 1) + "\n");

Program 17.1 (concluded) // output the scans textArea.append("Preorder scan: " + BinaryTree.preorderDisplay(root) + "\n\n"); textArea.append("Inorder scan: " + BinaryTree.inorderDisplay(root) + "\n\n"); textArea.append("Postorder scan: " + BinaryTree.postorderDisplay(root) + "\n\n"); // clear the text field expInput.setText(""); }

Student Question Using the normal rules for infix arithmetic, did the inorder scan produce the correct infix expression? If you used the fully parenthesized scan, what expression would have been printed?