private static ISet <ITreeProgram <TOutput> > GetSubCombs <TOutput>(ITreeProgram <TOutput> program) { var combs = new HashSet <ITreeProgram <TOutput> >(); if (program == null) { return(combs); } // checks no more children if (program.IsLeaf()) { combs.Add(program); return(combs); } // gets sub-programs from all children var childrenSubCombs = new List <IEnumerable <ITreeProgram <TOutput> > >(); foreach (var child in program.Input) { var childSubCombs = GetSubCombs(child); childrenSubCombs.Add(childSubCombs); // adds the sub-combinations of children to combination list foreach (var childSubComb in childSubCombs) { combs.Add(childSubComb); } } // creates new programs where each child is replaced by some sub-combination of it var allChildrenCombs = childrenSubCombs.GetAllCombinations(); foreach (var children in allChildrenCombs) { if (children.Count == program.Input.Count) { combs.Add(program.CreateNew(children)); } } childrenSubCombs.Clear(); return(combs); }