Είμαι προσπαθεί να υπολογίσει το ύψος ενός δέντρου. Είμαι doint με τον κώδικα που γράφεται παρακάτω.
#include<iostream.h>
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree tree;
class Tree
{
private:
int n;
int data;
int l,r;
public:
tree * Root;
Tree(int x)
{
n=x;
l=0;
r=0;
Root=NULL;
}
void create();
int height(tree * Height);
};
void Tree::create()
{
//Creting the tree structure
}
int Tree::height(tree * Height)
{
if(Height->left==NULL && Height->right==NULL)
{return 0;
}
else
{
l=height(Height->left);
r=height(Height->right);
if (l>r)
{l=l+1;
return l;
}
else
{
r=r+1;
return r;
}
}
}
int main()
{
Tree A(10);//Initializing 10 node Tree object
A.create();//Creating a 10 node tree
cout<<The height of tree<<A.height(A.Root);*/
}
Μου δίνει Corret αποτέλεσμα. Αλλά σε ορισμένες θέσεις (στο google σελίδα) Προτάθηκε να κάνει μια διάσχιση Postorder και χρησιμοποιούν αυτή τη μέθοδο ύψος για τον υπολογισμό του ύψους. Κάθε συγκεκριμένος λόγος;













