Ok people, I understand stacks and heaps, but when it comes to recursivity and binary trees, i have a major problem trying to understand this.
if someone peopl explain me, how this recur. stuff works, I would be happy!
For example, where the value goes, wich function is called and processed etc.
I'll leave here a simple example code with a few questions, thanks!
int BT::sumleafs(){
int s=0;
if(root==NULL)
cout<<"empty!\n";
else
s+=sumleafs(root); //1)IT SUMS WHAT?
cout<<"total: "<<s<<endl;
return 0;
}
int BT::sumleafs(NoTree *Ptree){
int s=0;
if(Ptree->right==NULL && Ptree->left==NULL){//sums the leafs if it doesn't has sons
s+= Ptree->value;
return s;
}
s+= sumleafs(Ptree->left); //2)what is this?It calls the same function, ok
//but if he is always going to the left, how its
//supose to go back to check the rest of the elements
//And what the s+= sums anyway? a node?
s+= sumleafs(Ptree->right);
return s;
}