0

I'm trying to parse an arithmetic expression for a Binary Tree using stacks. So far I have:

package home2;

import chapter7.binaryTree.*;
import chapter7.tree.NonEmptyTreeException;
import chapter5.nodeStack.*;

public class Expression {
private NodeStack<LinkedBinaryTree<String>> stack = new NodeStack<>();
private String sym;
LinkedBinaryTree<String> expression;
LinkedBinaryTree<String> branch = new LinkedBinaryTree<>();
LinkedBinaryTree<String> branchtwo = new LinkedBinaryTree<>();

public LinkedBinaryTree<String> buildTree(String E) throws NonEmptyTreeException, EmptyStackException{
    
    
    for (int i = 0; i < E.length(); i++){
        sym = E.substring(i, i+1);
        
        
        if ( sym.equals(")"))  {
            try{
                branchtwo = stack.pop();
                expression = stack.pop();
                branch = stack.pop();
                expression.attach(expression.root(), branch, branchtwo);
                stack.push(expression);
            }catch ( EmptyStackException e){}
            
            
        }else{
            expression = new LinkedBinaryTree<>();
            expression.addRoot(sym);    
            stack.push(expression);
        }

    
    }// end for loop
    return stack.pop();
}// end method
}// end class

Right now my test outputs:

The Original String(((3+1)3)/((9-5)+2))-((3(7-4))+6)

Copied as Tree Structure[+, (, *, -, 7, 4, 6]

From running the debugger I found that my expression method splices the expression into 3 trees in the stack rather than just having one giant tree inside it. I'm guessing my problem starts there, but I'm unsure how to approach it. Any input is welcome.

Thanks.

Community
  • 1
  • 1
Snowyace
  • 1
  • 1
  • 1
  • 1
    Hi. Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Apr 03 '14 at 06:36
  • That's what I did from what I said at the bottom and I couldn't pinpoint anything so I came here. – Snowyace Apr 03 '14 at 06:41
  • 1
    You must have an expectation of how this should work; i.e. you should be able to run the algorithm manually with paper and pencil. In which case, simply compare the behaviour of the real code with your paper-and-pencil version. – Oliver Charlesworth Apr 03 '14 at 06:44
  • Yes I agree Oli Charlesworth. – Snowyace Apr 03 '14 at 06:55
  • I found part of a problem. I'm able to successfully store and print the tree until it hits the characters starting at -((3*(7-4))+6)"; It completely separates the expression into two different trees at that point and stores them in the stack rather than having one giant expression in the stack. – Snowyace Apr 03 '14 at 07:43
  • Hah. What an overlooked mistake on my part. I missed a parenthesis in my expression which caused it to skip the stack. – Snowyace Apr 03 '14 at 07:49

0 Answers0