This is more of a general question so the answer does not have to be in C#. I have a general code structure below that I use to recursively walk a tree structure. In this particular case I am appending to a string builder object in each iteration.
Is there a way to improve this code to be faster or better?
public static void RecursiveFunction(StringBuilder sb, Object treeObject)
{
sb.Append(treeObject.GetCurrentValue("Name"));
if (treeObject.MoveToFirstChild())
{
RecursiveFunction(sb, treeObject);
}
if (treeObject.MoveToNextSibling())
{
RecursiveFunction(sb, treeObject);
}
if (treeObject.MoveToParent())
{
if (treeObject.MoveToNextSibling())
{
RecursiveFunction(sb, treeObject);
}
}
return;
}
Thanks.