J'ai écrit une fonction d'arbre récursive dans pascal (ou delphi) mais j'avais un message 'Mémoire insuffisante' quand je l'ai exécuté. je dois activer la fonction récursive Calculer dans ce code à la fonction non récursive, pouvez-vous me dire comment s'il vous plaît:Comment convertir une fonction récursive arborescente (ou un algorithme) en une boucle?
program testing(input,output);
type
ptr = ^tr;
tr = record
age:byte;
left,right:ptr;
end;
var
topper:ptr;
total,day:longint;
procedure mycreate(var t:ptr);
var temp:ptr;
begin
new(temp);
temp^.age:=1;
temp^.left:=nil;
temp^.right:=nil;
t:=temp;
end;
procedure gooneday(var t:ptr);
begin
if t^.age<>5 then
begin
if t^.age=2 then
mycreate(t^.left)
else if t^.age=3 then
mycreate(t^.right);
t^.age:=t^.age+1;
total:=total+1;
end;
end;
procedure calculate(var crnt:ptr);
begin
if crnt<>nil then
begin
gooneday(crnt);
calculate(crnt^.left);
calculate(crnt^.right);
end;
end;
begin
total:=0;
mycreate(topper);
day:=0;
while total<1000000000000 do
begin
total:=0;
day:=day+1;
calculate(topper);
end;
writeln(day);
writeln(total);
end.
Veuillez éditer votre question. Veuillez lire les instructions sur le côté droit de la page pour le formatage du code. –