fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9. static readonly Random r = new Random(12345);
  10. class Node
  11. {
  12. public int Value;
  13. public Node Left;
  14. public Node Right;
  15.  
  16. public static Node Generate(int i)
  17. {
  18. if (i > 8)
  19. {
  20. return null;
  21. }
  22. var n = new Node();
  23. n.Value = r.Next(0, 99);
  24.  
  25. if (r.NextDouble() < 0.9)
  26. {
  27. n.Left = Generate(i + 1);
  28. }
  29. if (r.NextDouble() < 0.9)
  30. {
  31. n.Right = Generate(i + 1);
  32. }
  33. return n;
  34. }
  35. }
  36.  
  37. enum SearchDirection
  38. {
  39. Left, Right, Done
  40. }
  41.  
  42. static void Main(string[] args)
  43. {
  44. var root = Node.Generate(0);
  45.  
  46. var nodes = new List<Node>();
  47. var dirs = new List<SearchDirection>();
  48.  
  49. nodes.Add(root);
  50. dirs.Add(SearchDirection.Left);
  51. Console.Write("{0:D2}", root.Value);
  52.  
  53. for (; nodes.Count > 0; )
  54. {
  55. var n = nodes.Last();
  56. var d = dirs.Last();
  57.  
  58. if (d == SearchDirection.Left && n.Left != null)
  59. {
  60. Console.Write("--{0:D2}", n.Left.Value);
  61. dirs[dirs.Count - 1] = SearchDirection.Right;
  62.  
  63. nodes.Add(n.Left);
  64. dirs.Add(0);
  65. continue;
  66. }
  67. else if (d == SearchDirection.Right && n.Right != null)
  68. {
  69. Console.WriteLine();
  70. for (var j = 0; j < nodes.Count; j++)
  71. {
  72. if (j == nodes.Count - 1)
  73. {
  74. Console.Write("└--");
  75. }
  76. else if (dirs[j] != SearchDirection.Done && nodes[j].Right != null)
  77. {
  78. Console.Write("│ ");
  79. }
  80. else
  81. {
  82. Console.Write(" ");
  83. }
  84. }
  85.  
  86. Console.Write("{0:D2}", n.Right.Value);
  87. dirs[dirs.Count - 1] = SearchDirection.Done;
  88.  
  89. nodes.Add(n.Right);
  90. dirs.Add(0);
  91. }
  92. else
  93. {
  94. nodes.RemoveAt(nodes.Count - 1);
  95. dirs.RemoveAt(dirs.Count - 1);
  96. }
  97. }
  98.  
  99. Console.ReadKey();
  100. }
  101. }
  102.  
Success #stdin #stdout 0.04s 24288KB
stdin
Standard input is empty
stdout
36--37--35--63--54--96--81--17
│  │      │  │  │  └--37
│  │      │  │  └--35--02--75
│  │      │  │      │  └--46
│  │      │  │      └--62--93
│  │      │  └--08--58--44--52
│  │      │          │  └--88
│  │      │          └--79--46
│  │      │              └--33
│  │      └--20--34--23
│  │          │  └--68--55--41
│  │          │      │  └--02
│  │          │      └--36--42
│  │          │          └--37
│  │          └--61--62
│  │              └--63--02--04
│  │                  │  └--14
│  │                  └--10--45
│  │                      └--68
│  └--25
└--30--44--54--87--12--04--43--03
    │  │  │  │  │  │  └--25
    │  │  │  │  │  └--28--81
    │  │  │  │  │      └--73
    │  │  │  │  └--61--30
    │  │  │  └--89--11--65--80
    │  │  │      │  │  └--18
    │  │  │      │  └--86--30
    │  │  │      │      └--41
    │  │  │      └--17--84
    │  │  │          └--28--86
    │  │  │              └--63
    │  │  └--05--97--44--06--00
    │  │      │  │  │  └--45
    │  │      │  │  └--04--19
    │  │      │  │      └--02
    │  │      │  └--74--56--07
    │  │      │      │  └--42
    │  │      │      └--89--40
    │  │      │          └--61
    │  │      └--03--76--31--12
    │  │          │  │  └--31
    │  │          │  └--70--51
    │  │          │      └--20
    │  │          └--35--05--85
    │  │              │  └--01
    │  │              └--26--97
    │  │                  └--85
    │  └--76
    └--36--57--42--66--59--14--05
        │      │  │  │  └--86
        │      │  │  └--59--70
        │      │  │      └--37
        │      │  └--69--57--50
        │      │      │  └--84
        │      │      └--80--63
        │      │          └--30
        │      └--74--44--84--98
        │          │  │  └--44
        │          │  └--40--66
        │          └--90--85--90
        │              │  └--47
        │              └--37--89
        └--84