program r5(input,output);
uses crt;
Type    ted=char;
        Tf=file of ted;
        Ta=array [1..10000] of integer;
        pzv=^zv;
        zv=record key:integer;
                left,right:pzv;
                end;
var F:tf;
        k,l,z,i,x,c,v,h,n:integer;
        A:TA;
        verch:pzv;
        m,b,o:boolean;
        p:string;
        q,vih:char;
{----------------------------------------------------------------------------}
procedure inder(k:integer; var d:pzv);
var r,s:pzv;
        begin
        new(s);
        s^.key:=k;
        r:=d;
        if (r^.left=nil) and (r^.right=nil) then
                begin
                r^.key:=k;
                r^.left:=nil;
                r^.right:=nil
                end
        else
                begin
                if r^.left^.key<k then r:=r^.right else r:=r^.left;
                inder(k,r)
                end;
        end;
{----------------------------------------------------------------------------}
procedure sortir(var A:Ta);
var i,k,t:integer;
        begin
        for i:=1 to n do
                begin
                for k:=i to n do
                        begin
                        if a[k]<a[i] then
                                begin
                                t:=A[i];
                                a[i]:=a[k];
                                a[k]:=t
                                end
                        end
                end
        end;
{----------------------------------------------------------------------------}
procedure delder (var d:pzv);
var q:pzv;
        begin
        while (d^.left<>nil) or (d^.right<>nil) do
                begin
                q:=d;
                while (q^.left<>nil) and (q^.right<>nil) do
                        if q^.left=nil then q:=q^.right else q:=q^.left;
                dispose (q)
                end
        end;
{---------------------------------------------------------------------------}
begin clrscr;
repeat
        WRITELN ('You need to input N numbers in file with spaces even after the last');
        writeln ('input this N');
        read(n);
        writeln('Inputting is finished');
        m:=true;
        o:=true;
        assign (f,'C:\users\user\desktop\pascal hw\textdoc.txt');
        reset (f);
        for i:=1 to n do
                begin
                v:=0;
                m:=true;
                b:=false;
                read (f,q);
                if q='-' then
                        begin
                        b:=true;
                        read (f,q)
                        end;
                        while (q<>' ') do
                                begin
                                if (ord(q)<48) or (ord(q)>57) then
                                        begin
                                        m:=false;
                                        break
                                        end;
                                v:=10 * v+ ord(q) - 48;
                                read (f,q)
                                end;
                        if b then v:=-v;
                        a[i]:=v
                end;
        if m then for i:=1 to n-1 do
                begin
                sortir(A);
                if a[i]=a[i+1] then o:=false
                end;
        readln;
        if m and o then
                begin
                writeln;
                new(verch);
                writeln('It is a thee from our file:');
                writeln;
                l:=2;
                verch^.key:=a[n div L +1];
                verch^.right:=nil;
                verch^.left:=nil;
                z:=0;
                c:=2;
                while n>=c do
                        begin
                        inc(z);
                        c:=2*c
                        end;
                for x:=1 to z do write('    ');
                write('   ');
                write (a[n div L +1]);
                a[n div L +1]:= - maxint - 1;
                writeln;
                L:=L*2;
                h:=0;
                while n div l>=1 do
                        begin
                        write ('  ');
                        h:=h+1;
                        inder(a[(N DIV L +1)*i],verch);
                        if h<z then for x:=1 to (z - h) do write('    ');
                        for x:=1 to l div 2 do
                                begin
                                write(a[((N*(2*x-1)) DIV L)+1]);
                                for c:=1 to z-h do write('    ');
                                a[((N*(2*x-1)) DIV L)+1]:=- maxint - 1
                                end;
                        writeln;
                        L:=L*2;
                        end;
                l:=l div 2;
                for i:=1 to n do
                        if a[i]<> - maxint - 1 then
                                begin
                                inder(a[(N DIV L)*i],verch);
                                write (a[(N DIV L)*i],'  ');
                                end
                        else write ('  ');
                writeln;
                writeln;
                writeln('Outputting is finished');
                delder(verch);
                dispose (verch);
                writeln
                end
        else writeln ('File is incorrect!!');
close(f);
writeln ('Do you want to continue computings? ');
writeln ('0-no, others-yes');
readln (vih);
until vih='0';
writeln ('Thank you for using my programm!');
readln
end.
