Hi everyone.
In this post I'm gonna show you just the definition of a function that I can't understand.
Here we have the exercise.....
Given a binary search tree:
define a recursive function which makes a simmetric visit of the tree and which prints the value of every node.
Two notes:
Note number one: simmetric visit is that kind of visit which shows in order:
1) Left sons;
2) Root;
3) Right sons.
Note number two: every node of the tree is a structured data.
Code:
struct btree{
float value;
struct btree * left_ptr;
struct btree * right_ptr;
}
Here we have the definition of the function that I'm not comprehending.
Code:
void visit_r(struct btree * ptr){
/*function which takes in input a pointer to struct btree*/
if (ptr != NULL) { /*if the pointer points to something*/
printf("I search the left son... ");
visit_r(ptr->left_ptr);
/*I can't understand what happens here. I re-use my
function with a new input, but I will give a new input
until pointer==NULL.
When pointer will be == NULL, I will not
enter in the "if cycle", and I will not be able
to print the value of my nodes!*/
printf("\n I print the value: %f \n", ptr->value);
printf("I search the right son... ");
visit_r(ptr->right_ptr);
} printf("Let's go back... ");
/*I can't understand neither how it can go back,
how it can 'climb' the tree*/
}
when it reaches line n°10 it starts again from line n°1 with a different input.
But I still don't understand how it can work,
because it will do this recursive path
until 'ptr->left_ptr' will be' == NULL'.
But when 'ptr->left_ptr' will be == 'NULL', I will not enter in the cycle "if(...)" and I will not be able to print the value of my nodes!
P.s. The code is written by a university teacher.
Thanks everyone!!