whereCode:printf("%s \n",treePtr->node->dedomena.name);
if (Tree_keno(treePtr)) { print("2\n");}
OutputCode:int Tree_keno(structPtr treePtr)
{
return (treePtr->node==NULL);
}
Can anyone figure out whats wrong?Code:Kristina
2
Printable View
whereCode:printf("%s \n",treePtr->node->dedomena.name);
if (Tree_keno(treePtr)) { print("2\n");}
OutputCode:int Tree_keno(structPtr treePtr)
{
return (treePtr->node==NULL);
}
Can anyone figure out whats wrong?Code:Kristina
2
I'm terribly sorry, but my mind reading machine is in the shop...
You need to show us at least the part of the program involved in the problem... How you're allocating your pointers and structs, the definition of the struct itself ... anything that might contribute to the error.
I am sorry but i didn't in order not to tire you..
Definitions
CreateCode:typedef struct TStoixeioyTree{/*TStoixeioyTree.h*/
char* name;
char* surname;
}TStoixeioyTree;
typedef struct typos_komvou * typos_deikti;/*ch8_BSTpointer.h*/
typedef struct typos_komvou{ /*ch8_BSTpointer.c*/
TStoixeioyTree dedomena;
typos_deikti apaidi;
typos_deikti dpaidi;
} typos_komvou;
struct tree_struct{/* den exw idea pou pane ta gamidia */
typos_deikti node;
}tree_struct;
typedef struct tree_struct *structPtr;
InsertCode:structPtr Tree_dimiourgia()
{
structPtr tree;
tree=malloc(sizeof(structPtr));
tree->node=NULL;
return tree;
}
Code:void Tree_eisagogi(structPtr treePtr , TStoixeioyTree stoixeio, int *error)
{
typos_deikti origin,temp;
origin=treePtr->node;
if(Tree_keno(treePtr))
{
temp=malloc(sizeof(typos_komvou));
if(temp==NULL)
{
*error=1;
return;
}
treePtr->node=temp;
treePtr->node->dedomena.surname=malloc(strlen(stoixeio.surname)+1 );
treePtr->node->dedomena.name=malloc(strlen(stoixeio.name)+1 );
TStree_setValues(&((treePtr)->node->dedomena), stoixeio);
treePtr->node->apaidi=NULL;
treePtr->node->dpaidi=NULL;
}
else if(TStree_mikrotero(stoixeio, treePtr->node->dedomena))
{
treePtr->node=treePtr->node->apaidi;
Tree_eisagogi(treePtr , stoixeio, error);
}
else if(TStree_megalytero(stoixeio, treePtr->node->dedomena))
{
treePtr->node=treePtr->node->dpaidi;
Tree_eisagogi(treePtr , stoixeio, error);
}
else if(TStree_iso(stoixeio, treePtr->node->dedomena))
/* These two lines of code are mine.Without this two,if we had a name twice then function would set error=1*/
return;
else
*error=1;
if(i!=1) /* i is a global var.*/
treePtr->node=origin;
}
Empty treeCode:void nodesprint(structPtr treePtr, FILE *fp) /* Print tree nodes */
{
typos_deikti origin;
origin=treePtr->node;
if (Tree_keno(treePtr)) { /* If tree is not empty */
treePtr->node=treePtr->node->apaidi;
nodesprint(treePtr,fp); /* Print left subtree */
fprintf(fp,"%s %s\n", treePtr->node->dedomena.surname , treePtr->node->dedomena.name);/* Print root node */
treePtr->node=treePtr->node->dpaidi;
nodesprint(treePtr,fp); /* Print right subtree */
}
treePtr->node=origin;
}
Code:int Tree_keno(structPtr treePtr)
{
return (treePtr->node==NULL);
}
Probably not directly related, but:
Not going to work. You need to allocate enough memory for a whole entire struct, not just a pointer to struct.Code:tree=malloc(sizeof(structPtr));
Your comments about Tree_keno and the code don't seem to match; you write "If tree is not empty" next to "if (Tree_keno(treePtr))", but that's not the way that test works. I would guess your trashing your tree on a regular basis.