1

I build an expression into a binary tree each time it rolls through the loop, creating a new tree at each ending of ")" and pushing those operators/operands into a stack to be popped back into one complete binary tree.

My Build Method:

package lab5;

import net.datastructures.*;

public class Expression<T> {

/** Contain Linked Tree and Linked Stack instance variables **/
LinkedBinaryTree<T> tree;
LinkedStack<LinkedBinaryTree<T>> stack;


public Expression () {
    tree = new LinkedBinaryTree<T> ();
    stack = new LinkedStack<LinkedBinaryTree<T>>();

} // end constructor

public LinkedBinaryTree<T> buildExpression (String expression) {// LinkedBinaryTree<T> is a type of LinkedBinaryTree
    // major TODO to implement the algorithm]
    LinkedBinaryTree<T> operand, op1, op2;
    LinkedStack<LinkedBinaryTree<T>> newStack = new LinkedStack<LinkedBinaryTree<T>>();
    String symbol;

    int i = 0;
    int len = expression.length();

    for (i = 0; i < len; i++) {
        symbol = expression.substring(i, i+1);

        if ((!symbol.equals ("(")) && (!symbol.equals (")"))) {
            operand = new LinkedBinaryTree<T> ();
            operand.addRoot((T)symbol);
            newStack.push(operand);
        } else if (symbol.equals ("(")){
            continue;
        } else {
            op2 = newStack.pop();
            operand = newStack.pop();
            op1 = newStack.pop();
        tree.attach(operand.root(), op1, op2);
        newStack.push(tree);
        }
    }
    tree = newStack.pop();
    return tree;

}  // end method buildExpression

}

My Test:

package lab5;
import net.datastructures.*;

public class ExpressionTest {

/**
 * @param args
 * @throws EmptyTreeException
 */

/** Paranthesize is code fragment 8.26 pg. 346 **/

/** evaluateExpression method apart of LinkedBinaryTree class in net.datastructures **/

public static void main(String[] args) {
    // declare local variables/objects
            String s = new String ();
            String exp = "((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))"; //-13
            Expression<String> expression = new Expression<String> ();

            // print the expression string
            System.out.printf ("The original Expression String generated via printf: %s", exp);

            // create the tree using the 'stub' method
            // i.e. it does nothing
            LinkedBinaryTree<String> tree = expression.buildExpression (exp);

            // use Object.toString simply to print its reference
            System.out.printf ("\n\nThe Tree: %s\n", tree);

    }

}

I am getting a NullPointerException and I do not know why. Do I need to 'Try' and then let the error roll through?

David Arce
  • 45
  • 1
  • 6
  • You should use a debugger to check where and why you have the error. – ekeith Apr 09 '16 at 22:12
  • It's suspending the run of the app due to the root of the tree. It stops after the t1.root.setParent(node) in a LinkedBinaryTree.class I am using to grab my LinkedBinaryTree object initialization from. This is apart of a net.datastructures that was provided by the author of the book im reading. In the book it states if I use addRoot(element), an error will occur if the tree is not empty. – David Arce Apr 10 '16 at 20:18
  • are you setting conditions to catch that error? if you aren't you could say this in the addRoot(element) method `if(root == null){ //addRootElement;} else{ //return an error message indicating there is already a root` – ekeith Apr 10 '16 at 22:29
  • No I am not. So, should I catch the error in my code to just let it roll through or handle it. I have no idea how to handle it haha. – David Arce Apr 10 '16 at 22:30
  • an error would ocurr if the root is not empty because addRoot(element) is trying to add a root where there is already an element in the root, you should handle it to make sure your code doesn't crash – ekeith Apr 10 '16 at 22:33
  • How do I handle it? I really have no idea what to do to handle that. – David Arce Apr 10 '16 at 22:39
  • I just answered that ^^^ up but if it helps you can post your whole code which would also include the addRoot method – ekeith Apr 10 '16 at 22:43
  • Thanks for your help as well. – David Arce Apr 11 '16 at 02:52

1 Answers1

1

The error probably is related to checking for '(' (single quotes) instead of "(" (double quotes) in symbol.equals ('('). You compare a string to a character, which will never be equal.

Maybe also stack is not initialized? It should be local to buildExpression().

Note that your code snippet won't compile:

  • stack is not defined
  • symbol is not defined

BTW: buildExpression() could be static, that would avoid the empty unused expression allocation in main.

Checking for "(" first would avoid checking for "(" twice.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
  • Sorry I took so long to respond. Okay so I updated my buildExpression code above. I am still getting the same error NullPointerException. Here is the error: `The original Expression String generated via printf: ((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))Exception in thread "main" java.lang.NullPointerException` – David Arce Apr 10 '16 at 17:54
  • What is the stack trace printed for the expression? – Stefan Haustein Apr 10 '16 at 20:40
  • I don't know what you mean. – David Arce Apr 10 '16 at 20:45
  • Sorry, meant th stack trace fro the null pointer exception (expression -> exception): http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Stefan Haustein Apr 10 '16 at 20:50
  • `at net.datastructures.LinkedBinaryTree.attach(LinkedBinaryTree.java:251) at lab5.Expression.buildExpression(Expression.java:40) at lab5.ExpressionTest.main(ExpressionTest.java:47) ` – David Arce Apr 10 '16 at 21:05
  • Well `tree` in `buildExpression` isn't declared anywhere... I'd guess that the line where the children are attached should be something like tree = operand.attach(op1, op2), but it's hard to tell without seeing the LinkedBinaryTree source... – Stefan Haustein Apr 10 '16 at 21:37
  • Hold on...let me add my whole class. It will be updated above. – David Arce Apr 10 '16 at 21:43
  • newStack.push(operand) instead of newStack.push(tree)? The LinkedBinaryTree API looks quite convoluted because it tries to distinguishes between a tree as a whole and the tree nodes... – Stefan Haustein Apr 10 '16 at 22:35
  • so operand is going to be an individual tree is made each time through the loop where you push onto a stack and then pop off the stack into one final, complete tree. – David Arce Apr 10 '16 at 22:39
  • Did you try `newStack.push(operand)` instead of `newStack.push(tree)`? Does it solve the problem? – Stefan Haustein Apr 10 '16 at 22:46
  • Well attach makes op1 and op2 child nodes of the root node of operand, so that's what you need to push. They are also declared to be part of tree, but that's not really relevant -- tree is later overwritten anyway (In the real world, a binary tree looks more like this: https://gist.github.com/stefanhaustein/22277face1566ae7e792912da2b6d8eb ) – Stefan Haustein Apr 11 '16 at 00:12
  • Oh i understand what you mean. I get it I get it. Thanks a lot for your help Stefan!! :) – David Arce Apr 11 '16 at 02:52