Help with a java program (binary tree)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cioccolatina
    New Member
    • Nov 2008
    • 2

    #1

    Help with a java program (binary tree)

    Hey guys, is there anyone who could help me..?

    I have file ExpressionBinar yTree.java :


    Code:
     
    /** 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;
        }
    }
    Also I have a supporting class (SearchTreeNode .java) for ExpressionBinar yTree and a test class ExpressionTest. java

    Code:
    ** 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);
        }
    }
    Batch file Go. bat compiles all source files and runs the test class:

    Code:
    javac ExpressionTest.java
    java ExpressionTest
    del *.class
    del foundations\*.class
    I am required to complete the details for class ExpressionBinar yTree. Additional statements may be added to class ExpressionTest to expand on the testing performed.

    Any ideas....?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by cioccolatina
    I am required to complete the details for class ExpressionBinar yTree. Additional statements may be added to class ExpressionTest to expand on the testing performed.

    Any ideas....?
    Yup, I'd say that additional statements have to be added to that class because
    as it is now it is an empty class, i.e. there are no useful things in it at all.

    You have to write an entire parser; an AST (Abstract Syntax Tree) and a few
    methods that transfer that AST to postfix, prefix and infix form. We won't do
    it for you but if you're stuck we're very willing to help so get going.

    kind regards,

    Jos

    Comment

    • cioccolatina
      New Member
      • Nov 2008
      • 2

      #3
      Thank You Jos!! :) Helped me a lot.

      Comment

      Working...