Hi. I am working on a project where I convert postfix notation to infix and evaluateing it. This includes building an evaluations tree (binary tree). I get how to create nodes, but how do I get the whole tree?
This is the code I have so far:

import java.util.*;
import java.io.*;
import java.lang.*;

public class test {

	public static void main (String args[]) {		
		
        Deque<Object> stack = new ArrayDeque<Object>(); 
        String exsp = "2 2 * 3 + 11 8 - * 2 *";
        String[] operators = {"-","+","*","/"}; 
        StringTokenizer st = new StringTokenizer(exsp);         // deler udtrykket i tokens
        while (st.hasMoreTokens()) 
        {

            String T=st.nextToken();
                    
            for(int i = 0; i < 3; i++ )
            {
                if( T.equals( operators[i]))
                {   
                    Node n1 = new Node (stack.pop(),null,null);
                    Node n2 = new Node (stack.pop(),null,null);
                    Node p = new Node(T,n1,n2);
                    stack.push(T);
                    
                    break;
                }
                else if( Character.isDigit(T.charAt(0)) )           //tjekker kun det forste symbol.
                {
                    Node p = new Node(T, null, null);
                    stack.push(T);
                    
                    break;
                }
            }
        }
	}
}


class Node
{
    Object data;
    Node leftChild;
    Node rightChild;

    
    public Node(Object D, Node L, Node R)
    {
        this.data = D;
        this.leftChild = L;
        this.rightChild = R;

    }
    
    
    public void inorder()
    {
        
        if( leftChild != null ) 
            leftChild.inorder();
            
        System.out.print( " " + data );
        
        if( rightChild != null ) 
            rightChild.inorder();
    }
        
        
	public Double eval ()
    {		
        
		Double validated=0.1;
        
        return validated;
	}
    
    public String infix()
    {
        String result = "";
        
        return result;
    }
}

hello efus
1. To have a reference to tree You need declare Node root = null; ,and change this systematically.
2. Better way is declare stack as below Deque<Node> stack = new ArrayDeque<Node>(); 3.In class Node declare

public String toString() {
        return "[" + data + "L" + leftChild + "R" + rightChild + "]";
    }

4.Use this to check current root value and calculate how many null -s You have. Use also inorder() to check is left feft or right is right?
5.Draw on paper Your tree and fill all null values.
6.Start from String exsp = "2 2 *"; quuba

This article has been dead for over six months. Start a new discussion instead.