Utils. Recursive Helper

RecursiveHelper class implements the main operations for recursive processing of hierarchical data. Each developer now and then faces the tasks that require the use of recursive algorithms. Processing tree-structured (or hierarchical) data is one most typical example of such tasks.

During implementation of such algorithm, the developer needs to implement, at least, one or, sometimes, a number of supplementary methods.

The following example shows a standard implementation of the recursive tree parsing algorithm:

  • c#
  • VB

public class Class1
{
  public int Int1 { get; set; }
  public List<Class1> Children { get; set; }
}
//…
void GoRound(Class1 obj)
{
  Console.WriteLine(obj.Int1);
  if (obj.Children != null)
    foreach (var class1 in obj.Children)
      GoRound(class1);
}
//…
void smth()
{
  GoRound(data);
}
//…

Public Class Class1
  Public Property Int1 As Integer
  Public Property Children As List(Of Class1)
End Class
'…
Private Sub GoRound(ByVal obj As Class1)
  Console.WriteLine(obj.Int1)
  If obj.Children IsNot Nothing Then
    For Each class1 In obj.Children
      GoRound(class1)
    Next
  End If
End Sub
'…
Private Sub smth()
  GoRound(data)
End Sub
'…

The same task can be solved using the RecursiveHelper as shown below:

  • c#
  • VB

public void smth()
{
  RecursiveHelper.Recursive(data, a => a.Children, a => Console.WriteLine(a.Int1));
}

Public Sub smth()
  RecursiveHelper.Recursive(data, Function(ByVal a) a.Children, Function(ByVal a) Console.WriteLine(a.Int1))
End Sub

or an alternative using an extension

  • c#
  • VB

public void smth()
{
  data.Recursive(a => a.Children, a => Console.WriteLine(a.Int1));
}

Public Sub smth()
  data.Recursive(Function(ByVal a) a.Children, Function(ByVal a) Console.WriteLine(a.Int1))
End Sub

RecursiveHelper implements methods for:

  • Recursive data processing
  • Obtaining the linear list of objects of the hierarchy
  • Searching the element by the path
  • Searching the element based on the condition
  • Processing data implementing the ITreeNode interface

To learn more about the RecursiveHelper, refer to the following documents:

Code reference