Name: cyan 2008-07-10 14:49
ok, to calculate the balancefactor of a binary tree you have to subtract the height of the right subtree from the left subtree...
normally you do something like this:
//tree structure
struct node{
int val;
struct node * left, * right;
};
struct node * root = NULL;
//Inserting some numbers
void insert(int a[]){
blablabla;
}
//function returning the height of the tree with root p)
int height(struct node *p){
blablabla;
}
int balancefactor(struct node *p){
if(!p) return 0;
return height(p->right)-height(p->left);
}
now check this out:
i have to write a recursive function "balancefactor2" that calculates the balancefactor without calling another function.
How to do it /prog/?
normally you do something like this:
//tree structure
struct node{
int val;
struct node * left, * right;
};
struct node * root = NULL;
//Inserting some numbers
void insert(int a[]){
blablabla;
}
//function returning the height of the tree with root p)
int height(struct node *p){
blablabla;
}
int balancefactor(struct node *p){
if(!p) return 0;
return height(p->right)-height(p->left);
}
now check this out:
i have to write a recursive function "balancefactor2" that calculates the balancefactor without calling another function.
How to do it /prog/?