Java 我的表达式树未正确存储对象:(
因此,我尝试将存储在队列中的后缀表达式输入表达式树。 当我将队列发送到我的build(QueueLi postfix)方法时,它工作正常,直到elseif(isOperator(token))语句从堆栈中获取运算符并附加两个操作数,并将其推入堆栈。我尝试在每次操作后打印元素,它工作正常,直到while循环之后,我在该循环中获取变量“root”,并将其设置为(stack.topAndPop)。当我尝试打印root、root.left和root.right元素时,它会像应该的那样打印出“+”,但随后会给我一个空指针异常,而不是打印出12和13。我哪里出错了?我如何修复它?感谢您的帮助Java 我的表达式树未正确存储对象:(,java,expression-trees,Java,Expression Trees,因此,我尝试将存储在队列中的后缀表达式输入表达式树。 当我将队列发送到我的build(QueueLi postfix)方法时,它工作正常,直到elseif(isOperator(token))语句从堆栈中获取运算符并附加两个操作数,并将其推入堆栈。我尝试在每次操作后打印元素,它工作正常,直到while循环之后,我在该循环中获取变量“root”,并将其设置为(stack.topAndPop)。当我尝试打印root、root.left和root.right元素时,它会像应该的那样打印出“+”,但随后
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方法(很抱歉代码太长)
节点--
类ListNode{
ListNode(对象元素){
此(元素,空);
}
}
堆叠--
公共类StackLi{
/**
*构造堆栈。
*/
公共图书馆(){
topOfStack=null;
}
排队--
公共类排队
{
/**
*构造队列。
*/
公共队列li(){
makeEmpty();
}
您可能希望共享类Node和方法leftChild和rightChild的实现,因为这段代码中的注意事项很清楚。错误可能是由于
新节点(stack.topAndPop())
。这个构造函数应该是不必要的,因为堆栈上的所有内容都已经是节点了,对吗?不幸的是,很难重现错误,因为您使用的是非标准队列和堆栈,我不知道节点
类是什么样子。
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);
}
ListNode( Object theElement, ListNode n ){
element = theElement;
next = n;
}
// Friendly data; accessible by other package routines
Object element;
ListNode next;
/**
* 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;
/**
* 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;