所以我试图将存储在队列中的后缀表达式输入到表达式树中。 当我将队列发送到我的构建(QueueLi postfix)方法时,它工作正常,直到elseif(isOperator(token))语句接受操作符并从堆栈附加两个操作数并将其推入堆栈。我尝试在每次操作后打印元素,并且它工作正常,直到while循环,我接受变量'root'并将其设置为等于(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);
}
节点 -
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;
}
栈 -
public class 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;
队列 -
公共类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 :(得分:0)
您可能希望共享类Node和leftChild和rightChild方法的实现,因为这段代码中的内容很清楚。