Je travaille sur un code pour prendre une instruction infixe et la convertir en un arbre binaire en utilisant 2 listes de tableaux et un lien liste. J'ai reçu une erreur indiquant:Code pour convertir une chaîne infixe en un arbre binaire et j'ai reçu: Code source non compilable - Type de code erroné
Exception dans le thread "principal" java.lang.RuntimeException: code source Uncompilable - Type Erroneous sym: prog5.InFixToBinaryTreeConverter.precedence à prog5.InFixToBinaryTreeConverter.createBinaryTree (InFixToBinaryTreeConverter.java:56) à prog5.InFixToBinaryTreeConverter.run (InFixToBinaryTreeConverter.java:31) à prog5.Prog5.main (Prog5.java:13)
Je suis à une perte, comment puis-je corriger ces erreurs?
Ma principale méthode:
public class Prog5 {
public static void main(String[] args) {
InFixToBinaryTreeConverter fp = new InFixToBinaryTreeConverter();
fp.run("((6 + 2) - 5) * 8/2");
}
}
La classe 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);
}
}
Le Infix à la classe Binary Tree Converter:
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;
}
}
}
vous avez mal orthographié 'precedence', le nom de la méthode est' precendence' – tima
@tima Vous devez l'ajouter comme une réponse. C'est exactement la solution au problème. –
J'ai fait la correction mais je reçois toujours l'erreur. Pour la ligne: while (precedence (this.stack.get ((this.stack.size() - 1, variable)))) il est dit qu'un int est requis mais un int et un String sont trouvés –