The Artima Developer Community
Sponsored Link

Design Forum
Build n-ary tree with delphi

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
ma mo

Posts: 1
Nickname: hello05
Registered: Oct, 2005

Build n-ary tree with delphi Posted: Oct 10, 2005 4:46 PM
Reply to this message Reply
Advertisement
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;

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:=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;

end.

Plz Help

Topic: How to create ERD diagram from starting document Previous Topic   Next Topic Topic: database design

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use