Advertisement
Egor_Vakar

(Delphi) lab 5.2 TreePrinter

Mar 23rd, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.38 KB | None | 0 0
  1. Unit TreePrinter;
  2.  
  3. Interface
  4.  
  5. Uses
  6.     System.SysUtils, Node, System.Generics.Collections, Math, Vcl.ExtCtrls, Vcl.Graphics;
  7.  
  8. Type
  9.     TTreePrinter = Class
  10.         Public Procedure DrawTree(Num: Integer; Node, Root: TNode; Var Background: TImage; Element: Integer);
  11.     End;
  12.  
  13. Implementation
  14.  
  15. Var
  16.     Centre: Integer;
  17.  
  18. Procedure DrawRoot(Root: TNode; Middle, Width, Height: Integer; Var Drawing: TImage; Elem: Integer);
  19. Begin
  20.     With Drawing.Canvas Do
  21.     Begin
  22.         Pen.Color := ClLime;
  23.         Brush.Color := ClGreen;
  24.         Pen.Width := 3;
  25.         Ellipse(Width - 5, Height - 65, Width + 25,  Height - 35);
  26.         Pen.Color := ClWhite;
  27.         TextOut(Width, Height - 58, IntToStr(Root.GetKey));
  28.     End;
  29. End;
  30.  
  31. Procedure PrintTree(Curr, Root: TNode; Middle, Width, Height: Integer; Var Background: TImage);
  32. Begin
  33.     Width := Trunc(Width / 2);
  34.  
  35.     If Curr.GetLeftChild <> Nil Then
  36.     Begin
  37.         Background.Canvas.MoveTo(Middle - 6, Height - 45);
  38.         Background.Canvas.Pen.Color := ClLime;
  39.         Background.Canvas.Pen.Width := 3;
  40.         Background.Canvas.LineTo(Middle - Width, Height - 8);
  41.         Background.Canvas.Brush.Color := ClGreen;
  42.         Background.Canvas.Ellipse(Middle - Width - 5, Height - 25, Middle - Width + 25,  Height + 5);
  43.         Background.Canvas.Pen.Color := ClWhite;
  44.         Background.Canvas.TextOut(Middle - Width, Height - 18, IntTOStr(Curr.GetLeftChild.GetKey));
  45.         Height := Height + 40 + 2;
  46.         Centre := Middle - Width + 3;
  47.         PrintTree(Curr.GetLeftChild, Root, Centre, Width, Height, Background);
  48.     End;
  49.  
  50.     If Curr.GetRightChild <> Nil Then
  51.     Begin
  52.         If Curr.GetLeftChild <> Nil Then
  53.            Height := Height - 40 - 2;
  54.  
  55.         Background.Canvas.MoveTo(Middle + 18, Height - 40);
  56.         Background.Canvas.Pen.Color := ClLime;
  57.         Background.Canvas.Pen.Width := 3;
  58.         Background.Canvas.LineTo(Middle + Width + 5, Height - 5);
  59.         Background.Canvas.Brush.Color := ClGreen;
  60.         Background.Canvas.Ellipse(Middle + Width - 5, Height - 25, Middle + Width + 25,  Height + 5);
  61.         Background.Canvas.Pen.Color := ClWhite;
  62.         Background.Canvas.TextOut(Width + Middle, Height - 18, IntTOStr(Curr.GetRightChild.GetKey));
  63.         Height := Height + 40 + 1;
  64.         Centre := Middle + Width - 1;
  65.         PrintTree(Curr.GetRightChild, Root, Centre, Width, Height, Background);
  66.     End;
  67.     If (Height > Background.Height) Then
  68.         Background.Height := Height + 10;
  69.     If (Width > Background.Width) Then
  70.         Background.Width := Width + 10;
  71. End;
  72.  
  73. Procedure PrintMirrorTree(Curr, Root: TNode; Middle, Width, Height: Integer; Var Background: TImage);
  74. Begin
  75.     Width := Trunc(Width / 2);
  76.  
  77.     If Curr.GetLeftChild <> Nil Then
  78.     Begin
  79.         Background.Canvas.MoveTo(Middle + 18, Height - 40);
  80.         Background.Canvas.Pen.Color := ClLime;
  81.         Background.Canvas.Pen.Width := 3;
  82.         Background.Canvas.LineTo(Middle + Width + 5, Height - 5);
  83.         Background.Canvas.Brush.Color := ClGreen;
  84.         Background.Canvas.Ellipse(Middle + Width - 5, Height - 25, Middle + Width + 25,  Height + 5);
  85.         Background.Canvas.Pen.Color := ClWhite;
  86.         Background.Canvas.TextOut(Width + Middle, Height - 18, IntTOStr(Curr.GetLeftChild.GetKey));
  87.         Height := Height + 40 + 1;
  88.         Centre := Middle + Width - 1;
  89.         PrintMirrorTree(Curr.GetLeftChild, Root, Centre, Width, Height, Background);
  90.     End;
  91.  
  92.     If Curr.GetRightChild <> Nil Then
  93.     Begin
  94.         If Curr.GetLeftChild <> Nil Then
  95.            Height := Height - 40 - 2;
  96.         Background.Canvas.MoveTo(Middle - 6, Height - 45);
  97.         Background.Canvas.Pen.Color := ClLime;
  98.         Background.Canvas.Pen.Width := 3;
  99.         Background.Canvas.LineTo(Middle - Width, Height - 8);
  100.         Background.Canvas.Brush.Color := ClGreen;
  101.         Background.Canvas.Ellipse(Middle - Width - 5, Height - 25, Middle - Width + 25,  Height + 5);
  102.         Background.Canvas.Pen.Color := ClWhite;
  103.         Background.Canvas.TextOut(Middle - Width, Height - 18, IntTOStr(Curr.GetRightChild.GetKey));
  104.         Height := Height + 40 + 2;
  105.         Centre := Middle - Width + 3;
  106.         PrintMirrorTree(Curr.GetRightChild, Root, Centre, Width, Height, Background);
  107.     End;
  108.     If (Height > Background.Height) Then
  109.         Background.Height := Height + 10;
  110.     If (Width > Background.Width) Then
  111.         Background.Width := Width + 10;
  112. End;
  113.  
  114. Procedure TTreePrinter.DrawTree(Num: Integer; Node, Root: TNode; Var Background: TImage; Element: Integer);
  115. Var
  116.     Width, Height: Integer;
  117. Begin
  118.     Centre := Background.Width Div 2 - 50;
  119.     Width := Centre;
  120.     Height := 70;
  121.  
  122.     Background.Picture := Nil;
  123.     With Background.Canvas Do
  124.     Begin
  125.         Brush.Color := ClGray;
  126.         Rectangle(0, 0, Background.Width, Background.Height);
  127.         Pen.Style := PsDash;
  128.         Pen.Color := ClWhite;
  129.         Pen.Width := 2;
  130.         Font.Size := 10;
  131.         Font.Color := ClWhite;
  132.         MoveTo(Width, Height);
  133.     End;
  134.  
  135.     DrawRoot(Root, 0, Width, Height, Background, Element);
  136.     if num = 1 then
  137.         PrintTree(Node, Root, Centre, Width, Height, Background)
  138.     else
  139.         PrintMirrorTree(Node, Root, Centre, Width, Height, Background);
  140.     If (Height > Background.Height) Then
  141.         Background.Height := Height + 10;
  142.     If (Width > Background.Width) Then
  143.         Background.Width := Width + 10;
  144. End;
  145.  
  146. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement