Bonjour j'aimerais programmé un arbre équilibré maismon programme plante. IP cute me dit que des données sont dans le tas mais ne peuvent être atteintes.
Je sais que le listing ci dessous est incomplet, il en manque la moitiée mais dans l'exemple que je prnd, ç ne pose pas de pb.
Program Arbreseq;
type parbre=^arbre;
arbre=record nom:string; {$V 0:40}
bal:integer; {$V 40:60}
g,d:parbre;
end;
var a,b:parbre;
cons,voy:string;
bo:boolean;
function creer(s:string):parbre;
begin
new(result);
result^.nom:=s;
result^.d:=nil;
result^.g:=nil;
result^.bal:=0;
end;
function insere(cle:string;var p:parbre):boolean;
var a:parbre;
begin
new(a);
if p=nil then begin
p:=creer(cle);
result:=true
end
else if p^.nom<cle then begin
if insere(cle,p^.d) then begin
p^.bal:=p^.bal-1;
If p^.bal=0 then result:=false
else if p^.bal=-1 then begin
a:=p;
p:=p^.g;
a^.g:=p^.d;
p^.d:=a;
dispose(a);
result:=false
end
else if p^.bal=-2 then begin
if p^.g^.bal=-1 then begin
a:=p;
p:=p^.g^.d;
a^.g:=p^.d;
p^.g:=a;
p^.d:=a^.d;
dispose(a);
result:=false
end
end
else result:=false
end
else begin
if insere(cle,p^.g) then begin
p^.bal:=p^.bal-1;
If p^.bal=0 then result:=false
else if p^.bal=-1 then begin
a:=p;
p:=p^.g;
a^.g:=p^.d;
p^.d:=a;
dispose(a);
result:=false
end
else if p^.bal=-2 then begin
if p^.g^.bal=-1 then begin
a:=p;
p:=p^.g^.d;
a^.g:=p^.d;
p^.g:=a;
p^.d:=a^.d;
dispose(a);
result:=false
end
end
end
else result:=false
end
end;
end;
BEGIN
// Là je créé un arbre d'exemple
a:=creer('h');
a^.g:=creer('e');
a^.d:=creer('k');
a^.d^.d:= creer('o');
a^.d^.g:= creer('n');
a^.g^.d:=creer('g');
a^.g^.g:=creer('c');
a^.g^.d^.g:=creer('f');
a^.bal:=-1;
a^.g^.bal:=-1;
a^.g^.d^.bal:=-1;
bo:=insere('p',a);
dispose(a);
dispose(b);
END.
Merci
