Nested Set Trees in ColdFusion (v1.0)

A common technique to manage hierarchical data in a relational database is to use an "Adjacency List" model, where you have both an ID column and a Parent ID column in a table. This is easy to understand and maintain but can be difficult or inefficient when you want to retrieve hierarchies of records.

The "Nested Set" model provides an alternative technique for managing this kind of data and is more efficient at reading a hierarchy but requires a little more work for inserts, updates, deletes and moves.

This project provides a library of ColdFusion code that you may like to use to help manage your hierarchical data using the nested set model.

More ...

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
6 Jun 2009 11:12AM
Raul Riera said:
Raul Riera's Gravatar There is no link or SVN to download the code on riaforge
6 Jun 2009 03:32PM
Kevan Stannard's Gravatar Hi Raul

Thanks for letting me know. Should have a download link now.

http://nstree.riaforge.org/
6 Jun 2009 08:47PM
zac spitzer said:
zac spitzer's Gravatar oracle has had "connect by" for donkey years, plus some tweaks in 10g

it make tree a total walk in the park

wish the other dbs would support it
6 Jun 2009 09:08PM
Kevan Stannard's Gravatar Hi Zac, thanks, good to know. At least MSSQL 2008 is supporting hierarchical structures now as well.
6 Jun 2009 09:13PM
zac spitzer said:
zac spitzer's Gravatar cool, that's good to know
3 Jul 2009 08:18AM
Hussein Grant said:
Hussein Grant's Gravatar Congrats on the 1.0 release.
This is great!
3 Jul 2009 02:39PM
Kevan Stannard's Gravatar Thanks Hussein, and thanks for all of your feedback along the way.
26 Jul 2009 11:58PM
John Lampacher said:
27 Jul 2009 12:52AM
Kevan Stannard's Gravatar John, thanks for the link!

For anyone reading, the link John provided demonstrates an alternative technique for managing hierarchical data which is based on maintaining a "lineage" column which is essentially a string of delimited values.
19 Oct 2009 01:57PM
Laker Netman said:
Laker Netman's Gravatar I am trying to devise a query that will return the oldest parent node "X" of a specific child node "Y", i.e., the first node immediately below the root that is in the hierarchy where "Y" lives.
I have Celko's "Trees And Hierarchies..." book which provides an SQL-92 query that outlines a solution, but I am having trouble getting it working in MSSQL 2000.
Can anyone post a query or link to an article that might help?

TIA,
Laker
20 Oct 2009 02:22AM
Kevan Stannard's Gravatar Hi Laker

You might be able to get this working using a slight variation of the query that you'd use for getting a breadcrumb list.

The normal breadcrumb query is:

select
c.*,
nstB.lft,
nstB.rgt
from
categoryNST nstA
cross join categoryNST nstB
inner join category c on nstB.id = c.categoryId
where
nstA.lft between nstB.lft and nstB.rgt
and nstA.id = {categoryId}
order by
nstB.lft

The modified version that just returns the first node immediately below the root is:

select top 1 -- ONLY USE THE FIRST NODE
c.*,
nstB.lft,
nstB.rgt
from
categoryNST nstA
cross join categoryNST nstB
inner join category c on nstB.id = c.categoryId
where
nstA.lft between nstB.lft and nstB.rgt
and nstB.lft > 1 -- EXCLUDE THE ROOT NODE
and nstA.id = #categoryId#
order by
nstB.lft
20 Oct 2009 09:04AM
Laker Netman said:
Laker Netman's Gravatar Brilliant! Thank you very much. That's exactly what I was looking to do.

Cheers,
Laker
2 Dec 2009 04:51PM
Adam said:
Adam's Gravatar I can't seem to move a node between the root node and the first level 1 node.

I something like:

-root
--cat 1
---cat 1a
----cat 1b
---cat 1aa
----cat 1bb
--cat 2
--cat 3
---cat 3a

and I want to move cat2 to between root and cat 1. Should this be possible - I have made some changes to the downloaded templates which may be breaing something?
2 Dec 2009 07:51PM
Kevan Stannard's Gravatar Hi Adam

Yes, that's a valid move. I just tested using the nstreedemo app from the download and was able to make 'cat 2' the first child of 'root' (i.e. between root and cat 1)
3 Dec 2009 03:13AM
Adam said:
Adam's Gravatar Hmmm. I replaced the showtree.cfm view with the one from the download package but still I cannot move the node to where I want it. This was the only file I changed so not sure why I can't move it.

The node I want to move is a direct child of the root node and I simply want to move it above the current first node as you mention. The current first child has several sub cats so not sure if that is an issue?
3 Dec 2009 05:10AM
Adam said:
Adam's Gravatar Hi Kevan,

Thanks for your reply. I haven't been able to make the move as described. I've copied the showtree.cfm view from the download package over the modified version I was using and it still doesn't work. This is the only file I've changed so I'm not sure why it doesn't perform the move.

Any help appreciated.
3 Dec 2009 02:39PM
Kevan Stannard's Gravatar Hi Adam

In the demo app are you using the "Move To" drop down or the "Move Up/Move Down" links? The Move To drop down is for effectively changing the "parent" of a node, rather than for reordering nodes. The library supports what you are trying to do but the nstreedemo only shows a subset of functionality and achieves this via the move up/move down links.

If this is not what you are experiencing can you describe how you are trying to move the nodes?

Thanks
3 Dec 2009 03:16PM
Adam said:
Adam's Gravatar Apologies for the multiple posts - I wasn't sure that the post was sticking.

I'm trying to move the node using the Move Up/Move Down links and not the 'move to' dropdown. I'm using the nstree to drive the sections of my site (e.g. the main section navigation) and I want to add a 'home' node. Of course I want this to be the first child of the root so that it appears first in the main nav.

After I create the new node I can move it up once but then it won't move up again past the current first node (which has nested nodes itself)...! There are currently only three child nodes off the root.

Are you saying that the demo does or doesn't support what I am trying to do? I don't see why I shouldn't be able to move it up to the first child position (as you state that you can do it)...

I can post a link tomorrow to show you what is happening if that helps explain?

Thanks,

Adam
5 Dec 2009 04:06AM
Kevan Stannard's Gravatar Hi Adam, yes demo code certainly can do what you describe. It sounds like there may be a problem with the data. You can perhaps either delete the data and re-enter, or run the reindex utility to ensure the left and right values are correct. Otherwise feel free to put something online or email me whatever you've got and I'll take a quick look (kevan at this domain).
5 Dec 2009 02:31PM
Adam said:
Adam's Gravatar Hi Kevan, I ran the reindex utility and it's working fine now! Many thanks for your help.
Add a comment
(will not be published)
(include http://)