Hi guys, below there is a c code which caluclates given data repetition in a binary tree.
The problem is I don't understand how it works. The all recursive and return process confused me. For example; what happens when we track the code on the tree attached(for data 'a'). If anyone can explain me step by step, I would appreciate.
Code:
typedef struct node *tree_pointer;
typedef struct node{
char data;
tree pointer left_child, right_child;
}
int repetition(tree_pointer L, char x){
if(L){
if(L->data == x){
return 1 + repetition(L->left_child, x) + repetition(L->right_child, x);
}
else{
return repetition(L->left_child, x) + repetition(L->right_child, x);
}
else{
return 0;
}
}
Comment