fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. static class Class1
  8. {
  9. private static IEnumerable<TNode> GetChildren<TNode>(
  10. TNode node,
  11. Func<TNode, IEnumerable<TNode>> GetNodes)
  12. {
  13. var nodes = GetNodes(node);
  14. return nodes.Concat(nodes.SelectMany(x => GetChildren(x, GetNodes)));
  15. }
  16.  
  17. public static IEnumerable<TNode> GetChildrenRecursively<TRoot, TNode>(
  18. this TRoot obj,
  19. Func<TRoot, IEnumerable<TNode>> EnumRoot,
  20. Func<TNode, IEnumerable<TNode>> GetNodes)
  21. {
  22. var nodes = EnumRoot(obj);
  23. return nodes.Concat(nodes.SelectMany(x => GetChildren(x, GetNodes)));
  24. }
  25. }
  26.  
  27. class Program
  28. {
  29. static void Main(string[] args)
  30. {
  31. int[] arr = { 1, 2, 3, 4 };
  32. var query = arr.GetChildrenRecursively<IEnumerable<int>, IEnumerable<int>>(x => arr.Select(y => new int[] { y }), x => arr.Except(x).Select(y => x.Concat(new int[] { y }.ToArray())));
  33. foreach (var item in query)
  34. {
  35. foreach (var i in item) Console.Write(i + " ");
  36. Console.WriteLine();
  37. }
  38. }
  39. }
  40. }
  41.  
Success #stdin #stdout 0.04s 16292KB
stdin
Standard input is empty
stdout
1 
2 
3 
4 
1 2 
1 3 
1 4 
1 2 3 
1 2 4 
1 2 3 4 
1 2 4 3 
1 3 2 
1 3 4 
1 3 2 4 
1 3 4 2 
1 4 2 
1 4 3 
1 4 2 3 
1 4 3 2 
2 1 
2 3 
2 4 
2 1 3 
2 1 4 
2 1 3 4 
2 1 4 3 
2 3 1 
2 3 4 
2 3 1 4 
2 3 4 1 
2 4 1 
2 4 3 
2 4 1 3 
2 4 3 1 
3 1 
3 2 
3 4 
3 1 2 
3 1 4 
3 1 2 4 
3 1 4 2 
3 2 1 
3 2 4 
3 2 1 4 
3 2 4 1 
3 4 1 
3 4 2 
3 4 1 2 
3 4 2 1 
4 1 
4 2 
4 3 
4 1 2 
4 1 3 
4 1 2 3 
4 1 3 2 
4 2 1 
4 2 3 
4 2 1 3 
4 2 3 1 
4 3 1 
4 3 2 
4 3 1 2 
4 3 2 1