if i have a struct define in main for example
Code:
struct node
{
int key_value;
struct node *left;
struct node *right;
};
insert(int key, struct node **leaf)
{
if( *leaf == 0 )
{
*leaf = (struct node*) malloc( sizeof( struct node ) );
(*leaf)->key_value = key;
/* initialize the children to null */
(*leaf)->left->left = 0;
(*leaf)->left->right = 0;
}
else if(key < (*leaf)->key_value)
{
insert( key, &(*leaf)->left );
}
else if(key > (*leaf)->key_value)
{
insert( key, &(*leaf)->right );
}
}
Because i use the double pointer if i was to write a print or search function would i need to pass a double pointer? I know it works if i do but want to be sure is this the only way? Here is example of the rest of my code
Code:
void print(struct node **leaf)
{
printf("here");
printf("are we here %d\n", (*leaf)->key_value);
if(leaf!=NULL)
{
//printf("Value of leaf in print is %d", leaf->key_value);
//print(&(*leaf)->left);
//print(&(*leaf)->right);
}
}
struct node *search(int key, struct node **leaf)
{
if( leaf != 0 )
{
if(key==(*leaf)->key_value)
{
return (*leaf);
}
else if(key<(*leaf)->key_value)
{
return search(key, (*leaf)->left);
}
else
{
return search(key, (*leaf)->right);
}
}
else return 0;
}
Thanks