Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit BinaryTree;
- Interface
- Uses
- System.SysUtils, Node, TreePrinter, Vcl.ExtCtrls, System.Generics.Collections;
- Type
- TArray = Array Of Integer;
- TBinaryTree = Class
- Public Root: TNode;
- Public Procedure BuildTree(NumArr: TArray; Low, High: Integer);
- Public Procedure Add(Key: Integer);
- Public Constructor NewBinaryTree;
- Public Procedure DrawTree(Num: Integer; Var Background: TImage; Element: Integer);
- Public Procedure Remove(Elem: Integer);
- Public Function DeleteRecursively(Current: TNode; Value: Integer): TNode;
- Public Function FindSmallestKey(Current: TNode): Integer;
- Public Function ToString(Node: TNode; Var ResultStr: String): String;
- Function IsEmpty(): Boolean;
- End;
- TNodesArray = Array of TNode;
- Implementation
- Var
- Counter: Integer = 0;
- Constructor TBinaryTree.NewBinaryTree;
- Begin
- Root := Nil;
- End;
- Procedure TBinaryTree.DrawTree(num: Integer; Var Background: TImage; Element: Integer);
- Var
- TreePrinter: TTreePrinter;
- Begin
- TreePrinter.DrawTree(num, Self.Root, Self.Root, Background, Element);
- End;
- Procedure TBinaryTree.Add(Key: Integer);
- Var
- Node, Current, Prev: TNode;
- ToExit: Boolean;
- Begin
- Node := TNode.NewNode(Key);
- ToExit := False;
- If (Self.Root = Nil) Then
- Self.Root := Node
- Else
- Begin
- Current := Self.Root;
- While Not ToExit Do
- Begin
- Prev := Current;
- If (Key < Prev.GetKey) Then
- Begin
- Current := Current.GetLeftChild;
- If (Current = Nil) Then
- Begin
- Prev.SetLeftChild(Node);
- ToExit := True;
- End;
- End
- Else If (Key > Prev.GetKey) Then
- Begin
- Current := Current.GetRightChild;
- If (Current = Nil) Then
- Begin
- Prev.SetRightChild(Node);
- ToExit := True;
- End;
- End
- Else
- ToExit := True;
- End;
- End;
- End;
- Procedure TBinaryTree.BuildTree(NumArr: TArray; Low, High: Integer);
- Var
- Middle, I: Integer;
- Begin
- For I := 0 To High Do
- Self.Add(Middle);
- End;
- Procedure TBinaryTree.Remove(Elem: Integer);
- Begin
- Root := DeleteRecursively(Root, Elem);
- End;
- Function TBinaryTree.DeleteRecursively(Current: TNode; Value: Integer): TNode;
- Var
- Flag: Boolean;
- Begin
- Flag := True;
- If (Current = Nil)Then
- Begin
- DeleteRecursively := Nil;
- Flag := False;
- End;
- If Flag And (Value < Current.GetKey) Then
- Begin
- Current.SetLeftChild(DeleteRecursively(Current.GetLeftChild, Value));
- DeleteRecursively := Current;
- Flag := False;
- End
- Else If Flag And (Value > Current.GetKey) Then
- Begin
- Current.SetRightChild(DeleteRecursively(Current.GetRightChild, Value));
- DeleteRecursively := Current;
- Flag := False;
- End;
- If Flag And (Current.GetLeftChild = Nil) Then
- Begin
- DeleteRecursively := Current.GetRightChild;
- Flag := False;
- End
- Else If Flag And (Current.GetRightChild = Nil) Then
- Begin
- DeleteRecursively := Current.GetLeftChild;
- Flag := False;
- End
- Else If Flag Then
- Begin
- Current.SetKey(FindSmallestKey(Current.GetRightChild));
- Current.SetRightChild(DeleteRecursively(Current.GetRightChild, Current.GetKey));
- DeleteRecursively := Current;
- Flag := False;
- End;
- End;
- Function TBinaryTree.FindSmallestKey(Current: TNode): Integer;
- Var
- Flag: Boolean;
- Begin
- Flag := True;
- If (Current.GetLeftChild = Nil) Then
- Begin
- FindSmallestKey := Current.GetKey;
- Flag := False;
- End
- Else
- If Flag Then
- FindSmallestKey := FindSmallestKey(Current.GetLeftChild);
- End;
- Function TBinaryTree.ToString(Node: TNode; Var ResultStr: String): String;
- begin
- If (Node <> Nil) Then
- Begin
- ToString(Node.GetLeftChild, ResultStr);
- ResultStr := ResultStr + '[' + IntToStr(Node.GetKey) + ']' + '<';
- ToString(Node.GetRightChild, ResultStr);
- End;
- ToString := ResultStr;
- End;
- Function TBinaryTree.IsEmpty(): Boolean;
- Var
- IsTreeEmpty: Boolean;
- Begin
- IsTreeEmpty := False;
- If (Root = Nil) Then
- IsTreeEmpty := True;
- IsEmpty := IsTreeEmpty;
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement