Re: Help with Reverse Tree Traversal using Recursion ..

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard -

Thanks for the helpful suggestion but that won't resolve my problem
the way I currently have it written because I'm creating a new, nested
<ul> each time I loop through:

print "<ul>";
foreach ($parent as $key => $value) {
...
}
print "</ul>";

So when I tried this, the output was still inverted because it began
printing at the innermost branch of the nested list.

I'm trying to figure out a way now to save the output into an array
instead of outputting and then traversing the array in a separate
function to output it. Might run into the same problem again but I'm
hopeful that once the data is an array, I can make use of a php
function to reverse or sort the array.

Another reader also recommended this article on php and recursion:

http://www.zend.com/zend/art/recursion.php

I'll post if I get any closer,

- Greg


On 11/14/05, Richard Lynch <ceo@xxxxxxxxx> wrote:
> On Mon, November 14, 2005 7:46 am, Greg Schnippel wrote:
> > I have a large data tree that I wanted to display in an outline
> > format. I used a textbook tree recursion function to go through the
> > data and display it as nested unordered lists:
> >
> > function outputTree($id) {
> >
> >        if (checkChildObjectExists($id)) {
> >                print "<ul>";
> >                $child = getChildArray($id);
> >                foreach ($child as $key => $value) {
> >                        print "<li><a
> > href=\"/object/$key/\">$value</a></li>";
> >                        outputTree($key);
> >                }
> >                print "</ul>";
> >        }
> > }
> >
> > This works as expected:
> >
> >> Level 1 (Top)
> >    > Level 2
> >       > Level 3 (Bottom)
> >
> > However, I also want to display a reverse tree to allow users to trace
> > their way back up the tree after landing on a lower branch. I tried to
> > be clever and reverse the function above as:
> >
> > function outputReverseTree($id) {
> >
> >  if (checkParentExists($id)) {
> >     print "<ul>";
> >     $parent = getParentArray($id);
> >     foreach ($parent as $key => $value) {
>
> Just swap the order of these two lines:
> >       print "<li><a href=\"/object/$key/\">$value</a>\n";
> >       outputReverseTree($key);
>
> So that you walk up to the ROOT of the tree *before* you start
> printing stuff out.
>
> >     }
> >     print "</ul>";
> >   }
> > }
> >
> > Which works, but there is something wrong with my logic as the tree
> > comes back reversed:
> >
> >> Level 3 (Bottom)
> >    > Level 2
> >       > Level 1 (Top)
> >
> > Any suggestions on how to do this?
>
>
> --
> Like Music?
> http://l-i-e.com/artists.htm
>
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux