SHARE
TWEET

Untitled

a guest Nov 12th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void VisualiseTree(Graphics g)
  2.             {
  3.                 if (Root == null)
  4.                 {
  5.                     g.DrawString("Дерево пустое (root = null)", new Font("Helvetica", 15), Brushes.Black, (float)0.0, (float)0.0);
  6.                     return;
  7.                 }
  8.  
  9.                 // План А
  10.                 int xEllipce = 235, yEllipce = 0, xLine = 240, yLine = 5, x1Line = 240, y1Line = 5;
  11.                 g.Clear(Form1.DefaultBackColor);
  12.                 g.FillEllipse(Brushes.White, xEllipce, yEllipce, 10, 10);
  13.                 g.DrawString("" + Root.Data, new Font("Helvetica", 8), Brushes.Black, xEllipce, yEllipce);
  14.                 if (Root.Left != null) Draw_Left(g, Root.Left, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line);
  15.                 if (Root.Right != null) Draw_Right(g, Root.Right, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line);
  16.  
  17.                 // План Б
  18.                 //VisualiseTree(g, Root);
  19.                
  20.             }
  21.             private void VisualiseTree(Graphics g, Node<T> node, int count = 0, double xValue = 235, double yValue = 0, double x1Line = 240, double y1Line = 5, double xLine = 240, double yLine = 5)
  22.             {
  23.                 if (node != null)
  24.                 {
  25.                     MessageBox.Show("x: " + xLine + " y: " + yLine + " x1: " + x1Line + " y1: " + y1Line);
  26.                     g.FillEllipse(Brushes.White, (int)xValue, (int)yValue, 10, 10); // круг
  27.                     g.DrawString("" + node.Data, new Font("Helvetica", 8), Brushes.Black, (float)xValue, (float)yValue); // числа в кругах
  28.                     g.DrawLine(pen, (float)xLine, (float)yLine, (float)x1Line, (float)y1Line); // линии
  29.                     VisualiseTree(g, node.Left, count++, xValue -= 35, yValue += 10, xLine, yLine, xLine -= 35, yLine += 11); // рек
  30.                     VisualiseTree(g, node.Right, count++, xValue += 65, yValue += 10, xLine + 35, yLine - 11, xLine += 65, yLine += 9);
  31.                 }
  32.             }
  33.             private void Draw_Left(Graphics g, Node<T> node, int parent_xEllipce, int parent_yEllipce, int parent_xLine, int parent_yLine, int parent_x1Line, int parent_y1Line)
  34.             {
  35.                 int count, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line, xEllipce_distance = 20, xLine_distance = 20, y_distance = 10;
  36.                 if (node.Right != null)
  37.                 {
  38.                     count = 1 + Children_Count_Right(node.Right, 0);
  39.                 } else count = 0;
  40.                 xEllipce = parent_xEllipce - xEllipce_distance - (count * xEllipce_distance);
  41.                 yEllipce = parent_yEllipce + y_distance;
  42.                 x1Line = parent_xLine;
  43.                 y1Line = parent_yLine;
  44.                 xLine = parent_xLine - xLine_distance - (count * xLine_distance);
  45.                 yLine = parent_yLine + y_distance;
  46.                 g.FillEllipse(Brushes.White, xEllipce, yEllipce, 10, 10);
  47.                 g.DrawString("" + node.Data, new Font("Helvetica", 8), Brushes.Black, xEllipce, yEllipce);
  48.                 g.DrawLine(pen, (float)xLine, (float)yLine, (float)x1Line, (float)y1Line);
  49.                 if (node.Left != null) Draw_Left(g, node.Left, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line);
  50.                 if (node.Right != null) Draw_Right(g, node.Right, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line); ;
  51.             }
  52.             private void Draw_Right(Graphics g, Node<T> node, int parent_xEllipce, int parent_yEllipce, int parent_xLine, int parent_yLine, int parent_x1Line, int parent_y1Line)
  53.             {
  54.                 int count, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line, xEllipce_distance = 20, xLine_distance = 20, y_distance = 10;
  55.                 if (node.Left != null)
  56.                 {
  57.                     count = 1 + Children_Count_Left(node.Left, 0);
  58.                 }
  59.                 else count = 0;
  60.                 xEllipce = parent_xEllipce + xEllipce_distance + (count * xEllipce_distance);
  61.                 yEllipce = parent_yEllipce + y_distance;
  62.                 x1Line = parent_xLine;
  63.                 y1Line = parent_yLine;
  64.                 xLine = parent_xLine + xLine_distance + (count * xLine_distance);
  65.                 yLine = parent_yLine + y_distance;
  66.                 g.FillEllipse(Brushes.White, xEllipce, yEllipce, 10, 10);
  67.                 g.DrawString("" + node.Data, new Font("Helvetica", 8), Brushes.Black, xEllipce, yEllipce);
  68.                 g.DrawLine(pen, (float)xLine, (float)yLine, (float)x1Line, (float)y1Line);
  69.                 if (node.Left != null) Draw_Left(g, node.Left, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line);
  70.                 if (node.Right != null) Draw_Right(g, node.Right, xEllipce, yEllipce, xLine, yLine, x1Line, y1Line);
  71.             }
  72.             private int Children_Count_Right(Node<T> node, int count)
  73.             {
  74.                 if (node.Right != null)
  75.                 {
  76.                     if (count == 1) return 1;
  77.                     return 1 + Children_Count_Right(node.Right, count - 1);
  78.                 } return 1;
  79.             }
  80.             private int Children_Count_Left(Node<T> node, int count)
  81.             {
  82.                 if (node.Left != null)
  83.                 {
  84.                     if (count == 1) return 1;
  85.                     return 1 + Children_Count_Left(node.Left, count - 1);
  86.                 } return 1;
  87.             }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top