I have a tree class to represent oriented trees (No binaries). The nodes are represented by a cell, which has the method lchild () that gives us the most left and right () son or simply increasing (as in list) to get his brother who follows him. Here I leave code to list previous order, to take into account.
void oprev(tree<int> &T, tree<int>::iterator p){
tree<int>::iterator q;
if (p == T.end()){
return;
}
cout<<*p<<" ";
q = p.lchild();
while( q != T.end() ){
oprev (T, q++);
}
void oprev(tree<int> &T){
oprev(T,T.begin());}
I need to calucar the height of a tree. I have this code but it is not correct
int altura(tree<int> &T,tree<int>::iterator p, int &x){
tree<int>::iterator q;
if (p == T.end()){
return x;
}
q = p.lchild();
while( q != T.end() ){
altura(T, q++,x);
}
if(p.lchild()!=T.end()){
x++;
}
return x;}
Having this tree.
As height I get 5.
I recognize that the error is in that the counter, sometimes adds back when it passes through levels that previously has already passed. I do not know how I can avoid this. Thank you very much. Greetings