Is this a tail-recursive method?

Started by
11 comments, last by Zahlman 13 years, 8 months ago
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


Beginner in Game Development?  Read here. And read here.

 

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.
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 IfEnd Sub
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).
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?

Beginner in Game Development?  Read here. And read here.

 

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?

Beginner in Game Development?  Read here. And read here.

 

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: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....?

Beginner in Game Development?  Read here. And read here.

 

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.
The JIT compiler will do tail recursion under certain circumstances, but depending on this behavior is asking for trouble.

This topic is closed to new replies.

Advertisement