Jeff: I never had any "old CS days" to forget, but the best approach
to tree-building that I've found is a single id/parentid query wed to
a set of parallel structures that are connected by reference.

<cfquery>
    FETCH YOUR IDs and PARENTIDs
</cfquery>

<!--- CREATE PARALLEL STRUCTURES --->
<cfset stcTree = StructNew()>
<cfset stcFlat = StructNew()>

<cfloop query="qryNodes">
        <cfset node = StructNew()>
        <cfset node.id = id>
        <cfset node.parentid = parentid>
        <cfset stcFlat["n#id#"] = node>
        <cfif NOT StructKeyExists(stcFlat, "n#parentid#")>
                <cfset stcTree["n#id#"] = stcflat["n#id#"]>
        <cfelse>
                <cfset stcflat["n#parentid#"]["n#id#"] = stcflat["n#id#"]>
        </cfif>
</cfloop>

You can recursively walk stcTree to display it graphically, or use
node.parentid and node.id to quickly crawl up the stcFlat to create a
breadcrumb trail. And unlike other methods, it also gives you an easy
way to snap a branch off the tree and do something with it.

--
Roger Benningfield
free blog hosting for MX people
http://mxblogspace.journurl.com/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:194820
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to