views:

29

answers:

1

So Im trying to input a postfix expression that is stored in a queue into an expression tree. When i send the queue to my build(QueueLi postfix) method, it works fine until the elseif(isOperator(token)) statement takes the operator and attaches the two operands from the stack and pushes it into the stack. I tried printing the element after each operation and it works fine until after the while Loop where I take variable 'root' and set it equal to (stack.topAndPop). When i try to print out the elements of root, root.left, and root.right, it prints out '+' like it should but then gives me a null pointer exception instead of printing out 12 and 13. Where am i going wrong and how can I fix it? Thanks for your help

public void build(QueueLi x){
    StackLi stack= new StackLi();
    Node n, n1, n2, n3;

    while(!x.isEmpty()){
        char token=((String) x.getFront()).charAt(0);
        if (isOperand(token)){
            n= new Node(x.dequeue());
            stack.push(n);
        }
        else if( isOperator(token)){
            n1= new Node(stack.topAndPop());
            n2= new Node(stack.topAndPop());
            n3= new Node(x.dequeue());

                    leftChild(n3, n2);//adds leftChild(n2(operand)) to n3(operator)
        rightChild(n3, n1);//adds rightChild(n1(operand)) to n3(operator)

                    //**if i print elements of n3, n3.left, n3.right here, it works

            stack.push(n3);//i think this is where the problem is?
        }//end else if statement

    }//end while

    root= new Node(stack.topAndPop());//when i print out the elements in root, 
                                              //it does not print the operands, gives a
                                              // null pointer exception

}//end build

EDIT Sorry this is my first time posting such questions So i AM using nonstandard Nodes, Stacks and Queues, so here are the codes for that as well as the methods leftChild() rightChild(I apologize for the long code)

private  Node root;

public TreeClass(){
    root = null;
}

private void leftChild(Node t, Node o){
//create a left child for node t
    t.left = new Node(o);
}

private void rightChild(Node t, Node o){
//create a right child for node t
    t.right = new Node(o);
}

Node--

class ListNode{ ListNode( Object theElement ){ this( theElement, null ); }

ListNode( Object theElement, ListNode n ){
    element = theElement;
    next    = n;
}

    // Friendly data; accessible by other package routines
Object   element;
ListNode next;

}

Stack--

public class StackLi{ /** * Construct the stack. */ public StackLi( ) { topOfStack = null; }

/**
 * Test if the stack is logically full.
 * @return false always, in this implementation.
 */
public boolean isFull( )    {
    return false;
}

/**
 * Test if the stack is logically empty.
 * @return true if empty, false otherwise.
 */
public boolean isEmpty( )   {
    return topOfStack == null;
}

/**
 * Make the stack logically empty.
 */
public void makeEmpty( )    {
    topOfStack = null;
}

/**
 * Get the most recently inserted item in the stack.
 * Does not alter the stack.
 * @return the most recently inserted item in the stack, or null, if empty.
 */
public Object top( )    {
    if( isEmpty( ) )
        return null;
    return topOfStack.element;
}

/**
 * Remove the most recently inserted item from the stack.
 * @exception Underflow if the stack is empty.
 */
public void pop( ) throws Underflow {
    if( isEmpty( ) )
        throw new Underflow( );
    topOfStack = topOfStack.next;
}

/**
 * Return and remove the most recently inserted item from the stack.
 * @return the most recently inserted item in the stack, or null, if empty.
 */
public Object topAndPop( )  {
    if( isEmpty( ) )
        return null;

    Object topItem = topOfStack.element;
    topOfStack = topOfStack.next;
    return topItem;
}

/**
 * Insert a new item into the stack.
 * @param x the item to insert.
 */
public void push( Object x )    {
    topOfStack = new ListNode( x, topOfStack );
}

private ListNode topOfStack;

Queue--

public class QueueLi { /** * Construct the queue. */ public QueueLi( ){ makeEmpty(); }

/**
 * Test if the queue is logically empty.
 * @return true if empty, false otherwise.
 */
public boolean isEmpty( ){
    return front == null;
}

/**
 * Test if the queue is logically full.
 * @return true if full, false otherwise.
 */
public boolean isFull( ){
    return false;
}


/**
 * Make the queue logically empty.
 */
public void makeEmpty( ){
    front = null;
    back  = null;
}

/**
 * Get the least recently inserted item in the queue.
 * Does not alter the queue.
 * @return the least recently inserted item in the queue, or null, if empty.
 */
public Object getFront( ){
    if( isEmpty( ) )
        return null;
    return front.element;
}

/**
 * Return and remove the least recently inserted item from the queue.
 * @return the least recently inserted item in the queue, or null, if empty.
 */
public Object dequeue( ){
    if( isEmpty( ) )
        return null;

    Object frontItem = front.element;
    front = front.next;
    if (front == null)
        back = null;
    return frontItem;
}
/**
 * Insert a new item into the queue.
 * @param x the item to insert.
 * @exception Overflow if queue is full.
 */
public void enqueue( Object x ) {
    ListNode newNode = new ListNode (x);
    if (isEmpty ())
        front = newNode;
    else
        back.next = newNode;
    back = newNode; 
}
private ListNode    front;
private ListNode    back;
A: 

You might want to share the implementations of class Node and methods leftChild and rightChild since things are note clear from this code.

roshang