private void AddChildren(TreeNode node, int maxDepth) { for (int i = 0; i < node.ChildTypes.Length; i++) { Type type = node.ChildTypes[i]; TreeNode child; do { var filter = node.GetChildSelectionFilter(i); if (maxDepth <= 1) { child = SelectTerminalNode(type, filter); } else { child = SelectNonTerminalNode(type, Math.Max(1, maxDepth), filter); } if (child == null) // no valid child found due to filtering { child = SelectAnyNode(type, Math.Max(1, maxDepth), filter); } if (maxDepth == 0 && child.ChildTypes.Length > 0) // array index must be a single terminal if already on max depth { continue; } if (node.AcceptChild(child, i)) { break; } } while (true); node.AddChild(child); AddChildren(child, maxDepth - 1); } }