Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As LanX pointed out, you need to convert the bottom-up "Who is my boss" structure into a top-down "Who are my subordinates" structure, then print the resulting tree.

For example:

use 5.010; use strict; use warnings; # Generalized sample input data (i.e. multiple top-level groups, # each with multiple independent bosses)... my %org_chart = ( '1929' => { '3' => { 'name' => 'Amy', 'boss' => 1929, }, '4425' => { 'name' => 'Dwight', 'boss' => 1 }, '480' => { 'name' => 'Fry', 'boss' => 2 }, '1919' => { 'name' => 'Bender', 'boss' => 2, }, '1929' => { 'name' => 'Professor', 'boss' => 0 }, '3968' => { 'name' => 'Cubert', 'boss' => 1929, }, '1' => { 'name' => 'Hermes', 'boss' => 1929, }, '4' => { 'name' => 'Zoidberg', 'boss' => 1 }, '2' => { 'name' => 'Leela', 'boss' => 1 }, }, '666' => { '666' => { 'name' => 'Satan', 'boss' => 0 }, '1' => { 'name' => 'Beelzebub', 'boss' => 666 }, '777' => { 'name' => 'Lucky', 'boss' => 1 }, '99' => { 'name' => 'Damien', 'boss' => 666 }, '333' => { 'name' => 'Satan Jr', 'boss' => 0 }, '11' => { 'name' => 'Unlucky', 'boss' => 333 }, } ); # Convert each top-level group into a tree of subordinates... for my $group (values %org_chart) { for my $employee (keys %{$group}) { # Who is this employee's boss??? my $boss = $group->{$employee}{boss}; # Nothing to do if employee doesn't have a boss... next if $boss == 0; # Record that this employee is a subordinate of their boss... push @{$group->{$boss}{subordinates}}, $group->{$employee}; } } # Draw each group's org-chart (horizontally), # starting with boss-less employees... for my $group (values %org_chart) { for my $employee (values %{$group}) { # Ignore employees who have a boss... next if $employee->{boss}; # Draw the org chart for employees that don't have a boss... draw_chart($employee); } # Draw gap between groups... say ""; } # Draw each employee and their subordinates tree... sub draw_chart { my ($root_node, $level) = @_; $level //= 0; # Report the employee... say " |" x $level, "-> ", $root_node->{name}; # Recursively report their subordinates... for my $subordinate (@{$root_node->{subordinates}}) { draw_chart($subordinate, $level+1); } }

In reply to Re: out of order tree generation by TheDamian
in thread out of order tree generation by adambot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found