Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

farmersckn

BinaryTree - Width

This topic is 6515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it *possible*, using only recursion, no arrays, just self-contained function, to find the width of a binary tree? i.e. the largest number of nodes on any level in the tree. thanks farmersckn

Share this post


Link to post
Share on other sites
Advertisement
Depends if you can live with a static array in the function. Or possibly a static pointer to a dynamic array. Yah, that''s it.

Using that, you could create the array on first entry to the function, and the array would contain a count of the number of nodes at each level of the tree. Reset the elements to zero, and recurse down the tree. You''d have to pass the number of the last level traversed. On each pass, increment the value in the array associated with the current array. Then at the end do a max search of the array.

Ok, so I can write code better than explaining it. Ever find you think in code?

Let me know if you follow. If not, I''ll post some psuedo-code.

Morbo

Share this post


Link to post
Share on other sites
Yes, it''s possible. But it''s ugly. It would be easier to just use some dynamic data structures. But, if you wanted to do it using only recursion''s implicit stack, it would look like this:

  

int TreeWidth(Node *p, int targetDepth, int currrentDepth)
{
if (targetDepth == currentDepth) return 1;

int result = 0;
if (p->left) result += TreeWidth(p->left, targetDepth, currentDepth + 1);
if (p->right) result += TreeWidth(p->right, targetDepth, currentDepth + 1);
return result;
}



Isn''t that ugly? Anyway, a call to TreeWidth(root, whateverDepth, 0) will return the correct value, assuming you''ve defined the root to have depth 0.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by farmersckn

Is it *possible*, using only recursion, no arrays, just self-contained function, to find the width of a binary tree? i.e. the largest number of nodes on any level in the tree.
thanks
farmersckn



Keep track of it while you''re BUILDING the tree...




Share this post


Link to post
Share on other sites
Hmmm... This was a kind of interesting question. I''ve been thinking about it some more, and I''ve realized a couple things. First, I could simplify my code a bit. For instance, this would work, too:

  
int TreeWidth(Node *p, int depth)
{
if (depth == 0) return 1;

int result = 0;
if (p->left) result += TreeWidth(p->left, depth - 1);
if (p->right) result += TreeWidth(p->right, depth - 1);

return result;
}


If you want to treat the root as depth 1 instead, you just change the value in the first if statement.

Second, this approach might not be as bad as I initially thought. Specifically, this approach uses O(depth) memory, where as listing the elements at each level requires O(2 ^ depth) memory, which is FAR worse. In both approaches, run time is O(n), where n is the number of nodes in the tree.

Also, as opposed to methods using static data to track between recursive calls, this method is thread-safe (So long as the tree is at least read-locked). I suppose that''s not typically a concern in a gaming environment, but I program in other environments as well, where that can be an important factor.

This isn''t a bad approach. If the tree is highly dynamic, it might even be the best. But if the tree is relatively static, and this info is needed often, the AP''s suggestion is the best bet.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!