9
I’m trying to assemble a Generic Tree in java to assemble a Boolean expression within a gene expression algorithm, this tree would hold several types of variables, I’m not sure which is the most optimized way to do this, I was thinking of saving logical operator as &&
, ||
in strings and also mathematical operators +
, -
, /
, *
. Operators would be stored at branch/root nodes and operands that could be functions or variables would be stored at terminal nodes (leaves), anyone have any idea? I spent the day in that trouble and I’m a little frustrated.
public class ArvoreJava {
public static No raiz; // o único campo de dado em Arvore
public ArvoreJava() { // construtor
raiz = null; //nenhum nó na arvore
}
public static No insere(String palavra, No no) { //metodo insere
if(no == null){
no = new No(palavra); //se nao existir nó cria um novo
}
else if((compare(palavra, no.palavra)) < 0){ // faz comparação, se palavra
no.filhoEsquerda = ArvoreJava.insere( palavra , no.filhoEsquerda);// menor que nó, insere na esquerda
}
else if((compare(palavra, no.palavra)) > 0){//se palavra maior que nó, insere na direita
no.filhoDireita = ArvoreJava.insere(no.palavra, no.filhoDireita);
}
else{// senão, palavra já contem
System.out.println("ERRO: valor já existe na árvore.");
return null;
}
return no;
}
public static void caminhando(ArvoreJava arv){//caminha na arvore
System.out.println("Pré-ordem: ");
arv.preordem(arv.raiz);
}
public static int compare(String palavra, String no){ // compara strings e retorna um inteiro
return palavra.toString().compareTo(no.toString());//-1 menor, 1 maior, 0 iguais
}
public void preordem(No no) {//caminha em preordem
if (no == null){
return;
}
System.out.println(no.palavra);
preordem(no.filhoEsquerda);
preordem(no.filhoDireita);
}
}
And the class of the knot.
package arvore;
public class No {
String palavra; //dado
No filhoEsquerda; //cria filho a esquerda
No filhoDireita; // cria filho a direita
public No(String palavra){
this.palavra = palavra;
}
public void mostraNo(){
{
System.out.print(palavra);
System.out.print(", ");
}
}
}
That is, what I would know how to implement is very simple, but in the personal project I need to implement a structure with these characteristics or close to them to get close to some satisfactory result. Whoever has the patience to try to help, I thank you in advance.
My problem is centered on the concept, I understand how to implement a tree but a tree that accepts these various types I am doubtful. I’ll post more or less what I know.
– Nicolas Bontempo
If I understand correctly, your problem is that you just want to represent the operators and operands within a logical expression in the form of a tree (the fact of being applied in a genetic algorithm does not come to the case).
– Luiz Vieira
This Luiz, this is my biggest impasse at the moment.
– Nicolas Bontempo
Should I assume that you know how to assemble tree structures in java and that your doubt is only about the most appropriate way to represent operators and operands? It’s not clear in the question.
– Luiz Vieira
I would know how to assemble a tree that would accept only one type, but I wouldn’t know how to represent a generic tree with several types, which is what I would need (if I’m right r) to represent the operands and operators.
– Nicolas Bontempo
@user2984406 Do you have experience with generic types in Java? Either way, I’m just assembling an answer with an example.
– mgibsonbr
I have an experience with little depth, anyway I’ll be studying this part today the time I have left of the night.
– Nicolas Bontempo
@user2984406 I put an answer with a practical example of how such a tree could be modeled. From your question, I see that you already have a good command of the "tree" data structure, so I focused on demonstrating how to mix knots of different types in the same tree (which seems to be the central point of your doubt). The methods for handling, traversing, etc, I leave to you. :)
– mgibsonbr