Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Hierarchical Tree Traversal

by Raad (Acolyte)
on Jul 15, 2004 at 19:31 UTC ( #374800=note: print w/replies, xml ) Need Help??

in reply to Re: Hierarchical Tree Traversal
in thread Hierarchical Tree Traversal

$idy is essentially an alias for the id generated from the result sql statement. I shift it to replace $root so that it becomes the iterator for the id comparisons. I've used it twice for clarity and I think I was having a problem using $id in my script because it's also used as a field in my data. $i is just a reference of ... oops I see that I should have used $i with my while rather than $idy.

I've spent a day familiarizing myself with DBIx::Tree::NestedSet but I can't figure out how to traverse the tree programmatically. It looks like it's not designed to populate the left and right ids!

Replies are listed 'Best First'.
Re^3: Hierarchical Tree Traversal
by Hero Zzyzzx (Curate) on Jul 16, 2004 at 00:23 UTC

    It creates the left and right values automatically (I'm the author of DBIx::Tree::NestedSet) when you invoke any method that deletes/moves/adds nodes. What use would the module be if it didn't? Did you even try the code I posted in the other thread you created?

    With a nested set you don't need a parent ID: the hierarchy is created via left and right. Managing a parent id is redundant.

    Traversing the tree is right in the docs.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

    My Biz

      I am afraid I am not versed enough in Perl's complex data structures and Object-Oriented programming to fathom how the module would generate right and left ids without using a parent id as a point of comparison! I just had a chance to look at the code and it calls for explicit usage of all the nodes. To be honest with you, I was looking for a quick and dirty way of building the ids without having to specify explicitly what my nodes are. I'll be happy to use your wonderful module if it would allow me to traverse the tree. In fact, now that you confirmed that it was designed for that purpose, I am planning to re-read the docs again and study your code to see how I can traverse the tree programmatically. I really appreciate your time in developing the module and just wish I was bright enough to understand all its capabilities. Please bear with this poor, un-enlightened monk!
Re^3: Hierarchical Tree Traversal
by Raad (Acolyte) on Jul 15, 2004 at 19:50 UTC
    Thanks for directing me in the right direction. The problem was indeed the $i reference. Also I had a mistake in my right statement. The correct algorithm to generate right and left ids for a hierarchical tree should be:
    my $root =1; my $counter = 1; rebuild_tree($root); sub rebuild_tree{ my $idy = shift; my $result= $dbh->prepare("SELECT id FROM terms WHERE parent_id = ?"); my $left = $dbh->prepare("UPDATE terms SET left_id=? WHERE id = ?"); my $right= $dbh->prepare("UPDATE terms SET right_id=? WHERE id = ?"); $left->execute($ctr++,$idy); $result->execute($idy); while (my $i = $result->fetchrow_hashref()) { rebuild_tree($i->{id}); } $right->execute($ctr++,$idy); }#end of sub
    The only way I could get it to work is by inserting the prepare handles in the sub.

      Since you are calling it recursivly you WANT to prepare them in there. Otherwise you handles would be re-executed before they finished fetchrowing. :)

      Eric Hodges

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://374800]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2021-05-17 19:12 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (159 votes). Check out past polls.