Nov 19, 2015 at 12:26am Nov 19, 2015 at 12:26am UTC
In the following procedure, I don't seem to understand how to specify the
1 2
ptr_to_tree -> ptr_to_left
ptr_to_tree -> ptr_to_right
parameters
1 2 3 4 5 6 7 8 9
void Add(node ** ptr_to_tree, node * theNewNode) {
if (*ptr_to_tree == 0)
*ptr_to_tree = theNewNode;
else
if (theNewNode -> i < (*ptr_to_tree) -> i)
return Add( &ptr_to_tree -> ptr_to_left, theNewNode); // ptr_to_tree
else
return Add(ptr_to_tree -> ptr_to_right, theNewNode); // ptr_to_tree
}
I call Add successfully (from main) with
1 2 3 4 5
node * a_new_node = (node *)malloc( sizeof (node) );
...
node * ptr_to_tree = 0;
...
Add(&ptr_to_tree, a_new_node);
Can someone kindly show me how
ptr_to_tree -> ptr_to_right
should be specified in
return Add(ptr_to_tree -> ptr_to_right, theNewNode); // ptr_to_tree
Thank you!
Last edited on Nov 19, 2015 at 12:27am Nov 19, 2015 at 12:27am UTC
Nov 19, 2015 at 4:21am Nov 19, 2015 at 4:21am UTC
You got it right in the if statement on line 5:
(*ptr_to_tree) -> i
deref the ** once, then again with -> to get the 'data' member.
Similarly, to obtain the 'pointer' members:
(*ptr_to_tree) -> ptr_to_right
Pass the address of this pointer to Add:
return Add( &(*ptr_to_tree) -> ptr_to_right, theNewNode);