• Status: Solved
• Priority: Medium
• Security: Public
• Views: 194

# How to construct a treeview from a relational database

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
table.,
Thank you.
0
mathematics
• 3
• 2
• 2
• +1
1 Solution

Commented:
Hi Mathematics,

See my comment, at your q:

http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10116479

meikl
0

Commented:
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.
0

Commented:
hi mathematics,

I've pasted my comment here

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 }
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
{ Do Something else with t, i.e. some data in the node.data option }
end
else
{ Do Something else with t  i.e. some data in the node.data option}
{ Do Something else with t  i.e. some data in the node.data option}
end;
Table1.Next; {Next Record}
end;
table1.Close;
end;

meikl
0

Author Commented:
Thanks everybody.
0

Commented:
Hi mathematics,

i wonder that the theoretical answer from wamoz helped you?

meikl

0

Author Commented:
Thank kretzschmar!
Your comment is useful for me before.
Hope you understand.
0

Commented:
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
0

Commented:
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."
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.