'C : Binary tree infinite recursion problem
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
