0
Iai galera, I have a problem in my code seemingly simple to solve, but I still get confused with pointers.
The code is to insert each digit and operator of an entire parent expression into a binary tree. The logic is, when finding a "(", I fill the root with "#" and save the address of this root in a stack to then be used and call that root again and fill in with the operator that will be found.
Ex: Takes the expression (2+3). The root will be "#", then calls the function to the left and fills in with "2" and calls the function again. When you find "+", call the top of the stack function and take the address saved by the pointer and call the root that has "#" and replace with "+". Then calls the insert function again by pointing right from this root and fills with "3".
I did some printing tests and believe that the other functions work properly. After finishing the insert function, I put it to print the root and exit "+", but there are no subtrees because it is the only thing q prints.
Arvore *Inserir(Arvore *raiz, char expressao[], int j, Pilha *PILHA){
char k = '#';
if(expressao[j] == '('){
if(raiz == NULL){
raiz = Criar_Raiz(k);
}
Arvore *ponteiro = (Arvore*)malloc(sizeof(Arvore));
ponteiro = raiz; // ponteiro recebe o endereço da raiz marcada com "#"
Inserir_Pilha(PILHA, ponteiro); //insere o ponteiro com o endereço na pilha
j++;
Inserir(raiz -> esquerda, expressao, j, PILHA);}
else if((expressao[j] == '+') || (expressao[j] == '-') || (expressao[j] == '*') || (expressao[j] == '/')){
Pilha *TOPO = Acessa_Topo_Pilha(PILHA);
raiz = TOPO -> endereco; // pega o endereço (raiz) armazenado no topo da pilha que indica o endereço da raiz a ser usada
raiz -> item = expressao[j];
j++;
Inserir(raiz -> direita, expressao, j, PILHA);}
else if(expressao[j] == ')'){
Retirar_Pilha(PILHA);
if (Vazia(PILHA))
j = 0;
else{
j++;
Inserir(raiz, expressao, j, PILHA);
}
}
else{ // se for algarismo
if(raiz == NULL)
raiz = Criar_Raiz(expressao[j]);
j++;
Inserir(raiz, expressao, j, PILHA);
}
return raiz;
}