Algorithm-Tree-NCA
view release on metacpan or search on metacpan
# -*- Mode: Perl -*-
# Copyright 2002 by Mats Kindahl. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
package Node;
sub new ($@) {
my($class,@children) = @_;
my $self = { _children => [@children] };
bless $self,$class;
}
sub children {
my($self) = @_;
return @{$self->{_children}};
}
sub make_preorder_list ($$) {
my($self,$listref) = @_;
push(@$listref, $self);
foreach my $c ($self->children()) {
$c->make_preorder_list($listref);
}
}
sub display {
my($self,$indent) = @_;
print ' ' x (2*$indent), $self->{_nca_number}, "\n";
foreach my $c ($self->children()) {
$c->display($indent+1);
}
}
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
#########################
# change 'tests => 1' to 'tests => last_test_to_print';
package main;
use Test;
BEGIN { plan tests => 4, todo => [] };
use Algorithm::Tree::NCA;
ok(1); # If we made it this far, we're ok.
#########################
# Insert your test code below, the Test module is use()ed here so read
# its man page ( perldoc Test ) for help writing this test script.
my $Tree = Node->new(Node->new(Node->new(),
Node->new()),
Node->new(Node->new(),
Node->new(),
Node->new(Node->new(),
Node->new())));
# Matrix with precomputed values of the NCA for each pair of nodes in
# the tree above. Observe that node number 0 is never used.
my $NCA = [[(undef) x 11],
[undef, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # Node 1
[undef, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1], # Node 2
[undef, 1, 2, 3, 2, 1, 1, 1, 1, 1, 1], # Node 3
[undef, 1, 2, 2, 4, 1, 1, 1, 1, 1, 1], # Node 4
( run in 1.941 second using v1.01-cache-2.11-cpan-13bb782fe5a )