I am not able to understand the recursion of functions. How it works? how the values are stored and all?
int tree_size(struct node* node) {
if (node==NULL) {
consider the follow three
1 2 3 4 5 6 7
1 has two children (2 and 3)
2 has two children (4 and 5)
..
4 has one child (7)
and you want to know the size of the tree at 1:
tree_size(tree1);
because tree1 is not NULL the if-condition not true, so the else statement will be executed:
tree_size(tree1): return tree_size( tree_size(tree2) + tree_size(tree3) + 1 )
same for tree2 and tree3
tree_size(tree2): return tree_size( tree_size(tree4) + tree_size(tree5) + 1 )
tree_size(tree3): return tree_size( tree_size(tree6) + tree_size(NULL) + 1 )
and so on. Now if we substituate the return value of tree_size(tree2) and tree_size(tree3) in tree_size(tree1) we would get:
tree_size(tree1): return tree_size( tree_size(tree4) + tree_size(tree5) + 1 + tree_size(tree6) + tree_size(NULL) + 1 + 1 )
Now you can see the term 1+1+1, this is the size of the tree for the first two niveau's, if we keep substituating the tree_size calls, w'll get an some of n times 1, with n the size of the tree