fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. class Node
  6. {
  7. public Node Left { get; protected set; }
  8. public Node Right { get; protected set; }
  9. public Node Parent { get; protected set; }
  10. public Node Next { get; set; }
  11.  
  12. public int Value { get; set; }
  13.  
  14. public Node(int val)
  15. {
  16. Value = val;
  17. }
  18.  
  19. public Node(int val, Node l, Node r) : this(val)
  20. {
  21. SetNodes(l, r);
  22. }
  23.  
  24. public void SetNodes(Node l, Node r)
  25. {
  26. l.Parent = this;
  27. r.Parent = this;
  28.  
  29. Left = l;
  30. Right = r;
  31. }
  32.  
  33. public void SetAsParent(Node p)
  34. {
  35. if (p.Left == null)
  36. p.Left = this;
  37. else if (p.Right == null)
  38. p.Right = this;
  39. else
  40. throw new Exception("Busy");
  41. }
  42. }
  43.  
  44. static void Main(string[] args)
  45. {
  46. for (var xx = 1; xx < 20; xx++)
  47. {
  48. var hA = new Node[xx];
  49. hA[0] = new Node(0);
  50.  
  51. for (int i = 1; i < hA.Length; i++)
  52. {
  53. hA[i] = new Node(i);
  54.  
  55. hA[i].SetAsParent(hA[(i + 1) / 2 - 1]);
  56. }
  57.  
  58. var root = hA[0];
  59. root.Next = root.Left;
  60. Link(root);
  61.  
  62. for (var r = root; r != null; r = r.Next)
  63. {
  64. Console.Write(r.Value + " ");
  65. }
  66. Console.Write("\r\n");
  67. }
  68. }
  69.  
  70. static void Link(Node n)
  71. {
  72. if (n == null)
  73. return;
  74.  
  75. if (n.Left != null)
  76. n.Left.Next = n.Right;
  77.  
  78. if (n.Right != null)
  79. n.Right.Next = n.Next.Left;
  80.  
  81. Link(n.Left);
  82. Link(n.Right);
  83. }
  84. }
Success #stdin #stdout 0.04s 23944KB
stdin
Standard input is empty
stdout
0  
0  1  
0  1  2  
0  1  2  3  
0  1  2  3  4  
0  1  2  3  4  5  
0  1  2  3  4  5  6  
0  1  2  3  4  5  6  7  
0  1  2  3  4  5  6  7  8  
0  1  2  3  4  5  6  7  8  9  
0  1  2  3  4  5  6  7  8  9  10  
0  1  2  3  4  5  6  7  8  9  10  11  
0  1  2  3  4  5  6  7  8  9  10  11  12  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18