mathematics
asked on
Generate a treeview
I have a dataset with fields 'son and 'father'.
(root with father=spaces)(likely subdirectory directory)
Do you know any algorithm generate a tree with
this dataset?
Please tell me. Thanks.
(root with father=spaces)(likely subdirectory directory)
Do you know any algorithm generate a tree with
this dataset?
Please tell me. Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi mathematics,
instead teh table i used, you can do it with a dataset also,
procedure TForm1.Button1Click(Sender : TObject);
var
Found : Boolean;
I : Integer;
t : TTreeNode;
begin
{ Clear Treeview }
while treeview1.Items.count > 0 do treeview1.Items[0].delete;
{ Create a root }
treeview1.Items.Add(treevi ew1.TopIte m,Table1.T ableName);
table1.open;
table1.first;
while not(Table1.eof) do
begin
i := 0;
Found := False;
{ Search for Father }
while Not(Found) and (i < TreeView1.Items.Count) do
begin
Found := Table1.FieldByName('Father ').AsStrin g = Treeview1.Items[i].Text;
if Not(Found) then inc(i);
end;
If Found then {Father exists, add son }
begin
t := TreeView1.Items.AddChild(T reeView1.I tems[i],Ta ble1.Field ByName('So n').AsStri ng);
{ Do Something else with t }
end
else
begin {Father not exists, add Father to root, add son }
t := TreeView1.Items.AddChild(T reeView1.T opItem,Tab le1.FieldB yName('Fat her').AsSt ring);
{ Do Something else with t }
t := TreeView1.Items.AddChild(t ,Table1.Fi eldByName( 'Son').AsS tring);
{ Do Something else with t }
end;
Table1.Next; {Next Record}
end;
table1.Close;
end;
can be optimized, of course
meikl
instead teh table i used, you can do it with a dataset also,
procedure TForm1.Button1Click(Sender
var
Found : Boolean;
I : Integer;
t : TTreeNode;
begin
{ Clear Treeview }
while treeview1.Items.count > 0 do treeview1.Items[0].delete;
{ Create a root }
treeview1.Items.Add(treevi
table1.open;
table1.first;
while not(Table1.eof) do
begin
i := 0;
Found := False;
{ Search for Father }
while Not(Found) and (i < TreeView1.Items.Count) do
begin
Found := Table1.FieldByName('Father
if Not(Found) then inc(i);
end;
If Found then {Father exists, add son }
begin
t := TreeView1.Items.AddChild(T
{ Do Something else with t }
end
else
begin {Father not exists, add Father to root, add son }
t := TreeView1.Items.AddChild(T
{ Do Something else with t }
t := TreeView1.Items.AddChild(t
{ Do Something else with t }
end;
Table1.Next; {Next Record}
end;
table1.Close;
end;
can be optimized, of course
meikl
Sorry. This is right
If Id is primary key
and father is pointing to father record
// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
currentNode := <-- append current rec to tree root
appendSubtree(Id, CurrentNode)
DataSet.Next
end
Procedure appendSubtree(AnId, ANode)
SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)
While Not SubDataSet.Eof Begin
SubTreeNode := <-- append current rec under ANode
AppendSubTree (Id, SubTreeNode)
SubDataSet.Next
End
End
If Id is primary key
and father is pointing to father record
// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
currentNode := <-- append current rec to tree root
appendSubtree(Id, CurrentNode)
DataSet.Next
end
Procedure appendSubtree(AnId, ANode)
SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)
While Not SubDataSet.Eof Begin
SubTreeNode := <-- append current rec under ANode
AppendSubTree (Id, SubTreeNode)
SubDataSet.Next
End
End
Cheers,
Raymond.