CS2134_HW6

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

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

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.

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

View Full Document
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:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern