Perl Monk, Perl Meditation PerlMonks

### Re: Hierarchical Data Structures

by gmax (Abbot)
 on Mar 11, 2004 at 08:08 UTC ( #335744=note: print w/replies, xml ) Need Help??

in reply to Hierarchical Data Structures

Each person accumulates points, calculated daily for 14 days, according to how many people are below them

Tree::DAG_Node can help you to represent such structure quite easily. I've written an Introduction to Tree::DAG_Node where there is an example of such calculation made with the tree's methods.

To get you started, here is a sample hierarchy with its graphic representation and the score calculation. Swapping places and calculating optimum strategies is left as an exercise for you :).

#!/usr/bin/perl -w use strict; use Tree::DAG_Node; my \$playerA = Tree::DAG_Node->new; \$playerA->name('A'); \$playerA->new_daughter->name('A1'); my \$a2 = Tree::DAG_Node->new; \$playerA->add_daughter(\$a2); \$a2->name('A2'); \$a2->new_daughter->name('A2a'); \$a2->new_daughter->name('A2b'); \$playerA->new_daughter->name('A3'); print map "\$_\n", @{\$playerA->draw_ascii_tree},"\n"; print \$playerA->dump_names; my \$playerB = Tree::DAG_Node->new; \$playerB->name('B'); \$playerB->new_daughter->name('B1'); my \$b2 = Tree::DAG_Node->new; \$playerB->add_daughter(\$b2); \$b2->name('B2'); \$b2->new_daughter->name('B2a'); \$b2->new_daughter->name('B2b'); \$b2->new_daughter->name('B2c'); my \$b3 = Tree::DAG_Node->new; \$playerB->add_daughter(\$b3); \$b3->name('B3'); \$b3->new_daughter->name('B3a'); my \$b4 = Tree::DAG_Node->new; \$b4->name('B4'); \$b4->new_daughter->name('B4a'); \$b4->new_daughter->name('B4b'); \$b3->add_daughter(\$b4); \$b3->new_daughter->name('B3b'); print map "\$_\n", @{\$playerB->draw_ascii_tree},"\n"; print \$playerB->dump_names; \$playerB->self_and_descendants, "\n"; print "A score: ", scalar \$playerA->self_and_descendants, "\n"; print "B score: ", scalar \$playerB->self_and_descendants, "\n"; print "B2 score: ", scalar \$b2->self_and_descendants, "\n"; __END__ | <A> /--------+-------\ | | | <A1> <A2> <A3> /-----\ | | <A2a> <A2b> 'A' 'A1' 'A2' 'A2a' 'A2b' 'A3' | <B> /-----------+--------------------\ | | | <B1> <B2> <B3> /-----+-----\ /--------+--------\ | | | | | | <B2a> <B2b> <B2c> <B3a> <B4> <B3b> /-----\ | | <B4a> <B4b> 'B' 'B1' 'B2' 'B2a' 'B2b' 'B2c' 'B3' 'B3a' 'B4' 'B4a' 'B4b' 'B3b' A score: 6 B score: 12 B2 score: 4

I would recommend this module also because the documentation is exceptionally clear and informative. I've used Tree::DAG_Node in several projects.

_  _ _  _
(_|| | |(_|><
_|

Create A New User
Node Status?
node history
Node Type: note [id://335744]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-03-17 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (224 votes). Check out past polls.

Notices?