(JAVA)
I have "cannot be resolved or is not a field" error on
node.right = y; and node.left = x;
//BuildExpressionTree.java
import java.util.*;
import javax.swing.*;
import javax.xml.soap.Node;
public class BuildExpressionTree {
private Stack stack = new Stack();
private Node node;
public static boolean isOperator(String token){
if(token == "+" || token == "-" ||
token == "*" || token == "/"){
return
true;
}
JOptionPane.showMessageDialog(null,
"Invalid token" + token, "Token Error",
JOptionPane.WARNING_MESSAGE);
return false;
}
public static boolean isInteger(String token){
try{
if(Integer.parseInt(token) == 0 ||
Integer.parseInt(token) == 1 ||
Integer.parseInt(token) == 2 ||
Integer.parseInt(token) == 3 ||
Integer.parseInt(token) == 4 ||
Integer.parseInt(token) == 5 ||
Integer.parseInt(token) == 6 ||
Integer.parseInt(token) == 7 ||
Integer.parseInt(token) == 8 ||
Integer.parseInt(token) == 9){
return
true;
}
}catch(NumberFormatException
nfe){
JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token
Error", JOptionPane.WARNING_MESSAGE);
return
false;
}
return true;
}
public String[] postfixExpression(String
postfixExp){
String expression =
postfixExp;
String[] expressionArray =
expression.split("\\s+");
String splitExpression = "";
for(int i = 0; i <
expressionArray.length; i++){
splitExpression
+= expressionArray[i];
}
StringTokenizer tokenizedExpression
= new StringTokenizer(splitExpression);
splitExpression = "";
while(tokenizedExpression.hasMoreTokens()){
String nextToken
= tokenizedExpression.nextToken();
splitExpression
+= nextToken + " ";
}String[] tokens =
splitExpression.split("\\s+");
return tokens;
}
public String constructTree(String expression) throws
RuntimeException{
stack = new Stack();
String result;
String[] expressionRead =
postfixExpression(expression);
for(int i = 0; i <
expressionRead.length; i++){
String nextToken
= expressionRead[i];
if(isInteger(nextToken)){
node = (Node) new OperandNode(nextToken);
stack.push(node);
}else
if(isOperator(nextToken)){
node = (Node) new OperatorNode(nextToken);
Node y;
Node x;
y = (Node) stack.pop();
x = (Node) stack.pop();
node.right = y;
node.left = x;
stack.push(node);
}else{
throw new RuntimeException();
}
}
result =
stack.peek().toString();
return result;
}
}
//Node.java
public abstract class Node {
protected String data;
protected Node left;
protected Node right;
public void setData(String data){
this.data = data;
}
public String getData(){
return data;
}
}
class OperandNode extends Node{
public OperandNode(String operand){
this.data = operand;
this.left = null;
this.right = null;
}
public String toString(){
return data + "";
}
}
class OperatorNode extends Node{
public OperatorNode(String operator){
this.data = operator;
this.left = null;
this.right = null;
}
public void setLeft(Node left){
this.left = left;
}
public Node getLeft(){
return left;
}
public void setRight(Node right){
this.right = right;
}
public Node getRight(){
return right;
}
public String toString(){
return "( " + this.left + " " +
data + this.right + " )";
}
}
/**
*
* @author Rashmi Tiwari
*/
import java.util.*;
import javax.swing.*;
import javax.xml.soap.Node;
public class BuildExpressionTree {
private Stack stack = new Stack();
private Node node;
public static boolean isOperator(String token){
if(token == "+" || token == "-" || token == "*" || token ==
"/"){
return true;
}
JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token
Error", JOptionPane.WARNING_MESSAGE);
return false;
}
public static boolean isInteger(String token){
try{
if(Integer.parseInt(token) == 0 ||
Integer.parseInt(token) == 1 ||
Integer.parseInt(token) == 2 ||
Integer.parseInt(token) == 3 ||
Integer.parseInt(token) == 4 ||
Integer.parseInt(token) == 5 ||
Integer.parseInt(token) == 6 ||
Integer.parseInt(token) == 7 ||
Integer.parseInt(token) == 8 ||
Integer.parseInt(token) == 9){
return true;
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "Invalid token" + token, "Token
Error", JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
public String[] postfixExpression(String postfixExp){
String expression = postfixExp;
String[] expressionArray = expression.split("\\s+");
String splitExpression = "";
for(int i = 0; i < expressionArray.length; i++){
splitExpression += expressionArray[i];
}
StringTokenizer tokenizedExpression = new
StringTokenizer(splitExpression);
splitExpression = "";
while(tokenizedExpression.hasMoreTokens()){
String nextToken = tokenizedExpression.nextToken();
splitExpression += nextToken + " ";
}String[] tokens = splitExpression.split("\\s+");
return tokens;
}
public String constructTree(String expression) throws
RuntimeException{
stack = new Stack();
String result;
String[] expressionRead = postfixExpression(expression);
for(int i = 0; i < expressionRead.length; i++){
String nextToken = expressionRead[i];
if(isInteger(nextToken)){
node = (Node) new OperandNode(nextToken);
stack.push(node);
}else if(isOperator(nextToken)){
node = (Node) new OperatorNode(nextToken);
Node y;
Node x;
y = (Node)stack.peek();
stack.pop();
x = (Node)stack.peek();
stack.pop();
node.right = y;
node.left = x;
stack.push(node);
}else{
throw new RuntimeException();
}
}
result = stack.peek().toString();
return result;
}
}
//Node.java
public abstract class Node {
protected String data;
protected Node left;
protected Node right;
public void setData(String data){
this.data = data;
}
public String getData(){
return data;
}
}
class OperandNode extends Node{
public OperandNode(String operand){
this.data = operand;
this.left = null;
this.right = null;
}
public String toString(){
return data + "";
}
}
class OperatorNode extends Node{
public OperatorNode(String operator){
this.data = operator;
this.left = null;
this.right = null;
}
public void setLeft(Node left){
this.left = left;
}
public Node getLeft(){
return left;
}
public void setRight(Node right){
this.right = right;
}
public Node getRight(){
return right;
}
public String toString(){
return "( " + this.left + " " + data + this.right + " )";
}
}
Get Answers For Free
Most questions answered within 1 hours.