Structure and pointers in C: Binary Tree
Hello, I'm trying to create a tree of mathematical expressions with the following logic:
My expression is a string, and I have 4 Character Options:
- (: upon finding this character, my code should create a node for the
pref
, insert in itsconteudo(pref->conteudo)
the character'#', save the address of thepref
in a vector of pointers of type pontNo calledendereco
, sum the variablek
(making the vector walk for next iteration) and do with whichpref
points topref->esq
. - digits (0 through 9): creates a node in
pref
and fills itsconteudo(pref->conteudo)
with the digit character. - operator ( + - * / ): when finding an operator, it looks at the last address of the vector
endereco
, causespref = endereco[k] (implicando que pref irá apontar para esse novo endereço)
to thenpref->conteudo
receive the character referring to the operator, after thatpref = pref->dir(aponta para a direita)
. - ): decreases the variable
k
, causing the vector referenceendereco
to decrease. If it makes it easier, I thought of the vectorendereco
with if it was a stack in itself.
That was my idea, however I'm getting serious segment fault issues even though I can't think of what I'm doing wrong. The code follows below divided into main and arvbin.c
Note: My intention was to have a pointer moving around the tree (pm
) and something static as a reference for the root, but as I said I'm having serious problems with handling pointers.
PS: I realized in other answers that setting with typedef a pointer to a structure is a bad programming practice, however this was the only way I could learn to define a pointer to an X structure within this structure.
Arvbin.h
#include <stdio.h>
#include <stdlib.h>
#include "arvbin.h"
typedef struct no *pontNo;
typedef struct no{
char conteudo;
pontNo esq;
pontNo dir;
}no;
void CRIA_NO(pontNo novoNo,char info){
novoNo = malloc(sizeof(no));
novoNo->conteudo = info;
novoNo->esq = NULL;
novoNo->dir = NULL;
}
Main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arvbin.h"
int main() {
//Declaracao de variaveis & Inicializacoes:
no raiz; //Raiz da arvore declarada
raiz.conteudo = '#'; //Declarando conteudo da raiz
pontNo pm = &raiz; //(P)onteiro para (M)ovimentacao na arvore,inicialmente apontando para raiz
pontNo endereco[20]; //Vetor de ponteiros para endereco do tipo pontNo, funcionara como uma pilha
char expressao[100]; //Expressao matematica inserida
int resultado;
//Funcionamento:
while(1){
printf("Escreva a expressao matematica(Caso queira parar, escreva 0): ");
scanf("%s",expressao);
if(strcmp(expressao,"0") == 0) break; //Condicao de parada do loop de insercao de expressoes
int k = 0;
int error = 0;
int i;
for(i = 0;expressao[i] != '\0';i++){
if(expressao[i] == '('){
if(i == 0){
endereco[k] = pm;
printf("%c\n",endereco[k]->conteudo);
k++;
pm = pm->esq;
}
else{
CRIA_NO(pm,'#');
endereco[k] = pm;
printf("%c\n",endereco[k]->conteudo);
k++;
pm = pm->esq;
}
}
else if(expressao[i] == '0'||expressao[i] == '1'||expressao[i] == '2'||expressao[i] == '3'||expressao[i] == '4'||expressao[i] == '5'||expressao[i] == '6'||expressao[i] == '7'||expressao[i] == '8'||expressao[i] == '9'){
CRIA_NO(pm,expressao[i]);
printf("%c\n",pm->conteudo);
}
else if(expressao[i] == '+'||expressao[i] == '-'||expressao[i] == '*'||expressao[i] == '/'){
}
else if(expressao[i] == ')'){
}
else{
printf("Erro na expressao: char nao reconhecido!\n");
error = 1;
break;
}
}
}
return 0;
}
If you have another way to solve this problem, let me know!