This assignment is about expression trees, which are binary trees that represent arithmetic expressions. In this assignment you will see how variations on preorder, inorder, and postorder traversals can do useful work on expression trees. This assignment is due Thursday, February 2.
This assignment makes use of files contained in this zip file. In the zip file there are four source files, Every arithmetic expression can be represented by a binary tree called its "expression tree". For example, the expression "2+3" has the expression tree + / \ 2 3and the slightly more complex expression "2 + 3 * 4" has the expression tree + / \ 2 * <====> 2 + 3 * 4 / \ 3 4Notice how the operator with higher precedence appears lower in the tree. That represents the fact that you should evaluate that operator first. Now consider the slightly different expression "(2 + 3) * 4", in which the addition operator should be evaluated before the multiplication operator. This expression has the expression tree * / \ + 4 <====> (2 + 3) * 4 / \ 2 3Notice how the parentheses in the arithmetic expression help determine the structure of the expression tree, but the parentheses are not part of the expression tree. Besides having an expression tree, every arithmetic expression also has a "prefix notation" and a "postfix notation". In prefix notation, operators are written before their operands. In postfix notation, operators are written after their operands. (In regular "infix notation", operators are written between their operands.) For example, the expression "2+3" has the prefix notation "+ 2 3". The expression "2 + 3 * 4" has the prefix notation "+ 2 * 3 4". Notice that you read this as a plus operator with its first operand being 2 and its second operand being the prefix notation "* 3 4". As another example, the expression "(2 + 3) * 4" has prefix notation "* + 2 3 4". You read this as a multiplication operator with its first operand being the infix notation "+ 2 3", and its second operand being 4. The expression "2+3" has the postfix notation "2 3 +". The expression "2 + 3 * 4" has the postfix notation "2 3 4 * +". You read postfix notation from right to left, so you read this as a plus operator with its second operand being the postfix notation "3 4 *" and its first operand being 2. As another example, the expression "(2 + 3) * 4" has postfix notation "2 3 + 4 *". You read this as a multiplication operator with its second operand being 4 and its first operand being the postfix notation "2 3 +". (Note: Even though you read postfix notation from right to left, you evaluate it from left to right.) Prefix and postfix notations are hard for humans to read and write, but they are very useful for language processing programs, like compilers and interpreters. (One reason why they are so useful is that they never need parentheses.) The goal of this assignment is to see how versions of preorder, inorder, and postorder traversal of a binary tree can do useful work for us when the binary tree is an expression tree. In summary:
In the zip file there is a program
The
The
The * / \ + 4 / \ 2 3is "2 + 3 * 4" but the correct parenthesized infix notation is "(2 + 3) * 4". So the infixPrint() method needs to figure out where to put parentheses. It does this using information about the precedence level of the arithmetic operators. Each operator has an integer precedence level. If an operator in a left sub tree has a lower precedence level than the operator at the root, then that left sub tree gets wrapped in a pair of parentheses. If an operator in a right sub tree has a lower, or equal, precedence level than the operator at the root, then that right sub tree gets wrapped in a pair of parentheses. For example, what would be the parenthesized infix notation for this expression tree?
* / \ / \ + * / \ / \ 2 3 4 5In the file InfixPrinter.java there is a method precedence() that returns the precedence level of each operator. Use this method to help your infixPrint() method decide where to put in parentheses.
The prettyprinter for this assignment is slightly different from the ones in assignment 1. For example, consider this binary tree. (- (+ 1 (* 2 3 ) ) (/ (^ 4 5 ) 6 ) )The prettyprinter for this assignment should print this tree in the following compact form. As with the second prettyprinter in assignment 1, a sub tree of depth 1 should be printed on a single line. In addition, every sub tree should have it left child's root on the same line as the sub tree's root. Finally, closing parentheses should be placed at the end of a line. (This is the prettyprinting format used by languages like Lisp or Scheme.) (- (+ 1 (* 2 3)) (/ (^ 4 5) 6))
So you need to write one modified postorder traversal, one modified preorder traversal, and a modified inorder traversal. The easiest is the
Turn in a zip file called Here are some references that you may find useful.
This assignment is due Thursday, February 2. |