CS2134_HW6

CS2134_HW6 - Homework #6 CS2134 Fall 2007 Profs Frankl/...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Homework #6 CS2134 Fall 2007 Profs Frankl/ Hellerstein Programming Part Due: 11:59 p.m. Fri Nov 16 (Written Part: to be posted soon. It will be due a few days earlier, so we can go over it in class before the midterm.) NOTE: For the programming part and the binary tree programming exercises in the written part, you may either use the Book’s binary tree implementation or may use a simple Node class like Struct BinaryTreeNode { Object element; BinaryTreeNode *left, *right; } An expression tree is a binary tree in which leaf nodes store operands and internal nodes store operators. For example, the expression tree * / \ x + / \ 4 5 represents the infix expression x * (4 + 5). In this assignment, you’ll write a program that reads in a postfix expression and builds an expression tree that represents it. The program will then traverse the tree to output the expression in different formats. Finally, the program will query the user for values for variables (identifiers) appearing in the expression, evaluate the expression, and output the value.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1. Building the tree: The algorithm for building the tree is similar to the algorithm for evaluating postfix expressions, which we studied, and similar to the algorithm used in HW#5 to convert postfix to fully-parenthesized infix. However, instead of maintaining a stack of numbers (representing the values of subexpressions) or a stack of strings (representing infix subexpressions), the program will maintain a stack of pointers to BinaryTreeNodes representing roots of subtrees of the expression tree. As before, scan the input from left to right, examining each token and taking appropriate actions with the stack. In this case, the possibilities are: ± Operand token: create a new node to hold the operand and push a pointer to this node onto the stack. This node will eventually become a leaf in the tree. ± Operator token: create a new node to hold the operator token. Pop pointers to the second operand’s subtree and the first operand’s subtree from the stack and “attach” them to the new node by setting the left and right fields of the new nodes to point to these subtrees. 2. After the tree has been built, traverse it in two different ways in order to output the expression in two different formats. * Postfix: do post-order traversal of the tree. (Use the one from the textbook or write your own.) * Fully-parenthesized infix: write a recursive function that slightly modifies in-order traversal so that it outputs opening parenthesis before traversing a subtree and closing parentheses after traversing each subtree. (To avoid parentheses around single operands, you can check whether the subtree is a leaf before adding parens. We won’t take off points if you include parentheses around each operand.) 3. Evaluate the expression: ± Write a recursive function int eval(BinaryTreeNode *r) that takes a pointer to a subtree and returns the value of the subexpression represented by that subtree. For example, the value of the subtree rooted at the + sign in the above
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2008 for the course CS 2134 taught by Professor Hellerstein during the Fall '07 term at NYU Poly.

Page1 / 8

CS2134_HW6 - Homework #6 CS2134 Fall 2007 Profs Frankl/...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online