Easiest way to do this is just to set up a recursive method call to go
through each level in the tree.  Effectively you're just doing a tree
traversal, and can apply all the standard algorithms to this: your nodes are
each of the people, and can be picked out using an appropriate SQL query
(e.g. SELECT name FROM People WHERE parent = 4).  You can build this query
for each recursive call, modifying the WHERE clause appropriately.

The problem with this approach is that you can end up with a shedload of
database connections going on, and if the tree is deep or broad you'll
suffer from a hell of a slowdown related to having to initiate a query for
every single person.  You can optimise this by using more complicated WHERE
clauses on your SQL to select everyone at a given level meeting the
appropriate criteria, thereby limiting the number of recursive calls to h
(where h is the height of the tree).  You may also want to maintain a list
of all the nodes you have already processed to check for the a mistake in
the data that leads to a cyclic dependency (e.g. mary is johns mother, mary
is johns daughter).  Your data shouldn't have these in it, but it's a good
idea to check to make sure or you risk crashing the server in a big ol'
recursive loop.  There are further ways to optimise the queries (such as
unrolling your recursion into a loop), but unless this is going to be a real
heavy load situation you're likely to be better off going for code
readibility and maintainability.

As far as doing this in JSP is concerned, you should put the code to do the
actual recursive search into a bean and simply format its output with
appropriate JSP.

Hope that helps,

Chris Tucker

-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Frank Mancini
Sent: Monday, November 12, 2001 2:04 PM
To: [EMAIL PROTECTED]
Subject: Please help! Recursive loop in JSP


I have a recursive looping problem here.

For example, say I have this table

id, name, parent

1,john,4
2,bill,3
3,cathy,4
4,jackie,0
5,jeff,4
6,joe,7
7,amy,0

Essentially,

jackie doesn't have a parent, but has john, jeff and cathy as children. bill
is the child of cathy and thus jackies grandchild.


How would I do this query such that I say I want to see all the children
(including everyone below him/her) below, say jackie...this is really a self
join to oneself with an unknown number of levels below.

How would I do this in jsp do display all of jackies relatives below her?

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to