
ITK 179
Fall, 2005
Califf
Programming Assignment 8
Binary Expression Trees
Program Due Date: Wednesday, November 30, 2005 at the beginning of class.
Progress due Friday, November 18 (worth 10 programming points): Turn in a version of
your program able to read a postfix expression, store it in the tree, and print it out in prefix
notation. Submit your zipped .java files to WebCT. Name the .zip file Prog8a.zip.
Objective: Practice with binary tree implementation.
The Problem
Write a program that reads arithmetic expressions in postfix notation and creates a binary
expression tree. The program should then print the expression out in prefix and properly
parenthesized infix form. It should also evaluate the expression and print the result. You may
assume spaces between elements of the expression.
Allow the user to enter as many postfix expressions as he/she likes.
You may use a GUI or a text interface for this problem.
Sample input:
5 6 * 4 3 + -
Sample output:
Prefix: - * 5 6 + 4 3
Infix: ((5 * 6) โ (4 + 3))
Result: 18
Extra credit (10 points):
Handle expressions with variables. Read in the expression and store it in the tree. Write the
prefix and infix. Ask the user for values for the variables (only one time for each variable, even
if a variable appears more than once in the expression). Evaluate the expression and display the
result. Give the user an opportunity to enter new values and reevaluate as many time as desired.
Note:
If you choose to use a GUI, feel free to be truly event-driven.
Use good object-oriented design.