2010-10-20 62 views
0

所以我試圖輸入一個後綴表達式存儲在隊列中的表達式樹 當我發送隊列到我的構建(QueueLi後綴)方法,它工作正常elseif(isOperator(token))語句接受操作符,並將堆棧中的兩個操作數連接到堆棧,並在每次操作後嘗試打印元素,直到while循環後, ',並設置它等於(stack.topAndPop)。當我嘗試打印出root,root.left和root.right的元素時,它會打印出'+'像它應該但它會給我一個空指針異常打印出12和13.我在哪裏出錯了,我該如何修復它?感謝您的幫助我的表達式樹沒有正確存儲對象:(

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 

編輯 很抱歉,這是我第一次發佈這樣的問題 所以我用非標準節點,棧和隊列,所以這裏是該代碼以及方法leftChild()rightChild(我的道歉長碼)

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--

類ListNode { ListNode(對象theElement){ 此(theElement,NULL); }

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

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

}

Stack--

公共類StackLi { /** * 構建堆疊。 */ 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--

公共類QueueLi { /** * 構建隊列。 */ 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; 
+0

該錯誤可能是由於'新節點(stack.topAndPop())'。這個構造函數不應該是必需的,因爲堆棧中的所有東西都已經是'Node'了,對吧?不幸的是,很難重現錯誤,因爲你使用的是非標準的隊列和堆棧,我不知道你的'Node'類是什麼樣的。 – 2010-10-20 07:23:38

回答

0

你可能想分享類節點和方法的實現leftChild和rightChild因爲事情音符從這個代碼清晰。

相關問題