#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int j1,j2;
int stat;
char opt;
struct node *child[3];
} Node;
void set_root(Node **root){
int c;
*root = (Node*)malloc(sizeof(Node));
(*root)->stat = 1;
(*root)->opt = 'R';
(*root)->j1 = 0;
(*root)->j2 = 0;
for(c = 0; c < 3; c++)
(*root)->child[c] = NULL;
}
void tree (Node *mother, int lvl, int k, int l, int vo1, int vo2, int x){
Node *tmp = NULL;
int children, j, i, c;
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;
}
if( choice == 'a' ){
if( k < 4 ){
i = 4;
j = l;
if( i == vo1 && j == vo2 ) choice++;
else{
tmp->opt = choice;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
mother->child[x] = tmp;
vo1 = k;
vo2 = l;
for(children = 0;children < 3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
else choice++;
}
if( choice == 'b' ){
if(l < 3 ){
i = k;
j = 3;
if(i == vo1 && j == vo2) choice++;
else{
tmp->opt = choice;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
mother->child[x] = tmp;
vo1 = k;
vo2 = l;
for(children = 0;children < 3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
else choice++;
}
if( choice == 'c' ){
if( k > 0 ){
i = 0;
j = l;
if(i == vo1 && j == vo2) choice++;
else{
tmp->opt = choice;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
mother->child[x] = tmp;
vo1 = k;
vo2 = l;
for(children=0;children<3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
else choice++;
}
if( choice == 'd'){
if( l > 0 ){
i = k;
j = 0;
if( i == vo1 && j == vo2 ) choice++;
else{
tmp->opt = choice;
mother->child[x] = tmp;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
vo1 = k;
vo2 = l;
for(children = 0;children<3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
else choice++;
}
if( choice == 'e'){
if(k>0 && l<3){
i = k;
j = l;
while(i>0 && j<3){
i--;
j++;
}
if( i == vo1 && j == vo2 ) choice++;
else{
tmp->opt = choice;
mother->child[x] = tmp;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
vo1 = k;
vo2 = l;
if( tmp->j1 == 2 || tmp->j2 == 2){
tmp->stat = 1;
mother->stat = 1;
return;
}
for(children=0;children<3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
else choice++;
}
if( choice == 'f'){
if(k<4 && l>0){
i = k;
j = l;
while(i<4 && j>0){
i++;
j--;
}
if( i == vo1 && j == vo2 ) return;
else{
tmp->opt = choice;
mother->child[x] = tmp;
tmp->j1 = i;
tmp->j2 = j;
tmp->stat = 0;
vo1 = k;
vo2 = l;
if( tmp->j1 == 2 || tmp->j2 == 2){
tmp->stat = 1;
mother->stat = 1;
return;
}
for(children = 0;children<3;children++){
if(tmp->stat == 1){
mother->stat = 1;
return;
}
tree(tmp,lvl+1,i,j,vo1,vo2,children);
}
return;
}
}
}
return;
}
void debugg(Node *a,int lvl){
int i = 0,j;
if(a==NULL){
return;
}
else{
for(j=lvl;j>0;j--) printf(" ");
printf("Level: %d\n", lvl);
for(j=lvl;j>0;j--) printf(" ");
printf("opt: %c\n",a->opt);
for(j=lvl;j>0;j--) printf(" ");
printf("j1: %d\n",a->j1);
for(j=lvl;j>0;j--) printf(" ");
printf("j2: %d\n",a->j2);
for(j=lvl;j>0;j--) printf(" ");
printf("stat: %d\n",a->stat);
if(a->child[0] != NULL){
for(j=lvl;j>0;j--) printf(" ");
printf("son of opt: %c\n",a->opt);
}
while(i<3){
debugg(a->child[i], lvl+1);
i++;
}
}
}
void exibir(Node *arv, int nivel) {
int i, n;
for(n = nivel; n>0; n--) printf("\t");
if(arv != NULL){
if (arv->stat == 1) {
printf("Opcao: %c\n", arv->opt);
printf("Jarra 1:%d litros\n", arv->j1);
printf("Jarra 2:%d litros\n", arv->j2);
for (i = 0; i < 3; i++) exibir(arv->child[i], nivel+1);
}
else {
printf("-\n");
}
}
return;
}
void de_alloc(Node *root) {
int i;
if (root != NULL) {
for (i = 0; i < 3; i++)
de_alloc(root->child[i]);
free(root);
}
}
int main(){
Node *root = NULL;
int i;
set_root(&root);
for(i=0;i<2;i++)
tree(root,1,0,0,0,0,i);
debugg(root,0);
exibir(root,0);
de_alloc(root);
printf("no seg fault\n");
return 0;
}
Level: 0
opt: R
j1: 0
j2: 0
stat: 1
son of opt: R
Level: 1
opt: a
j1: 4
j2: 0
stat: 0
son of opt: a
Level: 2
opt: b
j1: 4
j2: 3
stat: 0
son of opt: b
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 2
opt: b
j1: 4
j2: 3
stat: 0
son of opt: b
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 2
opt: b
j1: 4
j2: 3
stat: 0
son of opt: b
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 3
opt: c
j1: 0
j2: 3
stat: 0
son of opt: c
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 4
opt: d
j1: 0
j2: 0
stat: 0
Level: 1
opt: b
j1: 0
j2: 3
stat: 0
son of opt: b
Level: 2
opt: a
j1: 4
j2: 3
stat: 0
son of opt: a
Level: 3
opt: d
j1: 4
j2: 0
stat: 0
son of opt: d
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 3
opt: d
j1: 4
j2: 0
stat: 0
son of opt: d
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 3
opt: d
j1: 4
j2: 0
stat: 0
son of opt: d
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 4
opt: c
j1: 0
j2: 0
stat: 0
Level: 2
opt: f
j1: 3
j2: 0
stat: 0
son of opt: f
Level: 3
opt: a
j1: 4
j2: 0
stat: 0
son of opt: a
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 3
opt: b
j1: 3
j2: 3
stat: 0
son of opt: b
Level: 4
opt: a
j1: 4
j2: 3
stat: 0
Level: 4
opt: c
j1: 0
j2: 3
stat: 0
Level: 4
opt: c
j1: 0
j2: 3
stat: 0
Level: 3
opt: c
j1: 0
j2: 0
stat: 0
son of opt: c
Level: 4
opt: a
j1: 4
j2: 0
stat: 0
Level: 4
opt: b
j1: 0
j2: 3
stat: 0
Level: 2
opt: f
j1: 3
j2: 0
stat: 0
son of opt: f
Level: 3
opt: a
j1: 4
j2: 0
stat: 0
son of opt: a
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 4
opt: b
j1: 4
j2: 3
stat: 0
Level: 3
opt: b
j1: 3
j2: 3
stat: 0
son of opt: b
Level: 4
opt: a
j1: 4
j2: 3
stat: 0
Level: 4
opt: c
j1: 0
j2: 3
stat: 0
Level: 4
opt: c
j1: 0
j2: 3
stat: 0
Level: 3
opt: c
j1: 0
j2: 0
stat: 0
son of opt: c
Level: 4
opt: a
j1: 4
j2: 0
stat: 0
Level: 4
opt: b
j1: 0
j2: 3
stat: 0
Opcao: R
Jarra 1:0 litros
Jarra 2:0 litros
-
-
no seg fault