P
P
Porto_b2019-11-25 14:38:54
Delphi
Porto_b, 2019-11-25 14:38:54

Binary search tree?

There is a structure for a binary search tree

U = ^BTS;
BTS = record
       inf : integer;  //Ключ\Данные
         L : U;          //Левый потомок
         R : U;         //Правый потомок
      end;

And the code that fills it with random numbers / keys (variable R)
..

procedure CTree(var Tree : U; R : integer;);
begin
           if Tree = Nil
             then
                 begin
                   New(Tree);
                   Tree^.L := Nil;
                   Tree^.R := Nil;
                   Tree^.inf := R;
                 end
             else
                   if R <= Tree^.inf
                           then CTree(Tree^.L,R)
                           else CTree(Tree^.R,R)
              
end;
..
Randomize;
..
for i:=0 to n-1 do
   begin
            r := random(30);
            CTree(a, r);
   end;

So, if, for example, the root node has a key of 15, then all the other generated keys will be added to the right or to the left, depending on whether they are more than 15 or less, it may happen that all numbers are generated less than 15 and all of them are added only to the left side, as balanced fill the tree

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question