How to construct a treeview from a relational database

Posted on 1999-01-22
Last Modified: 2010-04-06
A table with fields son,father,data.
son is the son of the father in one tree.
now,please tell me any method to get the treeview from the
Thank you.
Question by:mathematics
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
  • 3
  • 2
  • 2
  • +1
LVL 27

Expert Comment

ID: 1362917
Hi Mathematics,

See my comment, at your q:


Accepted Solution

wamoz earned 50 total points
ID: 1362918
What you are describing is a classical problem known as the "Explosion of Parts" problem. It is intractable in a declarative language that does not support recursive evaluation, and unfortunately SQL falls into this category.

On the other hand the problem yields easily to iterative treatment.

What you should do is write a parametric SQL statement that selects all the offspring of a nominated individual, and run this each time a childless node is expanded. In the event handler procedure for node expansion you need to obtain the UID of the entity that the node represents, and use this as the parameter governing the list of entities that your query will return. You use this result set to add children to the node that is expanding.

This approach can even deal with circular references.
LVL 27

Expert Comment

ID: 1362919
hi mathematics,

I've pasted my comment here

procedure TForm1.Button1Click(Sender: TObject);
  Found : Boolean;
  I : Integer;
  t : TTreeNode;
  { Clear Treeview }
  while treeview1.Items.count > 0 do treeview1.Items[0].delete;
  { Create a root }
  while not(Table1.eof) do
    i := 0;
    Found := False;

    { Search for Father }

    while Not(Found) and (i < TreeView1.Items.Count) do
      Found := Table1.FieldByName('Father').AsString = Treeview1.Items[i].Text;
      if Not(Found) then inc(i);

    If Found then  {Father exists, add son }
      t := TreeView1.Items.AddChild(TreeView1.Items[i],Table1.FieldByName('Son').AsString);
       { Do Something else with t, i.e. some data in the option }
    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  i.e. some data in the option}
      t := TreeView1.Items.AddChild(t,Table1.FieldByName('Son').AsString);
       { Do Something else with t  i.e. some data in the option}
    Table1.Next; {Next Record}

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1362920
Thanks everybody.
LVL 27

Expert Comment

ID: 1362921
Hi mathematics,

i wonder that the theoretical answer from wamoz helped you?



Author Comment

ID: 1362922
Thank kretzschmar!
Your comment is useful for me before.
I am glad to every answer.
Hope you understand.

Expert Comment

ID: 2048901
wamoz theoretical answer was useful to me too.
I've come across a similar problem and solved it as wamoz mentioned but I wondered if SQL had a better way that I did not know.
wamoz says no, sql does not have a better way.
thanks for putting that doubt to rest

Expert Comment

ID: 2050011
The failure of SQL to support recursive evaluation is by design (ANSI committee). Relations containing circular references CANNOT be automatically materialised because they contain an infinite number of tuples.

I have written custom SQL interpreters that deal with this problem by terminating branch evaluation on detection of repeated tuples. However, when presenting this kind of information in a tree widget, it is actually *easier* to fetch one "child set" at a time as previously described, and as this leads to optimal performance, this is the preferred approach.

As a famously anonymous person once said, "If it aint broke, don't fix it."

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month7 days, 22 hours left to enroll

617 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