Pruning a tree is a bitch, let me tell you.  I've yet to come up with a
solution that only requires a single walk of the tree.

Do a walk of the tree, maintaining a list of nodeIDs (pageID or something)
that grows and shrinks as you walk.  When you find your node (the current
page), abort the walk.  The start a new walk that will actually generate
your pruned tree, and use that list that you build (which will contain the
path to the target node) to determine what branches you need to process and
which branches you can skip.  You'll probably want to prepend a NULL to the
front, so that the 'parent' of the topmost node is represented in your list.
Typically, you want to display the children of all the nodes in the list,
but nothing else.

You can use a loop for both walks, and I'd definitely recommend it for the
first walk, as a loop is a lot easier to break (CFBREAK) than a recursive
function.

Cheers,
barneyb

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Nando
> Sent: Monday, December 01, 2003 3:25 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [CFCDev] recursive functions
> 
> I'm caching the results of a few recursive function in an 
> object that create
> a tree structure of the site nav and a linked breadcrumb 
> trail for each page
> in the site. So the performance hit is only taken once in a 
> great while. I
> DO have to agree with Sean that the code is much more 
> elegant, and the depth
> of the tree is unlimited, something i didn't see how to do 
> with loops, (but
> might now if i really thought about it.)
> 
> The next challenge is how to "prune" the siteMap tree to 
> display only the
> navigation tree needed for a particular page - hopefully 
> that's easy, but i
> haven't looked into it yet.
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > Behalf Of Sean A Corfield
> > Sent: Monday, December 01, 2003 11:40 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: [CFCDev] recursive functions
> >
> >
> > On Dec 1, 2003, at 1:47 PM, Patrick Branley wrote:
> > > This is a very good point. Anything that can be done with 
> recursion
> > > can also
> > > be done using a loop.
> >
> > Sometimes implementing an algorithm with a loop makes for horrendous
> > code whereas recursive code can be elegant and simple - consider
> > algorithms for processing tree-structured data!
> >
> > > Does anybody know if there is a performance increase ? I 
> would assume
> > > there
> > > isnt.
> >
> > It depends. Recursion relies on function calls which can be 
> expensive
> > (creating a stack frame, pushing variables etc). If the algorithm is
> > sufficiently complex, the extra work you have to do to 
> manage 'context'
> > for a loop may exceed the function call overhead.
> >
> > Sean A Corfield -- http://www.corfield.org/blog/
> >
> > "If you're not annoying somebody, you're not really alive."
> > -- Margaret Atwood
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email
> > to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev'
> > in the message of the email.
> >
> > CFCDev is run by CFCZone (www.cfczone.org) and supported
> > by Mindtool, Corporation (www.mindtool.com).
> >
> > An archive of the CFCDev list is available at
> > www.mail-archive.com/[EMAIL PROTECTED]
> 
> 
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
> in the message of the email.
> 
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
> 
> An archive of the CFCDev list is available at 
> www.mail-archive.com/[EMAIL PROTECTED]
> 

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the word 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to