2017-08-12 36 views
0

我正在编写一个代码,以便通过使用2个数组列表和一个链接将中缀语句转换为二进制树名单。我收到错误消息,说明:在螺纹将中缀字符串转换为二叉树并收到的代码:不可编译的源代码 - 错误的sym类型

异常“主”了java.lang.RuntimeException:不可编译的源代码 - 错误的符号类型:prog5.InFixToBinaryTreeConverter.precedence 在prog5.InFixToBinaryTreeConverter.createBinaryTree(InFixToBinaryTreeConverter.java:56) 在prog5.InFixToBinaryTreeConverter.run(InFixToBinaryTreeConverter.java:31) at prog5.Prog5.main(Prog5.java:13)

我很茫然,我该如何解决这些错误?

我的主要方法:

public class Prog5 { 

    public static void main(String[] args) { 
     InFixToBinaryTreeConverter fp = new InFixToBinaryTreeConverter(); 
     fp.run("((6 + 2) - 5) * 8/2"); 
    } 
} 

Node类:

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

缀以二叉树的转换器类:

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

你拼错'precedence',方法名是'precendence' – tima

+1

@tima你应该将其添加为一个答案。这正是解决问题的方法。 –

+0

我做了修正,但我仍然收到错误。对于这行:while(precedence(this.stack.get((this.stack.size() - 1,variable))))它表示int是必需的,但是找到一个int和一个String –

回答

0

你拼错在while循环条件precedence,您在下面定义的方法被命名为precendence

我不确定你使用的是什么编辑器,但是在Eclipse中这会显示为错误,当你将鼠标悬停在它上面时,它会说该方法不存在。

this.stack.get方法只需要一个参数,它是一个int,但您通过它(this.stack.size()-1, variable)。你大概意思是这样的:

precedence(this.stack.get(this.stack.size() - 1), variable)

相关问题