On Wed, 2006-08-16 at 15:36 +0000, spacedman wrote:
> 
> 一首诗 wrote:
> > I have a table like this
> >
> > id  | name | parentid
> >
> > And parentid is a Foreign Key to ID.
> >
> > So I have a tree in my database.  My question is that, how can I
> > represent it on the web as a tree.
> 
> If you can rework your database slightly you can use Modified Preorder
> Tree Traversal, which is a really slick way of doing trees in
> relational databases. You can get everything you need to know to
> present a tree diagram with ONE database call. If you store parent-id
> then you can end up hitting the database many many more times as you
> climb up and down the tree.

Whilst this is a good data structure for many things, anybody thinking
of using it should also be aware of the trade-offs: insert, move and
delete are costly operations (relatively speaking). So if you are going
to be shuffling your tree a lot, you should do some performance testing
on typical data sizes to see if it meets your needs.

There are other, even trickier, storage systems for trees in databases
(Celko has written a whole book on some of them and doesn't even get to
the recent ideas) and they all have trade-offs in one place, whether it
be code complexity, or storage limitations (some depend upon storing a
couple of numbers in a pair of columns, so they have some theoretical
upper limits you sometimes hit) or, as with preorder-traversal, editing
costs. In each case, evaluating the possibilities in light of what you
are using it for is important.

I would also just mention that the simplistic approach (having parent
field) doesn't perform that badly for small structures. For example, I
can construct the whole tree for the tag hierarchy in my blog in only a
few microseconds. For small (say, a few dozen) datasets, the naïve
approach is possibly a good start, just to get something working.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to