CATEGORII DOCUMENTE |
APLICATII - ARBORI
1. Fiind dat un arbore final cu nodurile notate 1,, n si radacina 1, in fiecare nod are ca informatie o valoare intreaga, sa se realizeze un subprogram, in Pascal, care afiseaza informatiile continute de nodurile cu numere de ordine pare.
program arbore;
type sir=array[1..3O] of byte;
var rad, n , i : byte;
info, st, dr : sir;
procedure afis_2(i:integer);
begin
if (i mod 2=0) then write(info[i]:4);
if (st[i]<>0) then afis_2(st[i]);
if (dr[i]<>0) then afis_2(dr[i]); end;
begin
write('n='); readln(n); for i:= 1 to n do begin
writeln('informatiile despre nodul ',i); write('info[',i,']=');readln(info[i]); write('st[',i,']='); readln(st[i]); write('dr[',i,']='); readln(dr[i]); end;
writeln('dati radacina : '); readln(rad); afis_2(rad);
readln; end.
2. Fiind dat un arbore binar cu nodurile l,,n si radacina 1, afiseaza listele nodurilor obtinute in urna parcurgerilor in preordine, in inordine si in postordine.
Program parcurgeri;
type sir=array[1..3O] of byte; var rad, n , I : byte;
info, st, dr : sir;
procedure preordine(I:integer); begin
write(info[I]:4);
if (st[I]<>O) then preordine(st[I]);
if (dr[I]<>0) then preordine(dr[I]); end;
procedure inordine(I:integer); begin
if (st[I]<>0) then inordine(st[I]);
write(info[I]:4);
if (dr[I]<>0) then inordine(dr[I]); end;
procedure postordine(I:integer); begin
if (st[I]<>0) then postordine(st[I]); if (dr[I]<>0) then postordine(dr[I]); write(info[I]:4); end;
begin
write('Dati numarul de noduri ale arborelui n='); readln(n); write('Dati nodul radacina rad='); readln(rad);
for I:= 1 to n do begin
writeln('pentru nodul ',I,' dati : '); write('descendentul stang:'); readln(st[i]); write('descendentul drept'); readln(dr[i]); write('informatia :'); readln(info[i]); end;
writeln(' preordine : '); preordine(rad); writeln; writeln(' inordine : '); inordine(rad); writeln; writeln(' postordine : '); postordine(rad); writeln; readln; end.
3. Fiind dat un arbore final cu nodurile notate 1,, n si radacina 1, in fiecare nod are ca informatie o valoare intreaga, sa se realizeze un subprogram, in Pascal, care afiseaza descendentul stang al nodului nod.
program arbore;
type sir=array[1..3O] of byte;
var rad, n , i : byte;
info, st, dr : sir; procedure afis_2(i:integer); begin
if (i=nod) then begin
if (st[i]<>0) then write(st[i] :4)
else write('stangul nu exista'); end else begin
if (st[i]<>0) then afis(st[i]); if (dr[i]<>0) then afis(dr[i]); end
end;
begin
write('n='); readln(n); for i:= 1 to n do begin
writeln('informatiile despre nodul ',i); write('info[',i,']=');readln(info[i]); write('st[',i,']='); readln(st[i]); write('dr[',i,']='); readln(dr[i]); end;
readln(nod);
writeln('dati radacina : '); readln(rad); afis_2(rad); readln; end.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 2701
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved