Discussione: [PASCAL] Alberi Binari
View Single Post
Old 12-09-2005, 15:15   #13
leadergl
Senior Member
 
Iscritto dal: May 2003
Messaggi: 1113
no...per mia sfortuna il Turbo Pascal me lo fanno usare all'univ...

Cmq..tornarno al mio esercizio ho notato che non mi da il risultato voluto..
Allo stato attuale:
1) Creo bene i due alberi di test
2) Sembra tutto ok
3) Il risultato è completamente sbagliato ed è un numero casuale che sta in memoria...

vi propongo l'esercizio così magari riuscite a vedere l'errore che io non trovo...
Testo dell'esercizio:
Quote:
Sia A un albero binario ed AO un albero binario ordinato, contenente interi. Scrivere una funzione ricorsiva che dica quante foglie di A sono presenti in AO ma non sono foglie di AO.
Questo è il mio codice:
Codice:
{Esame di Programmazione Mod. B del 20/07/2005}
{Esercizio 5}
Program Esercizio5;
Uses CRT;
Type TipoAlbero=^nodoalbero;
     nodoalbero=RECORD
                      elem:integer;
                      sx, dx:TipoAlbero;
                END;
var Al,AlO:tipoalbero;
    totale,par:integer;

function Trova(AO:TipoAlbero; elem:integer):integer;
var trovato,temp:integer;
begin
     trovato:=0;
     if (AO<>NIL) AND (trovato=0) then
     begin
          if (AO^.elem=elem) then
             if ((AO^.sx=NIL) OR (AO^.dx=NIL)) then
                trovato:=1
          else
          begin
               trovato:=0;
               temp:=Trova(AO^.sx,elem);
               temp:=Trova(AO^.dx,elem);
          end;
     end;
     trova:=trovato;
end;

function Es5(A,AO:tipoalbero; VAR parziale:integer):integer;
var temp:integer;
begin
     if A<>NIL then
     begin
          if (A^.sx=NIL) AND (A^.dx=NIL) then
             parziale:=parziale+trova(AO,A^.elem)
          else
          begin
               temp:=Es5(A^.sx,AO,parziale);
               temp:=Es5(A^.dx,AO,parziale);
          end;
     end else
         Es5:=parziale;
end;

Procedure Stampa(A:tipoalbero);
begin
     if A<>NIL then
     begin
          Stampa(A^.sx);
          writeln(A^.elem);
          Stampa(A^.dx);
     end;
end;

Procedure InserisciOrd(VAR A:tipoalbero;elem:integer);
begin
     if A=NIL then
     begin
        new(A);
        A^.elem:=elem;
        A^.sx:=NIL;
        A^.dx:=NIL;
     end else if elem<A^.elem then
         InserisciOrd(A^.sx,elem)
     else
         InserisciOrd(A^.dx,elem);
end;

Procedure Inserisci(VAR A:tipoalbero; Liv_Att,Pos_Padre:integer);
var       ElemTemp:integer;
          c:char;
begin
     if Liv_Att=0 then
     begin
          write('Inserire elemento radice: ');
          readln(elemtemp);
          Inserisci(A,1,elemtemp);
     end else
     begin
          if A=NIL then
          begin
               new(A);
               A^.elem:=pos_padre;
               A^.sx:=NIL;
               A^.dx:=NIL;
          end;
          write('Esiste il figlio sinistro dell''elemento ',pos_padre,' del livello ',liv_att-1,'? ');
          readln(c);
          if (c='s') OR (c='S') then
          begin
               write('Inserire valore: ');
               readln(elemtemp);
               Inserisci(A^.sx,liv_att+1,elemtemp);
          end;
          write('Esiste il figlio destro dell''elemento ',pos_padre,' del livello ',liv_att-1,'? ');
          readln(c);
          if (c='s') OR (c='S') then
          begin
               write('Inserire valore: ');
               readln(elemtemp);
               Inserisci(A^.dx,liv_att+1,elemtemp);
          end;
     end;
End;


{Main dell'esercizio}
begin
     clrscr;
     writeln('Creazione e Riempimento Albero: AlO');
     InserisciOrd(AlO,28);
     InserisciOrd(AlO,15);
     InserisciOrd(AlO,31);
     InserisciOrd(AlO,13);
     InserisciOrd(AlO,20);
     InserisciOrd(AlO,34);
     InserisciOrd(AlO,5);
     InserisciOrd(AlO,14);
     InserisciOrd(AlO,33);
     writeln('Creazione e Riempimento Albero: Al');
     Inserisci(Al,0,0);
     writeln('Avvio Esercizio 5');
     par:=0;
     totale:=0;
     totale:=Es5(Al,AlO,par);
     writeln;
     writeln('Elementi di A non presenti in Al0 che soddisfano: ',totale);
     writeln;
     writeln('Premere un tasto per continuare!');
     readln;
end.
__________________
| Athlon XP Barton 3000+ | CoolerMaster HAC-V81 | ASUS A7N8X DELUXE v2.0 | 2*256 PC3200 + 1*512 PC3200 = 1GB DDR400| ATI Radeon 9250 | HD 80Gb Maxtor SATA | Ali Q-TEC 550W Dual Fan GOLD PFC
leadergl è offline   Rispondi citando il messaggio o parte di esso