-
Help with tree
So i have this code (main.c tab) and the output (untitled tab):
GDB online Debugger | Code, Compile, Run, Debug online C, C++
The problem is:
I don't want a node to have a repeated child e.g. node at level 2 had a child 'd' so it can't have another child 'd'.
for that i had a simple loop at line 38:
Code:
for(c = 0; c < 3; c++){
if( mother->child[c] != NULL){
if(choice == mother->child[c]->opt){
choice++;
}
}
}
But if you check the output this seems to be ignored. I don't have time to change the whole logic. I wanna know if somehow i can fix it or should I accept my fate.
-
Code:
char choice = 'a';
if( lvl == 5 ) return;
tmp=(Node*)malloc(sizeof(Node));
for(c = 0; c < 3; c++)
tmp->child[c] = NULL;
//avoid same opt siblings
//won't work. WHY ????
for(c = 0; c < 3; c++){
if( mother->child[c] != NULL){
if(choice == mother->child[c]->opt){
choice++;
}
}
}
if(choice > 'f'){
free (tmp);
return;
}
How are you supposed to get from 'a' to 'f' in only 3 iterations?
You may as well bail at the first match.
Code:
if(choice == mother->child[c]->opt){
free (tmp);
return;
}
No I haven't tested it.
-
Ohmygod I just noticed this foolishness. I just copied the for loop after each "choice" if and it worked. Of course it couldn't go from 'a' to 'f' like that. Thank you my friend.