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