Discussion: no :of nodes
Afficher un message
Vieux 08/05/2008, 02h22   #6
Chris Thomasson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: no :of nodes

"Chris Thomasson" <cristom@comcast.net> wrote in message
news:vLGdndQrSbqk2L_VnZ2dnUVZ_rjinZ2d@comcast.com. ..
> "sophia" <sophia.agnes@gmail.com> wrote in message
> news:7521541f-9a67-4239-93d5-1452d442314c@h1g2000prh.googlegroups.com...
>> 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);
>> }

>
> Its no good. Use dynamic allocation if the level count of the tree is not
> known in advance. Also, recursion is not safe. Passing some tress to this
> function will blow the stack. There are ways to traverse a tree without
> using recursion. Here is a simple example:
>
> http://pastebin.com/m7ffa217c


ARGH!!!!

Forget that code!

Look at this one instead!

http://pastebin.com/m3e5a9fd8


Sorry about the first one. I made forgot to call free!!!!!!!

;^(...



> Instead of threading the tree, you can include an auxiliary node that a
> traversal function uses as an node to link into a local queue. Check the
> 'destroy_tree()' function...


  Réponse avec citation
 
Page generated in 0,06498 seconds with 9 queries