|
This assignment is based on Section 7.3.6 (pages 298-300) of our textbook, and it is also a continuation of Programming Assignment 6.
In this assignment you will add four new static methods to your class Expression.java from Programming Assignment 6. The four new static methods are:
String postfix2Infix(String postfix)
BinaryTree<String> infix2BinaryTree(String infix)
int evaluateExpressionTree(BinaryTree<String> expTree, Position<String> v)
<T> String tree2String(Tree<T> tree)
Below is a description of each of these methods.
For the method infix2BinaryTree() , the input string holds an arithmetic expression in fully parenthesized infix notation. As in assignment 6, the arithmetic expression is made up of integers and six operators, + , - , * , / , % , and ^ (integer addition, subtraction, multiplication, division, remainder, and exponentiation). You can assume that the infix expression does not have any syntax errors in it, and you can assume that all of the tokens in the expression have whitespace around them (which makes it easy to use a StringTokenizer object to scan through the string). The algorithm for building the expression tree is given on page 298 of our textbook. You need to implement this algorithm using the framework of classes for Trees defined in our textbook. All of these classes are included in the zip file for this assignment.
The method infix2BinaryTree() requires fully parenthesized infix expressions, but these are tedious to write. Here is a way to convert regular infix expressions into fully parenthesized infix expressions. First, convert the original infix expression into a postfix expression using the method Infix2Postfix() from assignment 6. Then use the postfix2Infix() method that converts the postfix expression to a fully parenthesized infix expression. Write this method by modelling it after the EvaluatePostfix() method from assignment 6. That is, instead of using a stack of integer operands, use a stack of string operands, and instead of pushing the (integer) result of, say, x + y, onto the stack, push the string that x + y represents (with parentheses around it) onto the stack.
The algorithm for the
int evaluateExpressionTree(BinaryTree<String> expTree, Position<String> v) is given on page 300 of our textbook. You need to implement this algorithm using the textbook's framework of classes for Trees. The input to this method should be the tree created by the method infix2BinaryTree() described above.
To get a visual representation of the expression trees created by infix2BinaryTree() , write a method
public static <T> String tree2String(Tree<T> tree) . This method should draw an indented tree representation, similar to the directory trees drawn by the command line utility Tree for Windows (which is included in the zip file for this assignment). This method will need a recursive helper method
public static <T> String tree2StringHelper(Tree<T> tree, Position<T> v, String indentation)
If a tree node is an external node, the helper function should just return the node's string representation. If the node is an internal node, the helper function should concatenate the string representation of the node with the results from calling itself recursively on the child nodes (as in an preorder traversal of the tree, see pages 277-278 of the textbook). This helper method should increase the indentation by one space every time it calls itself recursively.
In the zip file along with this file there is a program TestExpressions.java that tests your implementations of the four new methods. Do not make any changes to the TestExpressions.java file. When TestExpressions.java runs, it should produce output like that contained in the file SampleOutput.txt .
Turn in a zip file called CS275Hw9Surname.zip containing your Java source file, Expression.java , and also all the original files contained in h9.zip . This assignment is due Friday, December 7.
|
|