-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Description
Current if you want write a recursive based iterator function.
Iterator Function TreeWalk(Of T) ( curr As BinaryNode(Of T)) : IEnumerable(Of BinaryNode(Of T))
If curr Is Nothing Then Return Enumerable.Empty(Of BinaryNode(Of T))
ForEach node In TreeWalk( curr.Left )
Yield node
Next
Yield curr
ForEach node In TreeWalk( curr.Righ )
Yield node
Next
End Function
it ends up being Quadratic runtime
If I could express the Iterator / Yielder as a parameter I could linearise the runtime.
Iteration Function TreeWalk( n : BinaryNode, iterator As ?? ) : IEnumerable(Of T)
If n Is Nothing Then Exit Functon
If n.Left IsNot Nothing Then TreeWalk(n.Left, Iterator)
Yield n.Value on Iterator
If n.Righ IsNot Nothing Then TreeWalk(n.Righ, Iterator)
End Function
Unknown6656, exyi, jrmoreno1, svick, eamodio and 2 more