Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: how to construct tree from parent pointer list

by bryank (Acolyte)
on Jun 30, 2009 at 16:00 UTC ( [id://776080]=note: print w/replies, xml ) Need Help??


in reply to Re^3: how to construct tree from parent pointer list
in thread how to construct tree from parent pointer list

Hi, Can you explain what the pieces of this does

sub visit_preorder { my ($cb, node, $depth) = @_; $depth ||= 0; $cb->($node, $depth); visit_preorder($cb, $_, $depth+1) for $node->children(); }

For example, what is '$cb' ? What I am trying to do is output the tree in a flattened format once the tree is built. Something like : Fruit|Apple|Granny Smith.. Fruit being the parent of Apple, Apple being the parent of Granny Smith..

Replies are listed 'Best First'.
Re^5: how to construct tree from parent pointer list
by ikegami (Patriarch) on Jun 30, 2009 at 19:21 UTC

    For example, what is '$cb'

    The first argument. It's expected to be a code reference.

    What I am trying to do is output the tree in a flattened format once the tree is built.

    Based on the code from Re: Tree path analyzer regex problem (maybe other issues)?, a reply in an older thread of yours,

    sub leaves { my $node = shift; my @children = $node->children(); if (!@children) { print(join('|', @_), "\n"); } else { for my $child (@children) { leaves($child, @_, $child->value()); } } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found