Re: no :of nodes
On 7 May, 19:16, sophia <sophia.ag...@gmail.com> wrote:
> Dear all,
>
> How good is this function which is used to find the no: of nodes
> in each level of the binary tree. ?
>
> int arrlev[100];
>
> void preorder(struct btree *n,int lev)
> {
> if(!(n))
> return;
>
> arrlev[lev]++;
> lev++;
>
> preorder(n->left,lev);
> preorder(n->right,lev);
>
> }
Apart from the comments made by others I note also
that the way it is written it will only count the nodes
starting from lev0 and above where lev0 is the value
it is initially passed as a second argument. I assume
the intention is to call the function with a second
argument of 0 but it might be clearer to write it like
this:
int arrlev[100];
void preorder(struct btree *n) {
preorder_aux(n , 0) ;
}
void preorder_aux(struct btree *n,int lev)
{
if(!(n))
return;
arrlev[lev]++;
lev++;
preorder(n->left,lev);
preorder(n->right,lev);
}
I don't think the name "preorder" is very appropriate
because one would use it in cases where one might have
doubts that the preorder is actually an order. But a tree
is actually an order. Best to call your function
count_level_nodes or something.
|