Skip to main content
gamedev.net
🔒 Locked

Tree

Started by H0bbes Jun 29, 2004 at 1:17 PM 2 replies 400+ views
Original Post
H0bbes
H0bbes
I stay programing with VB.NET I want to have a table like a list that represents a tree. I use DataTable Class to do my table. Can you sugest how to do this table.
Verg
Verg
Is your data heirarchal? If so, you'll need containers (that would be represented by the nodes on the tree) and data elements, that would be represented outside the tree... as in a ListView pane. (I don't work with VB much, so no specific code for you... sorry)

Or, are you doing something different than a Windows Explorer like app?
H0bbes
H0bbes
i do a app, however i try with hastable i think that is better.

tank you for your help.
Xai
Xai
The standard way to store a tree in a relational table is simply to have a self reference, storing the primary key of the parent element ...

Say your table would look like this:

int Id
string Name
... other data ...

just add a column of the same type as the primary key

int ParentId

now of course, you will have to write a recursive (or iterative using stack of course) algorithm to generate a view of the tree, but this is not very hard. I have written just such an algortihm for a coworker in both C# and PHP in just the last 2 weeks.

And simply queries like: WHERE (ParentId = @CurrentId) - will give you all the children of a given node. WHERE (ParentId = NULL) will give you the root element(s).

some other single level operations are very easy too ... it's just the multilevel ops that need looping / recursion.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.