I have the following tree
A
+-B
+-C
| +-D
+-E
+-F
+-G
I am trying to find G
given A
private TreeListNode FindTreeNode(TreeListNode node, Enumerations.ItemType type,
Nullable<long> id)
{
TreeListNode found = null;
foreach (TreeListNode child in node.Nodes)
{
if ((Enumerations.ItemType)child[2] == type)
{
if (id == null)
{
found = child;
break;
}
else
{
if ((long)child[0] == (long)id)
{
found = child;
break;
}
}
}
else
{
if (child.HasChildren)
{
found = FindTreeNode(child, type, id);
break;
}
}
}
return found;
}
FindTreeNode(root,C,null)
Since C
comes before G
the routinue works for finding C
and its children.
if the else block if(child.HasChildren)
it finds C
and its children.
When I am trying to find E
and its children,
the recursive call isn't working correctly.
It comes in with the root node A
but after it enters the recursion child becomes C
and then nodes.Nodes = 1
When I searching for F
or G
it has to continue its recursion. So how do I set the child to be the root node