0

I can't seem to find where the problem is in my code, i'm essentially storing character patterns, if an existing pattern is already present I'll store it in the duplicate node, otherwise it will shift to the regular pattern one. The problem starts when i have a duplicate, it should move to the duplicate node, but instead loops infinitely.

The binary tree struct: :

struct arbre{
    char id[10];
    int length;
    int token;
    int count;
    struct arbre *pattern;
    struct arbre *doublon;
};
typedef struct arbre *Arbre; 

The function to create and make new nodes

void ajouter(Arbre *a, char *tablettre, int length, int token){
    if(*a==NULL){
        *a=(Arbre)malloc(sizeof(struct arbre));
        strcpy((*a)->id, tablettre);
        //append((*a)->id,tablettre);
        //printf("%s",(*a)->id);
        (*a)->length = length;
        (*a)->token = token;
        (*a)->doublon=NULL;
        (*a)->pattern=NULL;
    }
    if (strcmp((*a)->id, tablettre) == 0){   /// The problem is here
        printf("%s",(*a)->doublon->id);
        printf(" and %s",tablettre);
        ajouter(&(*a)->doublon, tablettre, length, token);
    }
    if (strcmp((*a)->id, tablettre) != 0){
        ajouter(&(*a)->pattern, tablettre, length, token);

    }

}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    If you enter that function with `*a==NULL`, what is content of `doublon` in `printf("%s",(*a)->doublon->id);`? – Gerhardh May 05 '22 at 13:26
  • We do not see what data (variable types, content) you pass into that function. Please provide a [MCVE](https://stackoverflow.com/help/mcve) demonstrating the problem. – Gerhardh May 05 '22 at 13:27
  • 2
    Also get rid of `typedef struct arbre *Arbre; ` hiding pointer types with a typedefs only adds confusion. When I see `Arbre` I have absolutely no idea that is is a pointer. If I see `struct arbre *`, I know it's a pointer. – Jabberwocky May 05 '22 at 13:29
  • 2
    ... and thing s like `*a=(Arbre)malloc(sizeof(struct arbre));` are terrible. Rather write `(struct arbre*)malloc(sizeof(struct arbre));` or remove the cast alltogether and write `*a = malloc(sizeof(struct arbre));` – Jabberwocky May 05 '22 at 13:31
  • 2
    Besides your issue with dereferencing a `NULL` pointer, how would you ever get out of your recursion once you pass `*a==NULL`? You will always find that `id` holds same content as `tablettre` as you just copied it there a few lines above. Then you will always call again. You mustn't go from `if` to `if` without an `else` – Gerhardh May 05 '22 at 13:31
  • 1
    You should start learning how to use a debugger. Stepping through your code would immediately reveal that you will always get into second `if` after you entered the first one. That would have shown the issue withing seconds. – Gerhardh May 05 '22 at 13:35
  • What you create is basically a list of lists. All different strings are linked via `pattern` pointer, then all nodes with same string are linkes via `doublon` pointer. That is not really what I would call a "binary tree". Also the strings in `pattern` list are not sorted at all. Is that intented? – Gerhardh May 05 '22 at 14:03
  • Thank you everyone for all your answers! After posting it i started doing what you suggested Gerhard as i should have done before even posting the question. That was exactly my problem Gerhardh thank you! I should have realized that.. Yeah indeed that's not really a binary but i didn't really know how to call that, thank you for taking the time in any case that really helped ! I'm not sure what you mean by the strings in pattern not being sorted? – E.Bassaget May 05 '22 at 15:02
  • Jabberwocky Haha i just "copied" how my teachers wrote it, good to know it's bad i will remember that – E.Bassaget May 05 '22 at 15:06

0 Answers0