2
I’m trying to build a tree that receives a completely parented mathematical expression [e.g.: ((a+b)+c)] but I think my insertion function is wrong! Will someone give me a light?
typedef struct arvore {
char info;
struct arvore*esq;
struct arvore*dir;
}no;
no *cria(no*raiz, char el[], int i){
int j = i;
char s= el[j];
//printf("%s", el);
if(raiz == NULL){
raiz = (no*)malloc(sizeof(no));
(raiz)->dir = NULL;
(raiz)->esq = NULL;
}
if(s == '('){
j++;
s=el[j];
raiz->esq = cria(raiz->esq, el, j);
if(s == '+'){
(raiz)->info = s;
j++;
s=el[j];
raiz->dir = cria((raiz)->dir, el, j);
if(s==')'){
j++;
s=el[j];
//return raiz;
}
}
}else{
(raiz)->info=s;
j++;
s=el[j];
return raiz;
}
}
the intention is to make the tree have only letters and operators, and each node q has an operator in the field info will have 2 children with letters in the fields Infos, respective!
Thanks man, it helped a lot! This program is for a college job, so I think the best way out can be to use global variables! Full gratitude!
– roooooon