Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. private static List<Vector2> GetGrafTree(Node<double> currentNode)
  2. {
  3. if (currentNode.depth == 1)
  4. dots.Add(new Vector2(currentNode.Element, (Program.getForm.Width / 2), 10, 1)); // добавить корень дерева
  5. int index = dots.FindLastIndex(elem => (elem.Value == currentNode.Element)); // находим вышестоящий элемент
  6. //пока не конец левого поддерева
  7. if (currentNode.Left != null)
  8. {
  9. int x = dots[index].X - (30 * currentNode.depth);
  10. int y = dots[index].Y + 40;
  11. string name = currentNode.Element.ToString() + "&" + currentNode.Left.Element.ToString();
  12. //lines.Add(new Line(name, dots[index].X+5, dots[index].Y+25, x+15, y));
  13. dots.Add(new Vector2(currentNode.Left.Element, x, y, currentNode.Left.depth)); // позиция левого элемента дерева
  14. GetGrafTree(currentNode.Left); // следующий левый элемент
  15. }
  16. //пока не конец правого поддерева
  17. if (currentNode.Right != null)
  18. {
  19. int x = dots[index].X + (30 * currentNode.depth);
  20. int y = dots[index].Y + 40;
  21. string name = currentNode.Element.ToString() + "&" + currentNode.Right.Element.ToString();
  22. //lines.Add(new Line(name, dots[index].X+25, dots[index].Y+25, x+15, y));
  23. dots.Add(new Vector2(currentNode.Right.Element, x, y, currentNode.Right.depth)); // позиция правого элемента дерева
  24. GetGrafTree(currentNode.Right); // следующий правый элемент
  25. }
  26. return dots;
  27. }
  28.  
  29. class TreeNode
  30. {
  31. public TreeNode(int value)
  32. {
  33. Value = value;
  34. }
  35.  
  36. public int Value { get; set; }
  37.  
  38. public TreeNode Left { get; set; }
  39.  
  40. public TreeNode Right { get; set; }
  41. }
  42.  
  43. static void Print(TreeNode node, int y, int left, int right)
  44. {
  45. if (node == null)
  46. {
  47. return;
  48. }
  49.  
  50. var x = left + (right - left) / 2;
  51. Console.SetCursorPosition(x, y);
  52. Console.Write(node.Value);
  53.  
  54. Print(node?.Left, y + 1, left, x);
  55. Print(node?.Right, y + 1, x, right);
  56. }
  57.  
  58. var root = CreateTree();
  59. Print(root, 0, 0, Console.WindowWidth);
  60.  
  61. int Высота(Вершина вершина)
  62. {
  63. if (вершина == null) return -1;
  64. return 1 + Max(Высота(вершина.ЛевыйПотомок), Высота(вершина.ПравыйПотомок));
  65. }
  66.  
  67. <Viewbox Stretch="Uniform">
  68. <Canvas Name="TreeCanvas"/>
  69. </Viewbox>
  70.  
  71. void DrawTree(Node node, double x, double y, double dx, double dy)
  72. {
  73. if (node == null) return;
  74. var e = new Ellipse
  75. {
  76. Width = 5,
  77. Height = 5,
  78. Fill = new SolidColorBrush(Colors.Black)
  79. };
  80. TreeCanvas.Children.Add(e);
  81. Canvas.SetLeft(e, x);
  82. Canvas.SetTop(e, y);
  83. DrawTree(node.Left, x - dx, y + dy, dx / 2, dy);
  84. DrawTree(node.Right, x + dx, y + dy, dx / 2, dy);
  85. }
  86.  
  87. DrawTree(tree, 100, 0, 50, 50);
  88.  
  89. TreeCanvas.Height = 2 * 100 + 2 * 5;
  90. TreeCanvas.Width = 2 * 100 + 2 * 5;
Add Comment
Please, Sign In to add comment