Вот прога
Код
program derevya;
uses crt;
type tinf=integer;
ptree = ^ttree;
ttree = record
inf:tinf;
left, right: ptree;
end;
{-----------------------------------------------}
function btree(m:word):ptree;
var nl,nr:word; k:tinf; node:ptree; left, right: ptree;
begin if m=0 then btree:=nil
else begin
nl:=m div 2;
nr:=m-nl-1;
writeln;
writeln('Vvedite zna4enie');
readln(k);
new(node);
node^.inf:=k;
node^.left:=btree(nl);
node^.right:=btree(nr);
btree:=node
end;
end;
{-----------------------------------------------}
procedure printlevel(root:ptree;level,curlevel:integer);
begin
if (root<>NIL) then
begin
if (curlevel=level) then write(root^.inf,' | ')
else
begin
printlevel(root^.left,level,curlevel+1);
printlevel(root^.right,level,curlevel+1);
end;
end;
end;
{-------------------------------------------------------------------}
procedure print_tree(p:ptree; level:integer);
var i:integer;
begin
if p=nil then exit;
with p^ do
begin
print_tree(right,level+1);
for i:=1 to level do write(' ');
writeln(inf);
print_tree(left,level+1);
end
end;
{-------------------------------------------------------------------}
var m:word; root,p:ptree; level,k,n,current:integer; otvet:char;
begin
textmode(3);
textbackground(1);
repeat
clrscr;
writeln('Zadayte 4islo vershin');
readln(m);
root:=btree(m);
writeln;
writeln('Vvedennoe derevo:');
writeln;
print_tree(root,0);
writeln;
write('Vvedite nomer urovnya dlya pokaza');
writeln;
readln(level);
writeln('Vse vershini na urovne ',level,' : ');
printlevel(root,level,0);
writeln;
writeln('Dlya vihoda nazhmite "y"');
otvet:=readkey;
until otvet='y';
end.