Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data structure theory: Infinite nesting stored in database.....

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
I am trying to figure out the most efficient way to accomplish this:

In a SQL database a table exists that has a list of nodes. When SELECT'd out of the table an algorithm should be able to organize the nodes into a tree-like structure with the following rules:[ul][li]The root element is null[/li][li]All elements (including null) can have infinite child nodes, including none[/li][li]No implicit order or structure is defined to account for searching, ect. other than the parent-child relationship of the tree itself[/li][/ul]

As of now my solution defines a table something like this:
Table nodes(
varchar(32) ID
varchar DATA,
varchar(32) PARENT
)

and the recursive algorithm to build the tree is something like this:
Code:
sub build_tree (nodeList, parent_id):
   j := 0;
   children := new List;

   while counter < size nodeList:
      if list[j]->parent = parent_id:
         k := size children;
         children[k]:=list[counter]
         children[k]->children := build_tree(nodeList, children[k]->id)
      endif
      increment counter
   endwhile

   return children
endsub

At least I think that works....something like that anyway. Near as I can tell building this structure is O(n) where n=the deepest nest (the degree I suppose).

In searching on google, I found some information on trees where nodes have unlimited numbers of direct children. Their solution specified a next-sibling pointer, effectively making a linked list out of the siblings.

Any ideas as to how to improve this process? The database table is changeable.

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
I haven't figured this out yet myself (eventhough I wish I had the time to). Here's a link to a site that you may find interesting.


I originally saw this link because of this thread: thread1551-1217009

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top