David Arce
David Arce

Reputation: 45

Building a Binary Tree

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?

Upvotes: 1

Views: 371

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18793

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.

Upvotes: 1

Related Questions