I have file ExpressionBinaryTree.java :
Expand|Select|Wrap|Line Numbers
- /** class ExpressionBinaryTree
- * uses a binary tree to represent binary expressions
- * does not implement BinaryTree - all iterators return String
- */
- package foundations;
- import java.util.*;
- public class ExpressionBinaryTreeE implements Container {
- // Fields
- private SearchTreeNode root = null;
- private int size = 0;
- private String postfixString;
- // Constructors
- /** Create an empty expression tree
- */
- public ExpressionBinaryTreeE () {
- }
- /** Create and initialize an expression tree on infix
- */
- public ExpressionBinaryTreeE (String infix) {
- postfixString = (new FunctionEvaluation(infix)).postfix();
- buildExpressionTree();
- }
- // Commands
- /** Set a new value for infix
- * updates postfixString and rebuilds expression tree
- */
- public void setInfixString (String infix) {
- postfixString = (new FunctionEvaluation(infix)).postfix();
- buildExpressionTree();
- }
- /** Set a new value for postfixString
- */
- public void setPostfixString (String postfix) {
- // remove blanks then build expression tree
- // left as an exercise
- }
- /** Remove all objects from the container if found
- */
- public void makeEmpty () {
- root = null;
- }
- // Queries
- /** Return a reference to the root
- */
- public SearchTreeNode root () {
- return root;
- }
- /** Return true if the container is empty
- */
- public boolean isEmpty () {
- return root == null;
- }
- /** Return the number of objects in the container
- * postfixString has been trimmed
- */
- public int size () {
- return size;
- }
- /** Return the infix string on elements in the tree
- */
- public String traverseInorder () {
- // left as an exercise
- return "";
- }
- /** return the prefix string on elements in the tree
- */
- public String traversePreorder () {
- // left as an exercise
- return "";
- }
- /** Return the postfix on elements in the tree
- */
- public String traversePostorder () {
- //left as an exercise
- return "";
- }
- // Internal methods
- /** Build an expression tree from postfixString
- * - use a Stack of SearchTreeNode
- * throw NoSuchElementException for caught Stack error
- */
- private void buildExpressionTree () {
- // left as an exercise
- }
- private boolean isOperand (char ch) {
- // left as an exercise
- return false;
- }
- private boolean isOperator (char ch) {
- //left as an exercise
- return false;
- }
- }
Expand|Select|Wrap|Line Numbers
- ** A test class for ExpressionBinaryTree (prefix input)
- */
- import foundations.*;
- import java.io.*;
- public class ExpressionTest{
- public static void main(String[] args) throws IOException {
- ExpressionBinaryTreeE expr = new ExpressionBinaryTreeE("a+b/(c-d)-e");
- if (!expr.isEmpty()){
- System.out.println("The number of terms in the tree is: " + expr.size());
- System.out.println("Equiv infix string is: " + expr.traverseInorder());
- System.out.println("Equiv prefix string is: " + expr.traversePreorder());
- System.out.println("Equiv postfix string is: " + expr.traversePostorder());
- }
- System.out.println("Test makeEmpty()");
- expr.makeEmpty();
- System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
- expr.setInfixString("a*b/(c-d)*e");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
- if (!expr.isEmpty()){
- System.out.println("The number of terms in the tree is: " + expr.size());
- System.out.println("Equiv infix string is: " + expr.traverseInorder());
- System.out.println("Equiv prefix string is: " + expr.traversePreorder());
- System.out.println("Equiv postfix string is: " + expr.traversePostorder());
- }
- System.out.println("Test makeEmpty()");
- expr.makeEmpty();
- System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
- expr.setInfixString("a+(b-c)-d/e");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
- if (!expr.isEmpty()){
- System.out.println("The number of terms in the tree is: " + expr.size());
- System.out.println("Equiv infix string is: " + expr.traverseInorder());
- System.out.println("Equiv prefix string is: " + expr.traversePreorder());
- System.out.println("Equiv postfix string is: " + expr.traversePostorder());
- }
- System.out.println("Test makeEmpty()");
- expr.makeEmpty();
- System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
- expr.setInfixString("a*b-(c-d*e)+f/(g-h)");//"a*b-(c-d*e)+f/(g-h)" or "a+b-(c-d*e)"
- if (!expr.isEmpty()){
- System.out.println("The number of terms in the tree is: " + expr.size());
- System.out.println("Equiv infix string is: " + expr.traverseInorder());
- System.out.println("Equiv prefix string is: " + expr.traversePreorder());
- System.out.println("Equiv postfix string is: " + expr.traversePostorder());
- }
- System.out.println("Test makeEmpty()");
- expr.makeEmpty();
- System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
- BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
- String postfix = "";
- do {
- System.out.print("Enter a postfix string: ");
- postfix = keyboard.readLine();
- expr.setPostfixString(postfix);
- if (!expr.isEmpty()){
- System.out.println("The number of terms in the tree is: " + expr.size());
- System.out.println("Equiv infix string is: " + expr.traverseInorder());
- System.out.println("Equiv prefix string is: " + expr.traversePreorder());
- System.out.println("Equiv postfix string is: " + expr.traversePostorder());
- }
- System.out.println("Test makeEmpty()");
- expr.makeEmpty();
- System.out.println("The number of terms in the tree is: " + expr.size() + "\n");
- } while (postfix.length() >= 3);
- }
- }
Expand|Select|Wrap|Line Numbers
- javac ExpressionTest.java
- java ExpressionTest
- del *.class
- del foundations\*.class
Any ideas....?