2006/6/4, Niels <zorglub_olsen@xxxxxxxxxxx>:
Hi, I have a set of nodes. Each node has a parent and so the set can be thought of as a tree. I want to show that tree somehow on a webpage, served by PHP. I cannot use Dot/Graphwiz for various reasons. What I'm looking for is an output of DIVs or tablecells, showing the nodes and their connections. It's not a trivial task, IMO, but doable. Possibly somebody has already made something similiar, but I can't find anything on Google. Can anybody point me to helpful information? Thanks, Niels -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
I had a similar problem that, although it was with a binary tree, it can be used with your tree. PHP doesn't like too much the use of recursion, but this time recursion is the way to go (if you want to keep the code maintainable). Hopefully the tree will not span deep enough to cause any problems... (hopefully). The function will receive a node and return a table representation of the branch started by this tree, if you point this function to the root node you'll have your table. In each cell it will have to indicate whether it is empty, it has a node or a line connecting neighbor cells. One important thing to define here is the way the function will organize the nodes in the table, in my function the root node was at the top in the center. First, the trivial case: if the node doesn't have children return a table with one cell, the node itself. Then, the recursive case: if the node have children, call the function with those nodes and store the tables returned. According to the representation I used, it calculated the combined width of those tables, created a new table where the first row contained the parent node centered, the second line an horizontal line from the column where the first children would be to the column where the last children would be. Then it build the rest of the table pasting together, horizontally, the tables of the children. Voila! a nice table representation of the tree.