Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.69 KB | None | 0 0
  1. unit Tree;
  2.  
  3. interface
  4.  
  5. uses VCL.GRIDS, System.SysUtils, System.math;
  6.  
  7. type
  8.    TDIntArr = array of Integer;
  9.  
  10.    TBTree = class(TObject)
  11.    private
  12.       FVal: Integer;
  13.       FDepth: Integer;
  14.       FLeft, FRight: TBTree;
  15.    public
  16.       constructor Create; overload;
  17.       constructor Create(pVal: Integer); overload;
  18.       destructor Destroy; overload;
  19.  
  20.       procedure Add(pVal: Integer);
  21.       procedure SetLeft(Left: TBTree);
  22.       procedure SetRight(Right: TBTree);
  23.       function BinarySearch(L, R, pVal: Integer; Arr: TDIntArr;
  24.         var Root: TBTree): Integer;
  25.  
  26.       property Depth: Integer read FDepth write FDepth;
  27.       property Left: TBTree read FLeft write SetLeft;
  28.       property Right: TBTree read FRight write SetRight;
  29.       property Val: Integer read FVal write FVal;
  30.  
  31.    end;
  32.  
  33. implementation
  34.  
  35. constructor TBTree.Create;
  36. begin
  37.    inherited Create;
  38.    FLeft := nil;
  39.    FRight := nil;
  40.    FDepth := 1;
  41. end;
  42.  
  43. constructor TBTree.Create(pVal: Integer);
  44. begin
  45.    Create;
  46.    FVal := pVal;
  47. end;
  48.  
  49. destructor TBTree.Destroy;
  50. begin
  51.    if FLeft <> nil then
  52.       FLeft.Destroy;
  53.    if FRight <> nil then
  54.       FRight.Destroy;
  55.    inherited Destroy;
  56. end;
  57.  
  58. procedure TBTree.SetLeft(Left: TBTree);
  59. begin
  60.    FLeft := Left;
  61. end;
  62.  
  63. procedure TBTree.SetRight(Right: TBTree);
  64. begin
  65.    FRight := Right;
  66. end;
  67.  
  68. procedure TBTree.Add(pVal: Integer);
  69. begin
  70.    if pVal < FVal then
  71.    begin
  72.       if Left = nil then
  73.       begin
  74.          Left := TBTree.Create;
  75.          Left.FVal := pVal;
  76.       end
  77.       else
  78.       begin
  79.          Left.Add(pVal);
  80.       end;
  81.    end
  82.    else
  83.    begin
  84.       if Right = nil then
  85.       begin
  86.          Right := TBTree.Create;
  87.          Right.FVal := pVal;
  88.       end
  89.       else
  90.       begin
  91.          Right.Add(pVal);
  92.       end;
  93.    end;
  94.    if Right <> nil then
  95.       Depth := Max(Depth, 1 + Right.Depth);
  96.    if Left <> nil then
  97.       Depth := Max(Depth, 1 + Left.Depth);
  98. end;
  99.  
  100. function TBTree.BinarySearch(L, R, pVal: Integer; Arr: TDIntArr;
  101.   var Root: TBTree): Integer;
  102. var
  103.    M, MVal: Integer;
  104. begin
  105.    if L <= R then
  106.    begin
  107.       M := (L + R) div 2;
  108.       MVal := Arr[M];
  109.       Val := MVal;
  110.       if pVal < MVal then
  111.       begin
  112.          Left := TBTree.Create;
  113.          Root.Depth := Root.Depth + 1;
  114.          Result := FLeft.BinarySearch(L, M - 1, pVal, Arr, Root);
  115.       end
  116.       else if pVal = MVal then
  117.       begin
  118.          Result := M;
  119.       end
  120.       else
  121.       begin
  122.          Right := TBTree.Create;
  123.          Root.Depth := Root.Depth + 1;
  124.          Result := FRight.BinarySearch(M + 1, R, pVal, Arr, Root);
  125.       end;
  126.    end
  127.    else
  128.    begin
  129.       Result := -1;
  130.       Val := pVal;
  131.    end;
  132. end;
  133.  
  134. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement