# 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.

## 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 on other sites
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 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 IfEnd Sub

##### 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 on other sites
Quote:
 Original post by dmatterDoesn'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 on other sites
Quote:
 Original post by ZahlmanLooks 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 on other sites
Quote:
 Original post by Alpha_ProgDesIf 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 on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by Alpha_ProgDesIf 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 Dictionaryany 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 timea hinged switch that can assume either of two positions

what am i missing here....?

##### Share on other sites
Quote:
 Original post by ZahlmanLooks 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 on other sites
The JIT compiler will do tail recursion under certain circumstances, but depending on this behavior is asking for trouble.

1. 1
2. 2
frob
20
3. 3
Rutin
17
4. 4
5. 5

• 9
• 13
• 10
• 9
• 18
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007036

×