use strict;
use warnings;

use ChemPlugin;

$|=1;

my $nlevel = 4; 
print "Link ChemPlugin instances into a $nlevel bifurcating tree\n\n";

# Create the ChemPlugin instances
my $ninst = 2**$nlevel - 1;
my @cp = ();
for (my $i=0; $i<$ninst; $i++){
   push(@cp, new ChemPlugin());
}

# Link the instances into a tree
for(my $level=1; $level<$nlevel; $level++){
   for(my $i=2**($level-1); $i<2**$level; $i++){
      $cp[2*$i-1]->Link($cp[$i-1]);
      $cp[2*$i]->Link($cp[$i-1]);
   }
}

for (my$i=0; $i<$ninst; $i++){
   printf "Instance %2d is linked to %d instance(s)\n", $i, $cp[$i]->nLinks();
}

