How to create a pointer to store the address of a binary tree root and then use this address to call the root?

Asked

Viewed 114 times

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;
}

1 answer

0


[RESOLVED]

Simple changes were needed that I didn’t pay attention to before, and the pointer was doing right td. The code of the Insert function that was posted was the only modified.

Arvore *Inserir(Arvore *raiz, char expressao[], int j, Pilha *PILHA){ // Função de inserir na árvore    

Arvore *ponteiro = (Arvore*)malloc(sizeof(Arvore)); // mudança feita aqui
char k = '#';

if(expressao[j] == '('){ 
    if(raiz == NULL) // verifica se a raiz é nula
        raiz = Criar_Raiz(k);


    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++;
    raiz -> esquerda = Inserir(raiz -> esquerda, expressao, j, PILHA);} //mudança feita aqui

else if((expressao[j] == '+') || (expressao[j] == '-') || (expressao[j] == '*') || (expressao[j] == '/')){ // quando encontra um operador na expressão
    Pilha *TOPO = Acessa_Topo_Pilha(PILHA);
    raiz = TOPO -> endereco;
    raiz -> item = expressao[j];
    j++; 
    raiz -> direita = Inserir(raiz -> direita, expressao, j, PILHA);} //mudança feita aqui

else if(expressao[j] == ')'){
    Retirar_Pilha(PILHA);
    if (Vazia(PILHA))
         j = 0;colocada na árvore. sai da função
    else{ // se não está vazia
        j++; //
        Inserir(raiz, expressao, j, PILHA);
    }
}

else{ // se for algarismo
    if(raiz == NULL)
        raiz = Criar_Raiz(expressao[j]); 
    j++; 
    Inserir(ponteiro, expressao, j, PILHA); // mudança feita aqui
}
return raiz;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.