One way to display a tree would be so that it looks like this:
- - Sally
- - Mary
- - Tim
- - Albert
- - - Andy
- - - Terry
- - Ryan
- - - Greg
- - Jeff
- - Sally
Similar to a "tree-view" control. Conceptually, this is how a lot of menus are displayed.
This can be done with the simple dashes as I show, or with columns and rows in a table
Displaying a real tree-like structure as you describe but that is a lot more work. It certainly could be done, but will require some sophisticated manipulation of html tables. You would probably want to develop some images that allow you to construct tables that display the names and interconnecting lines. An easy way to look at the source html at sites that display this sort of hierarchical info: ancestry, dog breeding, sports championships or something like that.
As far as the code goes, you have pretty much coded the basic idea from my earlier post. Using a recursive pattern is typically how this sort of thing is done, just as you have done.
Also, there are other approaches. You could build a tree structure in code (using objects and dictionaries - this is known as a composite pattern). Once you have that, you just "traverse the tree" and build the html output as you go.
There are many approaches to building the tree structure. The psuedo code for one way is as follows:
Retrieve all rows in a single db query
Loop through the records, and for each record
- create a "person node" composite object which is a class you would have to define that contains a field for the name, a field for the parent name, and a dictionary of person objects which are the children of this person, and methods for adding children and displaying itself.
- add it to an "all persons" dictionary keyed by the name
Then loop through the dictionary and for each person object:
- If the parent field contains a value (all records but "grandpa" will)
- - Get the perons object from the dictionary for that parent
- - Add a reference to the person object to the parent objects child dictionary by calling the addChild method you wrote on the person class
- - Optionally, you can also add a reference to the parent in the child. This sort of circular reference can be very useful, but you want to make sure you have a method to break all references when you destroy the structure.
Now you are ready to display.
To do the simple display I showed above with the dashes you merely traverse the tree, adding the appropriate dashes as you go. This is also a recursive method of doing this work. The advantage to this sort of scheme is that you can build in the functionality you need into a generic composite pattern and use it for displaying the tree in any manner you like.
Your simple solution is probably good enough for a simple display, but when things start getting complicated you will want to be able to know the size of the tree (how many generations deep and how many persons wide) from the start so you can adjust the display accordingly to make sure you have room for everything. There are lots of different ways people build tree displays of this time with various tricks for positioning things.
Like I have been saying, there are numerous ways to do things like this. Another approach is to use a linked list or a doubly linked list.
How to use a forum to help solve problems