Problems with binary search tree in C

0

Now repositioning my post more clearly (this is a continuation of my previous question ). In the code, I try to create a binary search tree and insert elements into it. They should be saved in .txt file. As soon as I run it it says that .exe has stopped working. When I comment the insert method on main, it runs fine. When debugging it it accuses segmentation fault in p->id = 1 . I can not solve this. Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_NOME 50

typedef struct politico{
   int id;
   char* nome;
   char* apelido;
//Partido* partido;
   char* cargo;
   float valorPropinaMensal;
   int quantidadeVezes;

   struct politico* esq;
   struct politico* dir;

}Politico;

   Politico* criarP(int id, char* nomeCompleto, char* apelido, char* cargo, float valor, int qtdDeVezes) {

      Politico* politico = (Politico*)malloc(sizeof(politico));

      politico->id = id;

      politico->nome = malloc(MAX_NOME * sizeof(char));
      strcpy(politico->nome, nomeCompleto);
      politico->apelido = malloc(MAX_NOME * sizeof(char));
      strcpy(politico->apelido, apelido);
      politico->cargo = malloc(MAX_NOME * sizeof(char));
      strcpy(politico->cargo, cargo);
      //no->partido = malloc(MAX_NOME * sizeof(char));
      //strcpy(no->partido, partido);
      politico->valorPropinaMensal = valor;
      politico->quantidadeVezes = qtdDeVezes;
      politico->esq = NULL;
      politico->dir = NULL;

    return politico;
  }

Politico* inserirPoliticos(Politico* raiz, int id, char* nome, char* apelido, char* cargo, float valor, int qtdDeVezes){

   Politico* novo = criarP(id , nome, apelido, cargo, valor, qtdDeVezes);
   if(raiz == NULL) return novo;

   Politico* no = raiz;
   Politico* pai = NULL;
   while(no != NULL){
      pai = no;
      if(novo->id < no->id) no = no->esq;
      else no = no->dir;
  }

   if(pai->id > novo->id) pai->esq = novo;
   else pai->dir = novo;

   return raiz;
}

int main(){
   Politico* p = NULL;

   FILE* pEscrita = fopen("politicos.txt","w");

   p->id = 1;
   p->nome="sdwwfw";
   p->apelido="sdvdsvs";
   p->cargo="asasa";

   p->valorPropinaMensal=22.0;
   p->quantidadeVezes= 2;

   p = inserirPoliticos(p, p->id, p->nome, p->apelido, p->cargo, p->valorPropinaMensal, p->quantidadeVezes);

   fprintf(pEscrita,"%d;%s;%s;%s;%.2f;%d", p->id, p->nome, p->apelido, p->cargo, p->valorPropinaMensal, p->quantidadeVezes);

   fclose(pEscrita);
   return 0;
}
    
asked by anonymous 10.07.2016 / 19:13

1 answer

1

Look at this:

Politico* p = NULL;
p->id = 1;

If p is NULL , it is obvious that p->id = 1; will crash .

Let's see what you're trying to do:

  • No main you try to create (incorrectly) Politico .
  • You remove all data from this Politico and pass them one by one to the function inserirPoliticos .
  • In function inserirPoliticos , the function criarP is called.
  • In the criarP function, a new Politico is reassembled with the same data.
  • At last, you ride the politician, dismount and ride again. This is not very efficient. So there are two possible approaches:

  • You mount the policy in main and pass it to inserirPoliticos which only inserts a Politico already mounted.

  • You pass the raw data to inserirPoliticos and let inserirPoliticos mount the Politico structure when calling criarP .

  • I think the first approach is cleaner, for avoid having to pass a lot of parameters pertaining to the same thing in several different functions. So, its inserirPoliticos function looks like this:

    Politico* inserirPoliticos(Politico* raiz, Politico *novo) {
        if (raiz == NULL) return novo;
    
        Politico* no = raiz;
        Politico* pai = NULL;
        while (no != NULL) {
            pai = no;
            if (novo->id < no->id) {
                no = no->esq;
            } else {
                no = no->dir;
            }
        }
    
        if (pai->id > novo->id) {
            pai->esq = novo;
        } else {
            pai->dir = novo;
        }
    
        return raiz;
    }
    

    And with this, criarP is used in the main function:

    int main() {
        Politico *novo = criarP(1, "sdwwfw", "sdvdsvs", "asasa", 22.0, 2);
        Politico *raiz = inserirPoliticos(NULL, novo);
    
        FILE* pEscrita = fopen("politicos.txt", "w");
        fprintf(pEscrita, "%d;%s;%s;%s;%.2f;%d", p->id, p->nome, p->apelido, p->cargo, p->valorPropinaMensal, p->quantidadeVezes);
    
        fclose(pEscrita);
        return 0;
    }
    
        
    10.07.2016 / 20:24