Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit TreePrinter;
- Interface
- Uses
- System.SysUtils, Node, System.Generics.Collections, Math, Vcl.ExtCtrls, Vcl.Graphics;
- Type
- TTreePrinter = Class
- Public Procedure DrawTree(Num: Integer; Node, Root: TNode; Var Background: TImage; Element: Integer);
- End;
- Implementation
- Var
- Centre: Integer;
- Procedure DrawRoot(Root: TNode; Middle, Width, Height: Integer; Var Drawing: TImage; Elem: Integer);
- Begin
- With Drawing.Canvas Do
- Begin
- Pen.Color := ClLime;
- Brush.Color := ClGreen;
- Pen.Width := 3;
- Ellipse(Width - 5, Height - 65, Width + 25, Height - 35);
- Pen.Color := ClWhite;
- TextOut(Width, Height - 58, IntToStr(Root.GetKey));
- End;
- End;
- Procedure PrintTree(Curr, Root: TNode; Middle, Width, Height: Integer; Var Background: TImage);
- Begin
- Width := Trunc(Width / 2);
- If Curr.GetLeftChild <> Nil Then
- Begin
- Background.Canvas.MoveTo(Middle - 6, Height - 45);
- Background.Canvas.Pen.Color := ClLime;
- Background.Canvas.Pen.Width := 3;
- Background.Canvas.LineTo(Middle - Width, Height - 8);
- Background.Canvas.Brush.Color := ClGreen;
- Background.Canvas.Ellipse(Middle - Width - 5, Height - 25, Middle - Width + 25, Height + 5);
- Background.Canvas.Pen.Color := ClWhite;
- Background.Canvas.TextOut(Middle - Width, Height - 18, IntTOStr(Curr.GetLeftChild.GetKey));
- Height := Height + 40 + 2;
- Centre := Middle - Width + 3;
- PrintTree(Curr.GetLeftChild, Root, Centre, Width, Height, Background);
- End;
- If Curr.GetRightChild <> Nil Then
- Begin
- If Curr.GetLeftChild <> Nil Then
- Height := Height - 40 - 2;
- Background.Canvas.MoveTo(Middle + 18, Height - 40);
- Background.Canvas.Pen.Color := ClLime;
- Background.Canvas.Pen.Width := 3;
- Background.Canvas.LineTo(Middle + Width + 5, Height - 5);
- Background.Canvas.Brush.Color := ClGreen;
- Background.Canvas.Ellipse(Middle + Width - 5, Height - 25, Middle + Width + 25, Height + 5);
- Background.Canvas.Pen.Color := ClWhite;
- Background.Canvas.TextOut(Width + Middle, Height - 18, IntTOStr(Curr.GetRightChild.GetKey));
- Height := Height + 40 + 1;
- Centre := Middle + Width - 1;
- PrintTree(Curr.GetRightChild, Root, Centre, Width, Height, Background);
- End;
- If (Height > Background.Height) Then
- Background.Height := Height + 10;
- If (Width > Background.Width) Then
- Background.Width := Width + 10;
- End;
- Procedure PrintMirrorTree(Curr, Root: TNode; Middle, Width, Height: Integer; Var Background: TImage);
- Begin
- Width := Trunc(Width / 2);
- If Curr.GetLeftChild <> Nil Then
- Begin
- Background.Canvas.MoveTo(Middle + 18, Height - 40);
- Background.Canvas.Pen.Color := ClLime;
- Background.Canvas.Pen.Width := 3;
- Background.Canvas.LineTo(Middle + Width + 5, Height - 5);
- Background.Canvas.Brush.Color := ClGreen;
- Background.Canvas.Ellipse(Middle + Width - 5, Height - 25, Middle + Width + 25, Height + 5);
- Background.Canvas.Pen.Color := ClWhite;
- Background.Canvas.TextOut(Width + Middle, Height - 18, IntTOStr(Curr.GetLeftChild.GetKey));
- Height := Height + 40 + 1;
- Centre := Middle + Width - 1;
- PrintMirrorTree(Curr.GetLeftChild, Root, Centre, Width, Height, Background);
- End;
- If Curr.GetRightChild <> Nil Then
- Begin
- If Curr.GetLeftChild <> Nil Then
- Height := Height - 40 - 2;
- Background.Canvas.MoveTo(Middle - 6, Height - 45);
- Background.Canvas.Pen.Color := ClLime;
- Background.Canvas.Pen.Width := 3;
- Background.Canvas.LineTo(Middle - Width, Height - 8);
- Background.Canvas.Brush.Color := ClGreen;
- Background.Canvas.Ellipse(Middle - Width - 5, Height - 25, Middle - Width + 25, Height + 5);
- Background.Canvas.Pen.Color := ClWhite;
- Background.Canvas.TextOut(Middle - Width, Height - 18, IntTOStr(Curr.GetRightChild.GetKey));
- Height := Height + 40 + 2;
- Centre := Middle - Width + 3;
- PrintMirrorTree(Curr.GetRightChild, Root, Centre, Width, Height, Background);
- End;
- If (Height > Background.Height) Then
- Background.Height := Height + 10;
- If (Width > Background.Width) Then
- Background.Width := Width + 10;
- End;
- Procedure TTreePrinter.DrawTree(Num: Integer; Node, Root: TNode; Var Background: TImage; Element: Integer);
- Var
- Width, Height: Integer;
- Begin
- Centre := Background.Width Div 2 - 50;
- Width := Centre;
- Height := 70;
- Background.Picture := Nil;
- With Background.Canvas Do
- Begin
- Brush.Color := ClGray;
- Rectangle(0, 0, Background.Width, Background.Height);
- Pen.Style := PsDash;
- Pen.Color := ClWhite;
- Pen.Width := 2;
- Font.Size := 10;
- Font.Color := ClWhite;
- MoveTo(Width, Height);
- End;
- DrawRoot(Root, 0, Width, Height, Background, Element);
- if num = 1 then
- PrintTree(Node, Root, Centre, Width, Height, Background)
- else
- PrintMirrorTree(Node, Root, Centre, Width, Height, Background);
- If (Height > Background.Height) Then
- Background.Height := Height + 10;
- If (Width > Background.Width) Then
- Background.Width := Width + 10;
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement