Hey guys, I have a problem with my seemingly simple code to solve, but I still get confused with pointers.
The code is to insert each digit and operator of an entire parenthesized expression into a binary tree. The logic is, when I find a "(", I fill in the root with "#" and I save the address of this root in a heap to later be used and call that root again and fill with the operator that will be found.
Ex: Get the expression (2 + 3). The root will be "#", then call the function to the left and fill in with "2" and call the function again. When you find "+", call the function to access the top of the stack and get the address saved by the pointer and call the root that is "#" and replace with "+". Then call the insert function again by pointing to the right from this root and filling it with "3".
I did some printing tests and I believe the other functions work correctly. After completing the insert function, I put it to print the root and leave "+", but there are no subtrees because it is the only thing that 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;
}