Link to home
Start Free TrialLog in
Avatar of mathematics
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.
Avatar of rwilson032697
rwilson032697

There are some data aware tree views around. Take a look on DSP(http://sunsite.icm.edu.pl/delphi) - there is a search engine.

Cheers,

Raymond.
ASKER CERTIFIED SOLUTION
Avatar of apaparis
apaparis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
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(treeview1.TopItem,Table1.TableName);
  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').AsString = Treeview1.Items[i].Text;
      if Not(Found) then inc(i);
    end;

    If Found then  {Father exists, add son }
    begin
      t := TreeView1.Items.AddChild(TreeView1.Items[i],Table1.FieldByName('Son').AsString);
       { Do Something else with t }
    end
    else
    begin     {Father not exists, add Father to root, add son }
      t := TreeView1.Items.AddChild(TreeView1.TopItem,Table1.FieldByName('Father').AsString);
       { Do Something else with t }
      t := TreeView1.Items.AddChild(t,Table1.FieldByName('Son').AsString);
       { 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