Acasa Tehnologie Arbori binari: creare, parcurgere

Arbori binari: creare, parcurgere

by Dragos Schiopu

programare

>

{arbori binari: creare, parcurgeri}
type ref=^nod;
     nod=record
           inf:integer;
           st,dr:ref
         end;
var r:ref;
procedure creare(var r:ref);
var x:integer;
begin
write('x= ');
readln(x);
if x=0 then r:=nil
else begin
     new(r);
     r^.inf:=x;
     write('subarborele stang al nodului ',r^.inf,':');
     creare(r^.st);
     write('subarborele drept al nodului ',r^.inf,':');
     creare(r^.dr);
     end;
end;
procedure preordine(r:ref);
begin
if r<>nil then
  begin
  write(r^.inf,' ');
  preordine(r^.st);
  preordine(r^.dr);
  end;
end;
procedure inordine(r:ref);
begin
if r<>nil then
  begin
  inordine(r^.st);
  write(r^.inf,' ');
  inordine(r^.dr);
  end;
end;
procedure postordine(r:ref);
begin
if r<>nil then
  begin
  postordine(r^.st);
  postordine(r^.dr);
  write(r^.inf,' ');
  end;
end;
begin
r:=nil;
creare(r);
write('Preordine:');preordine(r);
writeln;
write('Inordine:');inordine(r);
writeln;
write('Postordine:');postordine(r);
writeln;
readln
end.
{PascalZone.uv.ro}

s-ar putea sa-ti placa