Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generate a treeview

Posted on 1999-01-11
4
Medium Priority
?
179 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:mathematics
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1355996
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.
0
 
LVL 1

Accepted Solution

by:
apaparis earned 200 total points
ID: 1355997
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
   appendSubtree(Id)
   DataSet.Next
end

Procedure appendSubtree(AnId)
   currentTreeNode := <-- append current rec to tree root
   SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)

   While Not SubDataSet.Eof Begin
      AppendSubTree (Id)
      SubDataSet.Next
   End
End

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355998
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
0
 
LVL 1

Expert Comment

by:apaparis
ID: 1355999
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

 
 
 
   
 

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question