fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var tree = new List<Data>
  10. {
  11. new Data
  12. {
  13. Id = 1,
  14. Items = new List<Data>
  15. {
  16. new Data {Id = 11},
  17. new Data {Id = 12},
  18. }
  19. },
  20. new Data
  21. {
  22. Id = 2,
  23. Items = new List<Data>
  24. {
  25. new Data {Id = 21},
  26. new Data {Id = 22},
  27. }
  28. },
  29. };
  30. var selected = new Data();
  31.  
  32. var output =
  33. (Action<string>) (Console.WriteLine);
  34. //(Action<string>) (Debug.WriteLine);
  35.  
  36. var comparator =
  37. (Func<Data, Data, bool>) ((x, sel) => true);
  38.  
  39. // ReSharper disable SpecifyACultureInStringConversionExplicitly
  40. // не использую при конверсии .ToString(CultureInfo.InvariantCulture)
  41. // т.к. данные выводятся пользователю
  42. var action =
  43. (Action<List<Data>, Data>) ((tr, x) => output(x.Id.ToString()));
  44. // ReSharper restore SpecifyACultureInStringConversionExplicitly
  45.  
  46. Console.WriteLine("Calling Depth-first Pre-order Recursive variant");
  47. new Traverser().DepthFirstPreOrderRecursive(tree, selected, comparator, action);
  48.  
  49. Console.WriteLine("Calling Depth-First reversed-Pre-Order on-Stack variant");
  50. new Traverser().DepthFirstPreOrderStack(tree, selected, comparator, action);
  51.  
  52. Console.WriteLine("Calling Breadth-first on-Queue variant");
  53. new Traverser().BreadthFirstQueue(tree, selected, comparator, action);
  54. }
  55. }
  56.  
  57. public class Traverser
  58. {
  59. public void DepthFirstPreOrderRecursive(List<Data> items, Data selected, Func<Data, Data, bool> comparator, Action<List<Data>, Data> action)
  60. {
  61. foreach (var x in items)
  62. {
  63. if (comparator(x, selected))
  64. {
  65. action(items, x);
  66. }
  67.  
  68. DepthFirstPreOrderRecursive(x.Items, selected, comparator, action);
  69. }
  70. }
  71.  
  72. public void DepthFirstPreOrderStack(List<Data> items, Data selected, Func<Data, Data, bool> comparator, Action<List<Data>, Data> action)
  73. {
  74. var stack = new Stack<Data>(items);
  75. while (stack.Count != 0)
  76. {
  77. var x = stack.Pop();
  78.  
  79. if (comparator(x, selected))
  80. {
  81. action(items, x);
  82. }
  83.  
  84. foreach (var child in x.Items)
  85. {
  86. stack.Push(child);
  87. }
  88. }
  89. }
  90.  
  91. public void BreadthFirstQueue(List<Data> items, Data selected, Func<Data, Data, bool> comparator, Action<List<Data>, Data> action)
  92. {
  93. var queue = new Queue<Data>(items);
  94. while (queue.Count != 0)
  95. {
  96. var x = queue.Dequeue();
  97.  
  98. if (comparator(x, selected))
  99. {
  100. action(items, x);
  101. }
  102.  
  103. foreach (var child in x.Items)
  104. {
  105. queue.Enqueue(child);
  106. }
  107. }
  108. }
  109. }
  110.  
  111. public class Data
  112. {
  113. public int Id { get; set; }
  114. public List<Data> Items { get; set; }
  115. public Data()
  116. {
  117. Items = new List<Data>();
  118. }
  119. }
Success #stdin #stdout 0.04s 24328KB
stdin
Standard input is empty
stdout
Calling Depth-first Pre-order Recursive variant
1
11
12
2
21
22
Calling Depth-First reversed-Pre-Order on-Stack variant
2
22
21
1
12
11
Calling Breadth-first on-Queue variant
1
2
11
12
21
22