Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: no :of node...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 18 Topic 26106 of 26955
Post > Topic >>

Re: no :of nodes

by Eric Sosman <Eric.Sosman@[EMAIL PROTECTED] > May 7, 2008 at 02:39 PM

sophia 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);
> }

     "How good" is a slippery question, because there are many
criteria of goodness, some of them in conflict.  So I'll offer
observations, not judgements.

     1) It will fail badly on a tree with more than a hundred
levels.  Do such trees really exist?  Try this: write a program
to build a binary search tree from an input stream of words,
then feed it a list of ten thousand words -- in alphabetical
order.

     2) Even if arrlev[] is made larger, the function is likely
to fail on very tall trees (or degenerate trees, as above).
Remember, each function invocation needs to "remember" where
it was called from so it can return there.  The amount of space
set aside for this bookkeeping is often rather limited, and if
the recursive calls go too deep they may exhaust it.

     3) You might gain some speed by handling the left branch
(say) in a loop while making recursive calls only for the right
branches.

     4) You might gain still more speed by using recursion only
when you encounter a node where both the left and right branches
are non-NULL.  This also offers some defense against degeneracy.

     5) Adding `const' to the first argument might be nice.

     6) Adding some commentary about what the function does and
how to use it might be even nicer.

     7) Passing a pointer to the totals array instead of using
the global variable arrlev[] might be nice.

-- 
Eric.Sosman@[EMAIL PROTECTED]

 




 18 Posts in Topic:
no :of nodes
sophia <sophia.agnes@[  2008-05-07 11:16:16 
Re: no :of nodes
roberson@[EMAIL PROTECTED  2008-05-07 18:30:53 
Re: no :of nodes
Eric Sosman <Eric.Sosm  2008-05-07 14:39:58 
Re: no :of nodes
Thad Smith <ThadSmith@  2008-05-07 21:03:29 
Re: no :of nodes
Keith Thompson <kst-u@  2008-05-07 21:26:12 
Re: no :of nodes
Eric Sosman <esosman@[  2008-05-08 08:36:50 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-08 06:37:01 
Re: no :of nodes
Thad Smith <ThadSmith@  2008-05-16 19:30:16 
Re: no :of nodes
fred.l.kleinschmidt@[EMAI  2008-05-07 15:19:02 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 17:14:15 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 17:22:50 
Re: no :of nodes
cri@[EMAIL PROTECTED] (R  2008-05-08 03:02:20 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 21:11:29 
Re: no :of nodes
cri@[EMAIL PROTECTED] (R  2008-05-08 14:41:40 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-08 07:57:12 
Re: no :of nodes
Spiros Bousbouras <spi  2008-05-08 05:04:02 
Re: no :of nodes
roberson@[EMAIL PROTECTED  2008-05-08 14:53:41 
Re: no :of nodes
Spiros Bousbouras <spi  2008-05-08 08:24:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:27:22 CDT 2008.