I do this code in order to build an n-ary tree with root "NULL" and the noeuds are the (Riz, pain....). But I have problem on the level of the code between // =============//. If the node N.item=debut already exists in the tree we increments the count of this node if not create a new node N ; N.count = 1 and create the link between node N and Tree .
plz give me any indications or suggestions to solve this problem
Thank you.
code :
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; //const taille=5; type Noeud= class private Fils : array[1..5] of Noeud; //liste de fils item :string; count : integer; //lien : Noeud;
constructor noeud.create ; var i : integer ; begin for i:=0 to 5 do fils:=nil ; item:='NULL' ; count :=0; //lien:='NULL';
end ;
destructor noeud.destroy ; var i : integer ; begin for i:=0 to 5 do if fils<>nil then fils.destroy ; inherited destroy ; end ;
constructor arbre.create ; begin tete:=noeud.create ; courant:=tete ; end ;
destructor arbre.destroy ; begin tete.destroy ; inherited destroy ; end ;
procedure arbre.insert_Tree(Chaine: string); const Delimiteur=','; var Debut, Fin: string; k: integer; begin // Récupère Début et Fin k:=Pos(Delimiteur,Chaine); if k=0 then begin Debut:=Chaine; Fin:=''; end else begin Debut:=Copy(Chaine, 1, k-1); Fin:=Copy(Chaine, k+1, length(Chaine)-k); end;
//============== Problème a ce niveau ============= {If the node N.item=debut already exists in the tree we increments the count of this node if not create a new node N ; N.count = 1 and create the link between node N and Tree .}
if courant.item=Debut then // Debut = first
begin inc(courant.count); courant:=courant.Fils[??] end else begin courant.fils[??]:=noeud.create ; //nouveau noeud courant:=courant.fils[??] ; courant.item:=Debut ; courant.count:=1; end ;
//============================================//
// ShowMessage(Debut);
// en s'arrete lorsque le liste est vide
if Fin='' then exit else insert_Tree(Fin);
end;
procedure arbre.buildTree(); const NbElements= 5; var T: array[1..NbElements] of string; i: integer; begin T[1]:='Riz,Pain,Eau'; T[2]:='Riz,Pain'; T[3]:='Eau,sucre'; T[4]:='Riz,Pain,Sucre'; T[5]:='Riz,Pain,Eau,Sucre'; courant:=tete ; for i:=1 to NbElements do insert_Tree(T); end;
procedure TForm1.FormCreate(Sender: TObject); begin Tree1:=arbre.create ; end; procedure TForm1.Button1Click(Sender: TObject); Begin Tree1.buildTree(); end; procedure TForm1.FormDestroy(Sender: TObject); begin Tree1.Destroy; end;