I am working on a code to take an infix statement and convert it to a binary tree by using 2 array lists and a linked list. I recieved an error stating:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: prog5.InFixToBinaryTreeConverter.precedence at prog5.InFixToBinaryTreeConverter.createBinaryTree(InFixToBinaryTreeConverter.java:56) at prog5.InFixToBinaryTreeConverter.run(InFixToBinaryTreeConverter.java:31) at prog5.Prog5.main(Prog5.java:13)
I am at a loss, how can I fix these errors?
My main method:
public class Prog5 {
public static void main(String[] args) {
InFixToBinaryTreeConverter fp = new InFixToBinaryTreeConverter();
fp.run("( ( 6 + 2 ) - 5 ) * 8 / 2");
}
}
The Node class:
public class Node<String> {
protected String element;
protected Node<String> left;
protected Node<String> right;
int x;
public Node(String e, Node left, Node right){
element = e; //data = element
this.left = this.right = null;
}
public void displayNode(){
System.out.print(element);
}
}
The Infix to Binary Tree Converter class:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static jdk.nashorn.internal.runtime.JSType.isNumber;
public class InFixToBinaryTreeConverter{
List<String> stack = new ArrayList<>(); //stack
List<String> inFix= new LinkedList<>(); //queue
List<Node> btstack = new ArrayList<>(); //stack
Node root = null;
//create a no-arg consutrctor that initializes the inFix, stack , &
btstack lists
String expression;
public void run(String s){ // run method is driver for program
this.expression = s;
createInFix();
createBinaryTree();
}
public void createInFix(){
String[] temporary = expression.split("\\s+"); //temp = a
for (int i = 0; i < temporary.length; i++ ){
inFix.add(temporary[i]);
}
}
public Node createBinaryTree(){
this.stack.add("(");
this.inFix.add(")");
while(!this.inFix.isEmpty()){
String variable = this.inFix.remove(0);
if(isNumber(variable)){
Node nodeNew = new Node(variable);
this.btstack.add(nodeNew);
}
if(isLeftParentheses(variable)){
this.stack.add(variable);
}
if(isOperator(variable)){
while(precedence(this.stack.get((this.stack.size()-1, variable)))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
this.stack.add(variable);
}
if(isRightParentheses(variable)){
if(this.stack.get(this.stack.size()-1) != null){
while(!isLeftParentheses(this.stack.get(this.stack.size()-1))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
root = this.btstack.get((this.btstack.size()-1));
}
return root;
}
void process(Node node){
System.out.print(node.element+ " ");
}
/* Given a binary tree, print its nodes in inorder*/
private void printInorder(Node node){
if (node != null){
printInorder(node.left); // first recur on left child
process(node); // then print the data of node
printInorder(node.right); // now recur on right child
}
}
/* Given a binary tree, print its nodes in preorder*/
private void printPreorder(Node node){
if (node != null){
process(node); // first print data of node
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
}
}
private void printPostorder(Node node){
if (node != null){
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
process(node); // first print data of node
}
}
void printPostorder(){
printPostorder(root);
}
void printInorder(){
printInorder(root);
}
void printPreorder(){
printPreorder(root);
}
private static boolean isOperator(String str){ //check to see if c is an operator
if("+".equals(str) || "-".equals(str) || "*".equals(str) || "/".equals(str) || "^".equals(str)){
return true;
}else {
return false;
}
}
private static boolean precendence(String operator1, String operator2){
if("^".equals(operator1)){
return true;
} else if ("^".equals(operator2)){
return false;
}else if ("*".equals(operator1) || "/".equals(operator1)){
return true;
}else if("+".equals(operator1) || "-".equals(operator1)){
if("*".equals(operator2) || "/".equals(operator2)){
return false;
}else{
return true;
}
}return false;
}
private boolean isLeftParentheses(String x) {
if("(".equals(x)){
return true;
}
else {
return false;
}
}
private boolean isRightParentheses(String x) {
if(")".equals(x)){
return true;
}
else {
return false;
}
}
}
You misspelled precedence
in your while loop condition, the method you defined below is named precendence
.
I'm not sure what editor you are using but in Eclipse this would show up as an error and when you hover over it, it will say the method does not exist.
And this.stack.get
method takes exactly one parameter which is an int
, but you are passing it (this.stack.size()-1, variable)
. What you probably meant is this:
precedence(this.stack.get(this.stack.size() - 1), variable)