Podcast Ch 17 a Title Expression Trees Description





















- Slides: 21
Podcast Ch 17 a • Title: Expression Trees • Description: Building an expression tree; build. Exp. Tree 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 full. Paren. Infix 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 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+ *
build. Exp. Tree() public static TNode<Character> build. Exp. Tree(String postfix. Exp){ // new. Node is a reference to the root of subtrees // we build, and new. Left/new. Right are its children TNode<Character> new. Node, new. Left, new. Right; char token; // subtrees go into and off the stack ALStack<TNode<Character>> s = new ALStack<TNode<Character>>(); int i = 0, n = postfix. Exp. length(); // loop until i reaches the end of while(i != n) { // skip blanks and tabs in the while (postfix. Exp. char. At(i) == i++; the string expression ' ' || 't')
build. Exp. Tree() (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 = postfix. Exp. char. At(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 new. Right = s. pop(); new. Left = s. pop();
build. Exp. Tree() (continued) // create a new subtree with token as root and // subtrees new. Left and new. Right and push it // onto the stack new. Node = new TNode<Character>(token, new. Left, new. Right); s. push(new. Node); } else // must be an operand { // create a leaf node and push it onto the stack new. Node = new TNode<Character>(token); s. push(new. Node); } }
build. Exp. Tree() (concluded) // if the expression was not empty, the root of // the expression tree is on the top of the stack if (!s. is. Empty()) return s. pop(); else return null; }
Run of Program 17. 1
Program 17. 1 (continued) public void action. Performed(Action. Event ae) { // obtain the expression the user typed String expression = exp. Input. get. Text(); // build the expression tree TNode<Character> root = Binary. Tree. build. Exp. Tree(expression); // output the expression and its tree text. Area. append("Expression tree for " + expression + "nn"); text. Area. append(Binary. Tree. display. Tree(root, 1) + "n");
Program 17. 1 (concluded) // output the scans text. Area. append("Preorder scan: " + Binary. Tree. preorder. Display(root) + "nn"); text. Area. append("Inorder scan: " + Binary. Tree. inorder. Display(root) + "nn"); text. Area. append("Postorder scan: " + Binary. Tree. postorder. Display(root) + "nn"); // clear the text field exp. Input. set. Text(""); }
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?





















