#!/usr/bin/perl --
use strict; use warnings;
use Tree::Simple;
my $tdat = <<'SHABBA';
1 -> 2
2 -> 0
3 -> 4
4 -> 2
5 -> 10
6 -> 10
7 -> 9
8 -> 9
9 -> 6
10 -> 4
11 -> 10
12 -> 10
13 -> 12
14 -> 10
15 -> 2
SHABBA
my %nodes;
while( $tdat =~ /^(\d+)\D+(\d+)$/mg ){
#~ my( $parent_id, $child_id ) = ( $1, $2 );
my( $child_id , $parent_id ) = ( $1, $2 );
my $parent = $nodes{$parent_id} ||= Tree::Simple->new( $parent_id
+);
my $child = $nodes{$child_id} ||= Tree::Simple->new( $child_id );
$parent->addChild( $child );
}
for my $k ( sort { $a <=> $b } keys %nodes ){
my $tree = $nodes{$k};
use Tree::Simple::View::ASCII;
my $tree_view = Tree::Simple::View::ASCII->new( $tree );
$tree_view->includeTrunk(1);
print $tree_view->expandAll(), $/, $/;
$tree->traverse(sub {
my ($_tree) = @_;
my $d = $_tree->getDepth();
print ("($k)($d)", ("\t" x $d), $_tree->getNodeValue(), "\n");
});
print "($k) getHeight ", $tree->getHeight, "\n";
print "($k) getWidth ", $tree->getWidth, "\n";
print "($k) getDepth ", $tree->getDepth, "\n";
print "#" x 12, "\n";
}
__END__
Not sure if this is what you want, but the Height/Width number seems to jive (7) , the depth is zero based
0
|---2
|---1
|---4
| |---3
| |---10
| |---5
| |---6
| | |---9
| | |---7
| | |---8
| |---11
| |---12
| | |---13
| |---14
|---15
(0)(0)2
(0)(1) 1
(0)(1) 4
(0)(2) 3
(0)(2) 10
(0)(3) 5
(0)(3) 6
(0)(4) 9
(0)(5) 7
(0)(5) 8
(0)(3) 11
(0)(3) 12
(0)(4) 13
(0)(3) 14
(0)(1) 15
(0) getHeight 7
(0) getWidth 7
(0) getDepth -1
############
I would be so grateful! Pics? ;) |