Advertisement
Egor_Vakar

(Delphi) lab 5.2 BinaryTree

Mar 23rd, 2022
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.51 KB | None | 0 0
  1. Unit BinaryTree;
  2.  
  3. Interface
  4.  
  5. Uses
  6.     System.SysUtils, Node, TreePrinter, Vcl.ExtCtrls, System.Generics.Collections;
  7.  
  8. Type
  9.     TArray = Array Of Integer;
  10.  
  11.     TBinaryTree = Class
  12.         Public Root: TNode;
  13.         Public Procedure BuildTree(NumArr: TArray; Low, High: Integer);
  14.         Public Procedure Add(Key: Integer);
  15.         Public Constructor NewBinaryTree;
  16.         Public Procedure DrawTree(Num: Integer; Var Background: TImage; Element: Integer);
  17.         Public Procedure Remove(Elem: Integer);
  18.         Public Function DeleteRecursively(Current: TNode; Value: Integer): TNode;
  19.         Public Function FindSmallestKey(Current: TNode): Integer;
  20.         Public Function ToString(Node: TNode; Var ResultStr: String): String;
  21.         Function IsEmpty(): Boolean;
  22.     End;
  23.  
  24.     TNodesArray = Array of TNode;
  25.  
  26. Implementation
  27.  
  28. Var
  29.     Counter: Integer = 0;
  30.  
  31. Constructor TBinaryTree.NewBinaryTree;
  32. Begin
  33.     Root := Nil;
  34. End;
  35.  
  36. Procedure TBinaryTree.DrawTree(num: Integer; Var Background: TImage; Element: Integer);
  37. Var
  38.     TreePrinter: TTreePrinter;
  39. Begin
  40.     TreePrinter.DrawTree(num, Self.Root, Self.Root, Background, Element);
  41. End;
  42.  
  43. Procedure TBinaryTree.Add(Key: Integer);
  44. Var
  45.     Node, Current, Prev: TNode;
  46.     ToExit: Boolean;
  47. Begin
  48.     Node := TNode.NewNode(Key);
  49.     ToExit := False;
  50.     If (Self.Root = Nil) Then
  51.         Self.Root := Node
  52.     Else
  53.     Begin
  54.         Current := Self.Root;
  55.         While Not ToExit Do
  56.         Begin
  57.             Prev := Current;
  58.             If (Key < Prev.GetKey) Then
  59.             Begin
  60.                 Current := Current.GetLeftChild;
  61.                 If (Current = Nil) Then
  62.                 Begin
  63.                     Prev.SetLeftChild(Node);
  64.                     ToExit := True;
  65.                 End;
  66.             End
  67.             Else If (Key > Prev.GetKey) Then
  68.             Begin
  69.                 Current := Current.GetRightChild;
  70.                 If (Current = Nil) Then
  71.                 Begin
  72.                     Prev.SetRightChild(Node);
  73.                     ToExit := True;
  74.                 End;
  75.             End
  76.             Else
  77.                 ToExit := True;
  78.         End;
  79.     End;
  80. End;
  81.  
  82. Procedure TBinaryTree.BuildTree(NumArr: TArray; Low, High: Integer);
  83. Var
  84.     Middle, I: Integer;
  85. Begin
  86.       For I := 0 To High Do
  87.         Self.Add(Middle);
  88. End;
  89.  
  90. Procedure TBinaryTree.Remove(Elem: Integer);
  91. Begin
  92.     Root := DeleteRecursively(Root, Elem);
  93. End;
  94.  
  95. Function TBinaryTree.DeleteRecursively(Current: TNode; Value: Integer): TNode;
  96. Var
  97.     Flag: Boolean;
  98. Begin
  99.     Flag := True;
  100.     If (Current = Nil)Then
  101.     Begin
  102.         DeleteRecursively := Nil;
  103.         Flag := False;
  104.     End;
  105.     If Flag And (Value < Current.GetKey) Then
  106.     Begin
  107.         Current.SetLeftChild(DeleteRecursively(Current.GetLeftChild, Value));
  108.         DeleteRecursively := Current;
  109.         Flag := False;
  110.     End
  111.     Else If Flag And (Value > Current.GetKey) Then
  112.     Begin
  113.         Current.SetRightChild(DeleteRecursively(Current.GetRightChild, Value));
  114.         DeleteRecursively := Current;
  115.         Flag := False;
  116.     End;
  117.     If Flag And (Current.GetLeftChild = Nil) Then
  118.     Begin
  119.         DeleteRecursively := Current.GetRightChild;
  120.         Flag := False;
  121.     End
  122.     Else If Flag And (Current.GetRightChild = Nil) Then
  123.     Begin
  124.         DeleteRecursively := Current.GetLeftChild;
  125.         Flag := False;
  126.     End
  127.     Else If Flag Then
  128.     Begin
  129.         Current.SetKey(FindSmallestKey(Current.GetRightChild));
  130.         Current.SetRightChild(DeleteRecursively(Current.GetRightChild, Current.GetKey));
  131.         DeleteRecursively := Current;
  132.         Flag := False;
  133.     End;
  134. End;
  135.  
  136. Function TBinaryTree.FindSmallestKey(Current: TNode): Integer;
  137. Var
  138.     Flag: Boolean;
  139. Begin
  140.     Flag := True;
  141.     If (Current.GetLeftChild = Nil) Then
  142.     Begin
  143.         FindSmallestKey := Current.GetKey;
  144.         Flag := False;
  145.     End
  146.     Else
  147.         If Flag Then
  148.             FindSmallestKey := FindSmallestKey(Current.GetLeftChild);
  149. End;
  150.  
  151. Function TBinaryTree.ToString(Node: TNode; Var ResultStr: String): String;
  152. begin
  153. If (Node <> Nil) Then
  154.     Begin
  155.         ToString(Node.GetLeftChild, ResultStr);
  156.         ResultStr := ResultStr + '[' + IntToStr(Node.GetKey) + ']' + '<';
  157.         ToString(Node.GetRightChild, ResultStr);
  158.     End;
  159.     ToString := ResultStr;
  160. End;
  161.  
  162. Function TBinaryTree.IsEmpty(): Boolean;
  163. Var
  164.     IsTreeEmpty: Boolean;
  165. Begin
  166.     IsTreeEmpty := False;
  167.     If (Root = Nil) Then
  168.         IsTreeEmpty := True;
  169.     IsEmpty := IsTreeEmpty;
  170. End;
  171.  
  172. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement