1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| 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;
public
constructor create ;
destructor destroy ;
end; { Noeud }
Arbre =
class
private
tete,courant : noeud ;
public
constructor create ;
destructor destroy ;override ;
procedure insert_Tree(Chaine: string);
procedure buildTree();
end ;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
Tree1 : Arbre;
implementation
{$R *.dfm}
constructor noeud.create ;
var i : integer ;
begin
for i:=0 to 5 do
fils[i]:=nil ;
item:='NULL' ;
count :=0;
//lien:='NULL';
end ;
destructor noeud.destroy ;
var i : integer ;
begin
for i:=0 to 5 do
if fils[i]<>nil then fils[i].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 courant.item=Debut then // Si le nud tque N.item=debut existe déjà dans larbre on incrémente le count de ce noeud
begin // Debut = élément a ajouter
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[i]);
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;
end. |
Partager