Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Is this a tail-recursive method?

This topic is 2946 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

I'm thinking no. Is there a way, to make tail-recursive or better?


Protected Sub toggleAllChildren(ByVal node As TreeNode)

If node.Checked = True Then
If node.ChildNodes.Count = 0 Then
node.Checked = True
Else
For Each childNode As TreeNode In node.ChildNodes
childNode.Checked = True
toggleAllChildren(childNode)
Next
End If

Else
If node.ChildNodes.Count = 0 Then
node.Checked = False
Else
For Each childNode As TreeNode In node.ChildNodes
childNode.Checked = False
toggleAllChildren(childNode)
Next
End If
End If
End Sub


Share this post


Link to post
Share on other sites
Advertisement
No, not tail recursive; the recursive calls occur inside loops. Making it tail recursive would require such a radical restructuring of the function that you might as well make it non-recursive.

Share this post


Link to post
Share on other sites
Doesn't this do the same thing? Have I missed something?

Protected Sub toggleAllChildren(ByVal node As TreeNode)
If node.ChildNodes.Count > 0 Then
For Each childNode As TreeNode In node.ChildNodes
childNode.Checked = node.Checked
toggleAllChildren(childNode)
Next
End If
End Sub

Share this post


Link to post
Share on other sites
Looks correct to me, dmatter. We shouldn't need to check 'count', either, since iterating over 0 children correctly does nothing. Oh, and this function doesn't "toggle" all children, either; it sets them to match the parent (root, recursively).

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Doesn't this do the same thing? Have I missed something?

*** Source Snippet Removed ***

If I uncheck the parent node, will the child nodes uncheck as well?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Looks correct to me, dmatter. We shouldn't need to check 'count', either, since iterating over 0 children correctly does nothing. Oh, and this function doesn't "toggle" all children, either; it sets them to match the parent (root, recursively).

is mine... correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
If I uncheck the parent node, will the child nodes uncheck as well?


'childNode.checked = node.checked' sure looks to me like it unchecks child nodes when the parent node is unchecked.

Quote:
is mine... correct?


In the sense of doing the same thing as dmatter's, sure. But this word... "toggle"... I do not think it means what you think it means. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Alpha_ProgDes
If I uncheck the parent node, will the child nodes uncheck as well?


'childNode.checked = node.checked' sure looks to me like it unchecks child nodes when the parent node is unchecked.

Quote:
is mine... correct?


In the sense of doing the same thing as dmatter's, sure. But this word... "toggle"... I do not think it means what you think it means. :)

Quote:

from Free Dictionary
any instruction that works first one way and then the other; it turns something on the first time it is used and then turns it off the next time

a hinged switch that can assume either of two positions

what am i missing here....?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Looks correct to me, dmatter. We shouldn't need to check 'count', either, since iterating over 0 children correctly does nothing.
Hehe, quite right too.

Does anyone know whether .net compilers actually use tail recursion as an optimisation anyway? My understanding is that due to the time constraints on compilation this sort of thing might not happen.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!