BinaryTree - Width

Started by
3 comments, last by farmersckn 23 years, 3 months ago
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
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
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
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.
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...




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.

This topic is closed to new replies.

Advertisement